This is a discussion on Microsoft Messsage Queuing (MSMQ) in C# within the C# Programming forums, part of the Software Development category; There are several different kinds of queues available in the C# ASP .NET framework with the most popular one being ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| There are several different kinds of queues available in the C# ASP .NET framework with the most popular one being the standard Queue collection object. While these collections are easy to work with and fairly robust they are still stored in memory and therefore temporary. Often, in enterprise application design, more stringent means of managing and storing temporary data are required. In the case of receiving timely data to be processed, using a queue in your software design makes sense. When the data needs to be guaranteed for delivery and protected from loss, the Microsoft Message Queue MSMQ provides a scalable easy solution.
__________________ S.Balasubramanian Nothing is impossible |
| Sponsored Links |
| |||
| Creating a Message Queue and Add Private Messages in Local System: private string queueName = "private$\\test1"; try { System.Messaging.Message msg = new System.Messaging.Message(); msg.Body = "Hello"; System.Messaging.MessageQueue[] privateMessageQueues = System.Messaging.MessageQueue.GetPrivateQueuesByMa chine("localhost"); bool flag = true; foreach (System.Messaging.MessageQueue privareMessageQueue in privateMessageQueues) { if (privareMessageQueue.QueueName == queueName) { flag = false; break; } } if (flag) { System.Messaging.MessageQueue.Create(".\\" + queueName); } System.Messaging.MessageQueue msgQ = new System.Messaging.MessageQueue(".\\" + queueName); msgQ.Send(msg); } catch (Exception ex) { MessageBox.Show(ex.ToString()); }
__________________ S.Balasubramanian Nothing is impossible Last edited by Balasubramanian.S : 02-22-2008 at 01:10 AM. |
| |||
| Receiving Messages from Message Queue from Local System: private string queueName = "private$\\test1"; System.Messaging.MessageQueue msgQ = new System.Messaging.MessageQueue(".\\" + queueName); Object o = new Object(); System.Type[] arrTypes = new System.Type[2]; arrTypes[0] = TypeOf(String); arrTypes[1] = o.GetType(); msgQ.Formatter = new XmlMessageFormatter(arrTypes); string message= ((string)msgQ.Receive().Body); MessageBox.Show(message.ToString(), "Message Received!");
__________________ S.Balasubramanian Nothing is impossible Last edited by Balasubramanian.S : 02-24-2008 at 09:16 PM. |
| |||
| Creating a Public Message Queue and add public messages in this queue: private string queueName = ".\\test1"; try { System.Messaging.Message msg = new System.Messaging.Message(); msg.Body = "Hello"; if (!System.Messaging.MessageQueue.Exists(queueName)) { System.Messaging.MessageQueue.Create(queueName); } System.Messaging.MessageQueue msgQ = new System.Messaging.MessageQueue(queueName); msgQ.Send(msg); } catch (Exception ex) { MessageBox.Show(ex.ToString()); }
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Creating a Message Queue in another System (Through Lan) : private string queueName = "Machine Name\\test1"; try { System.Messaging.Message msg = new System.Messaging.Message(); msg.Body = "Hello'; if (!System.Messaging.MessageQueue.Exists(queueName)) { System.Messaging.MessageQueue.Create(queueName); } System.Messaging.MessageQueue msgQ = new System.Messaging.MessageQueue(queueName); msgQ.SetPermissions(userName, MessageQueueAccessRights.FullControl); msgQ.Send(msg); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } Machine Name is the name of the computer. If you give the IP address of the system, Message Queue could not be identified. Only, System name is identified. userName is the login name of the system. This user only can retrieve or add messages in this queue.
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Retrieving a message from Message Queue from another System (Through Lan) : private string queueName = "system name\\test1"; try { if(System.Messaging.MessageQueue.Exists(queueName) ) { string message; Object o = new Object(); System.Type[] arrTypes = new System.Type[2]; arrTypes[0] = typeof(string); arrTypes[1] = o.GetType(); messageQ.Formatter = new XmlMessageFormatter(arrTypes); message = ((string)messageQ.Receive().Body); MessageBox.Show(message.ToString(), "Message eceived!"); } else { MessageBox.Show("Queue does not exist"); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); }
__________________ S.Balasubramanian Nothing is impossible |
| |||
| MSMQ: Only public queues can be accessed by another system. We can get the count of Private Queues but not the messages in this queue from another system. Credential is very important in MSMQ. The specified user only can read the messages from the queue. The credential is set when the queue is created. msgQ.SetPermissions(userName, MessageQueueAccessRights.FullControl);
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Enable MSMQ: (XP OS): Start -------> ControlPanel -------> Add Or Remove Programs ---- > Add Or Remove Windows Components ------ > Message Queuing Enable MSMQ: (Windows 2003 Server): Start -------> ControlPanel -------> Add Or Remove Programs ---- > Add Or Remove Windows Components ------ > Application Servers ---- > Message Queuing
__________________ S.Balasubramanian Nothing is impossible |
| |||
| MSMQ takes advantage of various built in security features in Windows operating systems: * Access Control to restrict user access to Message Queuing objects * Authentication implemented through any authentication method supported by Windows 2000 including public key certificates, Kerberos or NTLM * Encryption through both asymmetric (public/private keys) or symmetric (secret key) cryptography is used to encrypt messages sent between Message Queuing components * Auditing to record which users attempt to access Message Queuing objects in Active Directory Through the use of Message Queuing, no message loss and ensures that messages are not modified or viewed except by authorized users.
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Get all private and Public Message Queues in Local System: Private Queues: System.Messaging.MessageQueue[] privateMessageQueues = System.Messaging.MessageQueue.GetPrivateQueuesByMa chine("localhost"); Public Queues : MessageQueueCriteria Crit = new MessageQueueCriteria(); Crit.MachineName = "localhost"; System.Messaging.MessageQueue[] publicMessageQueues = System.Messaging.MessageQueue.GetPublicQueues(Crit );
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Get All messages from Public and Private Queue : Private Queue : string queueName = "Private$\\test1"; System.Messaging.MessageQueue messageQ = new System.Messaging.MessageQueue(queueName); System.Messaging.Message[] message = messageQ.GetAllMessages(); Public Queue : string queueName = ".\\test1"; System.Messaging.MessageQueue messageQ = new System.Messaging.MessageQueue(queueName); System.Messaging.Message[] message = messageQ.GetAllMessages();
__________________ S.Balasubramanian Nothing is impossible Last edited by Balasubramanian.S : 02-27-2008 at 10:21 PM. |
| |||
| when we receive a message from Message Queue using the following method, the received message is deleted from MessageQueue. string message; Object o = new Object(); System.Type[] arrTypes = new System.Type[2]; arrTypes[0] = typeof(string); arrTypes[1] = o.GetType(); messageQ.Formatter = new XmlMessageFormatter(arrTypes); message = ((string)messageQ.Receive().Body);
__________________ S.Balasubramanian Nothing is impossible |
| |||
| We can receive a message from Message Queue without deleting the message from the Queue using the following method. string queueName = ".\\test1"; System.Messaging.MessageQueue msgQueue = new System.Messaging.MessageQueue(queueName); { Object o = new Object(); System.Type[] arrTypes = new System.Type[2]; arrTypes[0] = typeof(string); arrTypes[1] = o.GetType(); msgQueue.Formatter = new XmlMessageFormatter(arrTypes); System.Messaging.Message[] message = msgQueue.GetAllMessages(); if (message.Length > 0) { for (int i = 0; i < message.Length; i++) { string msg = (Payment)message[i].Body; MessageBox.Show(msg, "Message Received!"); } } else { MessageBox.Show("There are no messages in this Queue"); }
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Add messages to message Queue in Remote System : 1) Create a service that will run in the remote system. 2) This service contains the code to read or write the message queue. 3) Access this service from local system Service to Add message in the Queue: [WebMethod] public string AddMessage(string queueName, string message) { string retValue = ""; try { if (!MessageQueue.Exists(queueName)) { MessageQueue.Create(queueName); } System.Messaging.MessageQueue msgQ = new System.Messaging.MessageQueue(queueName); msgQ.SetPermissions("administrator", MessageQueueAccessRights.FullControl); Message msg = new Message(); msg.Body = message; msgQ.Send(msg); } catch (Exception ex) { retValue = ex.ToString(); } return retValue; }
__________________ S.Balasubramanian Nothing is impossible Last edited by Balasubramanian.S : 03-07-2008 at 10:18 PM. |
| |||
| Service to read messages from Message Queue from Remote System : [WebMethod] public string GetMessage(string queueName) { System.Messaging.MessageQueue messageQ = new System.Messaging.MessageQueue(queueName); System.Type[] arrTypes = new System.Type[2]; Object o = new Object(); arrTypes[0] = typeof(string); arrTypes[1] = o.GetType(); messageQ.Formatter = new XmlMessageFormatter(arrTypes); string message = (string)messageQ.Receive().Body; return message; }
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Application to Read messages from the service : Add the above services as a web reference. MSMQService.MSMQService msmqService = new MessageQueueServiceApplication.MSMQService.MSMQSer vice(); string message = msmqService.GetMessage(".\\test1"); test1 is the public queue. MSMQService is the webservice.
__________________ S.Balasubramanian Nothing is impossible |
| |||
| Application to Add messages in the queue in Remote System using Web service : Add the above service as a web reference. MSMQService.MSMQService msmqService = new MessageQueueServiceApplication.MSMQService.MSMQSer vice(); string retValue = msmqService.AddMessage(".\\test1", "Hello");
__________________ S.Balasubramanian Nothing is impossible |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Microsoft Information | Shanthi | Technology BUZZzzzzz | 5 | 03-21-2008 04:12 AM |
| Microsoft Message Queuing | krishnakumar | ASP and ASP.NET Programming | 1 | 02-17-2008 10:01 PM |
| Microsoft delivered a new version of its Microsoft Security Assessment Tool (MSAT) | senthilkannan | Microsoft | 0 | 12-19-2007 03:47 AM |
| Microsoft Windows XP SP3 | senthilkannan | Microsoft | 2 | 11-27-2007 10:08 PM |
| Microsoft bit | nhoj | Microsoft | 1 | 04-06-2007 02:03 AM |