What about Messaging? What about MSMQ? Here is everything all in one place.

 
Alright, so I’ve fooled around MSMQ in VB6 before when it first came out just to see what it’s about, but what about in .NET? What’s it for anyway?
Message Queueing in general is a place where you can store “stuff” until it can be processed. That “stuff” can be something as simple as a number, to a string, to a complex object that has been serialized. Serialized is a concept where you basically have an instance of a class in memory, you “write down” absolutely everything you need to recreate it. Then later, you can “deserialize” that object. Serialization is just like freezing an object, putting it in the freezer – then later on, bringing it back to life in the microwave.
Serialization is a cool concept, because you can serialize ANYTHING. In .NET, it’s a built-in part of the “object” class, the class which all classes inherit. So the only thing you have to do to make a class serializable – is to add an “attribute” at the class level, like this:
[Serializable]
public class MyCustomMessage
That’s it!
Anyhow, back to this issue of Messaging. So why would you use messaging anyway? Imagine you have a website and you do credit card processing. You use some Internet gateway that is supposed to be up 24/7. You have try/catch blocks in case it’s not up – but imagine, a user is trying to give you money – and the gateway is down. You can’t process the transaction! You just lost a sale! Or what if the gateway is just slow?
Now the users are experiencing slow response because of your vendor!
Now imagine, you put the transactions into a queue (encrypted), and your webpage waits – say 5 seconds, for it to be processed. If it isn’t – the user sees a “Thank you for your order, you’ll hear from us shortly!“ message. If everything is fine, your users see a “Thank you for your order, here is your confirmation!“. Point is, messaging is a buffer between your real-time (like a website) applications and uncontrollable/imtermittant services (like credit card processing).
So there will typically be two parts to a messaging solution – something that submits things to the queue, and an application to process the queue. That will typically not be a web application, but maybe a Windows service or some other kind of app – depending on your needs.
This sounds great – where do I start!!?
Well first, add a reference to your .NET project to System.Messaging. And then here are the basic things you will need:
View the queues on your machine:
Right-click “My Computer“ and choose “Manage“, the “Services and Applications“ – then “Message Queueing“. Under that, the “Private Queues“ are what we are dealing with today.
Create a queue programatically:
if ( !System.Messaging.MessageQueue.Exists(".Private$MyTestQueue") )
{
    System.Messaging.MessageQueue.Create(".Private$MyTestQueue");
}
Add generic text to a queue:
System.Messaging.MessageQueue objMsgQ = new System.Messaging.MessageQueue(".Private$MyTestQueue");
objMsgQ.Formatter = new System.Messaging.XmlMessageFormatter();
objMsgQ.Send("test", "test label");
Read generic text from the queue (this was about the best I could figure out):
System.Messaging.MessageQueue objMsgQ = new System.Messaging.MessageQueue(".Private$MyTestQueue");
objMsgQ.Formatter = new System.Messaging.XmlMessageFormatter();
System.Messaging.Message objMsg = objMsgQ.Receive();
byte[] bytBuff = new Byte[1024];
objMsg.BodyStream.Read(bytBuff, 0, 1024);
this.textBox2.Text = System.Text.ASCIIEncoding.ASCII.GetString(bytBuff);
But writing out text doesn’t seem to be the purpose of messaging. Seems like the more natural way to do things, is to queue complex objects. So here is very simple class, that is marked as serializable:
[Serializable]
public class MyCustomMessage
{
    public MyCustomMessage()
    {
    }
    public string MessageBody = "";
    public int SomeInteger = -1;
}
Now, here’s how I would send this complex object:
System.Messaging.MessageQueue objMsgQ = new System.Messaging.MessageQueue(".Private$MyTestQueue");
System.Messaging.Message objMsg = new System.Messaging.Message();
objMsgQ.Formatter = new System.Messaging.BinaryMessageFormatter();
MyCustomMessage objMCM = new MyCustomMessage();
objMCM.MessageBody = "this is text specific to this instance";
objMCM.SomeInteger = 12304;
objMsgQ.Send(objMCM, "Test app: " + this.textBox1.Text);
And then read it back and populate an instance of MyCustomMessage, I’d do this:
System.Messaging.MessageQueue objMsgQ = new System.Messaging.MessageQueue(".Private$MyTestQueue");
objMsgQ.Formatter = new System.Messaging.BinaryMessageFormatter();
System.Messaging.Message objMsg = objMsgQ.Receive();
MyCustomMessage objMCM = (MyCustomMessage)objMsg.Body;
this.textBox2.Text = "Message body: " + objMCM.MessageBody + Environment.NewLine + "Number: " + objMCM.SomeInteger;
So hopefully this makes some sense. I had quite a time finding out how to deserialize correctly – but this is a working example.
And to tie that back to my original concept – imagine you have a class called “CreditCardOrder“, and you have a bunch of properties or public members that are like “CustomerFirstName“, “CustomerLastName“, “CreditCardNumber“, etc, etc.. you populate that class, submit it to the queue and move on. It’s pretty cool stuff!
Posted in MSMQ, Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2 other followers

%d bloggers like this: