Creating An MSMQ In VB.NET (VS2012)

[Note: After I loaded up my server with hundreds of thousands of queued messages, I discovered an annoying default setting in Windows that you should know about. The blog post/article about it is HERE. To date I have not found a way to disable this limitation programmatically, as it is above the individual queue level… If you figure it out, please drop me a line! :)]

This gave me fits for a long time, as there are no viable examples to be found.

The gist of the thing is that you want to set up publish/subscribe “queue” in your apps, and on the target machine, for separate threads, or even portions of the same thread, to talk to each other, sending messages. Even though MSMQ is up to version 5 or so now, you can’t find any MS documentation on it.

Some of the nice things about queues are:

  • Asynchronous – they will hold onto messages (you can set size limits, or as much as your client’s HDD will hold).
  • Transactions Available – you can easily guarantee delivery of each message, or reduce overhead if the messages aren’t too important. You can also use things like CRCs, ordering, etc. to allow a higher level logic determine if a message should be re-sent.
  • Easy – As you will see below, they are drop dead easy to create.
  • Persistent – Once created on the machine, and not deleted, they will stay-even if your program(s) aren’t running.

We will deal with “Private Queues” here, which require nothing fancy. Public ones require domain/AD integration, etc. They work the same.

Pre-Requisites

1. Your PC (development or target) will not have MSMQ enabled by default. You add it in through Add/Remove, or Programs and Features, depending on your OS.

Navigate around until you see something like “Turn Windows features on or off“. Click the box next to “Microsoft Message Queue (MSMQ) Server“:

Enabling MSMQ
Enabling MSMQ

Next, we’ll add a reference to the MSMQ subsystem so our program will have access to the right classes & methods to use this server…

2. In your Visual Studio Project/Solution, add a Reference to the System.Messaging.dll:

That is it!

Create, Send, Receive, and Delete

This is the easy part:

…so now any time we call this sub, we know that the program will create (if not exists) a private queue called theq, then use it to send a message.

So here is the complete code, with a calling sub in a simple Module:

…and here is what you will see out your console when it runs:

…was this what you expected to see?

So how can we check/see that the queue is there? Here are the 2 easiest ways:

1. Computer Management (compmgmt.msc)

2. In Visual Studio, in Server Explorer:

I hope this helps the next poor sap that has to search all over just to find a simple, working example…

pat
:)

Leave a Reply

Your email address will not be published. Required fields are marked *