C# to Write to MSMQ

This blog shows how to write to MSMQ from C#. In an earlier blog, I showed how to write to MSMQ from Powershell.

There are two tricks:
1) With Transactional Queues you have to include the Transaction, and also specify the second parm on the .Send method.
2) MessagePriority is only supported in non-transactional queues.

<pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;

namespace MSMQCSharpConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            string queueName = @"FormatName:DIRECT=OS:ServerName\Private$\biztalknontrans";
            string message = "<helloWorld><MyMessage language='English' /></helloWorld>";
            // a good label with a key and date/time helps to see what's in the queue if the queue gets backed up  
            string label = "Order=12345 Date=" + System.DateTime.Now.ToString("yyyyMMdd hh:mm:ss");
            //label = null; 
            MessagePriority msgPriority = MessagePriority.Highest;
            WriteMSMQNoTrans(queueName, message, label, msgPriority);

            //queueName = @"FormatName:DIRECT=OS:ServerName\Private$\biztalktrans";
            //WriteMSMQTrans(queueName, message, label);  // Transactional queues do not support priority 

            Console.WriteLine("\n\nPress enter to end:");
            Console.ReadLine(); 
        }

        public static void WriteMSMQNoTrans(string queueName, string messageText, string label, MessagePriority msgPriority)
        {
            MessageQueue mq;
            try 
            {
                mq = new System.Messaging.MessageQueue(queueName);
                UTF8Encoding utf8 = new System.Text.UTF8Encoding(); 

                byte[] msgBytes = utf8.GetBytes(messageText);
                System.IO.MemoryStream msgStream = new System.IO.MemoryStream(); 
                msgStream.Write(msgBytes, 0, msgBytes.Length); 

                Message mm = new Message(); 
                mm.BodyStream = msgStream; 
                if (label != null) 
                {
                   mm.Label = label;
                }
                mm.Priority = msgPriority;
                mq.Send(mm);
                Console.WriteLine("Message sent (NonTrans)"); 
            }
            catch (Exception ex) 
            {
                string errMessage = "QueueName: " + queueName + " Error:" + ex.Message; 
                Console.WriteLine(errMessage); 
                System.Diagnostics.EventLog.WriteEntry("Logger",errMessage); 
            }
        }

        public static void WriteMSMQTrans(string queueName, string messageText, string label)
        {
            try
            {
                MessageQueue mq;
                MessageQueueTransaction tran = new MessageQueueTransaction();
                tran.Begin();
                mq = new System.Messaging.MessageQueue(queueName);
                UTF8Encoding utf8 = new System.Text.UTF8Encoding();

                byte[] msgBytes = utf8.GetBytes(messageText);
                System.IO.MemoryStream msgStream = new System.IO.MemoryStream();
                msgStream.Write(msgBytes, 0, msgBytes.Length);
                
                Message mm = new Message();
                mm.BodyStream = msgStream; 
                if (label != null)
                {
                    mm.Label = label;
                }
                //mq.Send(mm);  // wow with transactioanl queue this will run, not give an error, but no data will be written 
                                // You must include second parm below. 
                mq.Send(mm, MessageQueueTransactionType.Single);  
                 

                Console.WriteLine("Message sent (Trans)");
                tran.Commit();
                tran.Dispose(); 
            }
            catch (Exception ex)
            {
                string errMessage = "QueueName: " + queueName + " Error:" + ex.Message;
                Console.WriteLine(errMessage);
                System.Diagnostics.EventLog.WriteEntry("Logger", errMessage);
            }

            
        }

    }  // end class 
}

</pre>

For further examples: https://msdn.microsoft.com/en-us/library/ms978430.aspx and https://support.microsoft.com/en-us/kb/815811

Uncategorized  

Leave a Reply