I was Queuing a Serialized object in MSMQ. But soon realized that VB (legacy) do not like the format.
public void QueueJob(Job job, string destinationQueue, string messageLabel)
{
try
{
// open the queue
MessageQueue mq = new MessageQueue(destinationQueue);
// set the message to durable.
mq.DefaultPropertiesToSend.Recoverable = true;
// send the job object
mq.Send(job, messageLabel);
mq.Close();
}
catch (Exception e)
{
throw e;
}
}
The Queued Serialized Job Object was picked up by VB as "?????????????????"
For example when I queued a string= "Hello"
VB(Legacy) would pick up from the queue as "?????"
I did the opposite then to find out what is wrong:
I Queued from VB(legacy) "Hello"
I discovered via going through the Message.BodyStream of .Net that the stream is returning something like this: "H0\e0\l0\l0\o and was appending a "0\" in front of every character.
I soon realized that the default formatting of VB(legacy) and .Net Serialized object is not same.....
Soon discovered (after googling and from a link from my collegue) that Message should be Queued using ActiveXMessageFormatter for VB(legacy) to pick it correctly.
Here is the final code:
public void QueueJob(Job job, string destinationQueue, string messageLabel)
{
try
{
// open the queue
MessageQueue mq = new MessageQueue(destinationQueue);
// set the message to durable.
mq.DefaultPropertiesToSend.Recoverable = true;
// send the job object
mq.Formatter = new ActiveXMessageFormatter();
XmlSerializer xmlSer = new XmlSerializer(job.GetType());
StringWriter sWriter = new StringWriter();
// Serialize the job to xml.
xmlSer.Serialize(sWriter, job);
mq.Send(sWriter.ToString(), messageLabel);
mq.Close();
}
catch (Exception e)
{
throw e;
}
}
What I have done here is got the string of the the Job object using the StringWriter after serializing it. And specified the Formatter to be ActiveXMessageFormatter, and then Send the message as usual to the Queue.