MessageQueue msMq = msMq = new MessageQueue (queueName) Private static void ReceiveMessageFromQueue( string queueName) Following lines are catch blocks that execute if any error occur. The next line unboxed the body of the message to the Person object and writes its property on the console (you can see the first image above). Under the try block I have used XmlMessageFormatter object to specify the format of the message I have in the queue. To receive a message from queue, we need to first instantiate the queue using MessageQueue. You will need to make sure that the queue exists, otherwise you shall get an error. Look at the above picture - 2, you will see that there is a message displayed at the right side panel. msMq.Send("Sending data to MSMQ at " + ()) Ĭonsole. check if queue exists, if not create it Private static void SendMessageToQueue( string queueName) Next couple of lines are catch block that execute in case any error occur. Using Send method of the MessageQueue object, it sends the message to the queue. Next, under the try block, it creates the instance of the Person object and sets its values. Below method, first checks for the existence of the queue, if it doesn't exists then create one else use it. Now, lets see the actual SendMessageToQueue method. In this case I am using Person class that has two properties, FirstName and LastName as displayed below. Now, let's send a message to the MSMQ using C#īelow code receive a queue name and sends an object to the queue as message. " and then private followed by $ symbol and then the actual queue name is written. In the above code, you can see that the name starts with ". The queue name should be written in specific format in order to access them programatically. if queue doesn't exists, it creates otherwise use the existing one). My code snippet below does that automatically (ie. Alternatively, this can be done using C# code as well. Right click the Private Queues and select New > Priovate Queue and create a queue. Go to My Computer and right click, select Manage and explore the nodes as displayed in the picture below. Using System.Messaging Creating a Queue in the systemĬreating a queue in the system is an easy task. To work with MSMQ, you will need to use following namespace. NET How to's solution (ebook + source code + video) series based on real time project problems, click here to get it. Messages are stored in the queue of the system (Microsoft Operating System) and once the receiving application runs, it starts peeking the messages one by one. The benefit of MSMQ is that the receiving application doesn't have to run at the time of sending the message. MSMQ stands for Microsoft Message Queuing is one of the most reliable way to sending and receiving messages from one system to another system both system can be located at different geographical locations.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |