Common issue with MSMQ that hurts

So all to often as developers, we find ourselves fighting some code or application until your blue in the face. This article is about one of those instances when using MSMQ as your message delivery system. Although it can often be very simple to set up, especially when doing it through Active Directory, but there may be situations where you do not have this option (like setting up MSMQ over a DMZ or the Internet).

When this happens, it is necessary to make sure that ALL the options on both ends of the MSMQ pipeline are set up exactly the same! Below are the options that are required to be the same on both ends:

1.) Recoverable

2.) UseJournalQueue

3.) AcknowledgeType

The other portion that must be set correctly is the path of the MSMQ server on the client end. If you are using something internal, the typical syntax is : FormatName:Direct=OS:serverName\\private$\\queueName.

When going across networks or the internet (where the DNS name may not be attainable, the syntax is: FormatName:Direct=TCP:serverName\private$\queueName.

Another note to add here is the DeadLetterQueue option. Although this is not required, it is still a nice thing to add for troubleshooting issues as sometimes you may get lucky and if the message gets to the server, but the server isn’t sure what to do with it, the message will be placed in that queue if DeadLetterQueue is set to true.

An example of what the code might look like:

                string mqPath = null;
                mqPath = @"FormatName:Direct=TCP:serverName\private$\queueName;

                if (string.IsNullOrEmpty(mqPath))
                {
                    return returnValue;    // if no queuepath, then do not even try to Send.
                }

                // create the data packet
                Message message = new Message();

                message.Body = someString;

               MessageQueue mq = new MessageQueue(mqPath);

                try
                {
                    message.UseDeadLetterQueue = true;
                    message.UseJournalQueue = true;
                    message.AcknowledgeType = AcknowledgeTypes.FullReachQueue | AcknowledgeTypes.FullReceive;
                    message.Recoverable = true;
                    mq.Send(message);
                }
                
                catch(Exception ex)
                {
                    // Handle failure
                }

 

Hope this helps others in the code struggle!

 

Happy coding!

Advertisements

About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in .NET, .NET 4.5, ASP.NET, C#, MSMQ, MVC and tagged , , , , . Bookmark the permalink.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s