C # uses Message Queuing, including remote access

Source: Internet
Author: User
Tags domain server msmq

Recently do a small functional requirements, using the queue, with a lot of problems, and now summed up, hoping to be able to help people in need.

My requirements are simple, that is, multiple clients connected to one of my small data forwarding server, started using socket communication to achieve this function, once the data server received from different clients sent messages, These messages are processed (I am here to receive the data and then forwarded to another server), but given that the client sends information to the server every other short period of time, and even when the number of pick-up service is more, it is feared that there will be concurrent access problems, and also want to avoid the data forwarding server There are other unknown problems with frequent access to information from multiple different threads, so the way the queue is handled when the client sends information to the data forwarding server.

In general, the use of MSMQ, the first to install the message service, this direct Baidu on the line;

Add the messaging reference in VS, you can use the MessageQueue class, and then you need to think clearly about the flow of your data (message), before because of their own wrong understanding of the queue, to exactly where to create the queue, the queue of messages by WHO to send and receive not clear, There are also references to some of the writing is not too clear blog, around a good big circle, so today in my own project needs as an example, explain 1, how to create a queue 2, how to send a message to the queue 3, how to get messages in the queue

First, create a queue: According to my needs, I want to send information to the data forwarding server through socket communication, so in order to avoid the generation of concurrent access problems, Message Queuing should be established on the data forwarding server;

 MessageQueue myqueue = ;  string  QueuePath = @ " .\private$\queuedemo   ;  if  (! = messagequeue.cr            Eate (QueuePath); } myqueue  = new  MessageQueue (queuepath); 

This creates a message queue named Queuedemo on the data forwarding server, and the message from the client is saved in this queue, and you can see the Queuedemo queue you created in the message queue under Computer Management--services and applications, private$ Keyword is the description of the team as a dedicated queue, if there is no such keyword to configure the domain server, or is quite troublesome, this or with the help of Baidu Bar, the front of the "." Represents the created queue directory is native, once the queue is created, it is the system, the next thing to do is how you write the message into this queue, or read the value of the queue to pay special attention here , do not write the QueuePath path string

string @" Formatname:direct=tcp:192.168.1.153\private$\queuedemo ";

This is written for the remote computer to access the queue, because MessageQueue's create () and Exisit () methods are not able to identify the above FormatName format, and to ensure that the Create () The function is executed and then referenced with the MessageQueue instance, so that the server-side queue is created;

In the client, send the message to the queue;

 string  s =  "client   The message sent to the queue   ); System.Messaging.Message msg  = new              System.Messaging.Message (); Msg.            Body  = S; Msg. Formatter  = new  xmlmessageformatter (new  type[] {typeof  (string  ) });            
MessageQueue MQ = new MessageQueue (@ "Formatname:direct=tcp:192.168.1.153\private$\queuedemo"); Mq. Send (msg);

In the client, using a MessageQueue instance to point to the queue path created on the server's local machine , the path in the constructor of the MessageQueue instance must be in formatname format. Specify whether TCP communication or HTTP or machine as shown in my code above, and then call the Send () method, write the message into the queue, this request to send the object to be serialized in the way, so to set formatter, It's xmlmessageformatter and BinaryMessageFormatter, and so on. Note that the body of the message that holds your message is of type object and therefore can send objects of any class you write to the message queue

Receiving Message Queuing in the server

MessageQueue MQ =NewMessageQueue (@".\private$\queuedemo"); Mq. Formatter=NewXMLMessageFormatter (NewType[] {typeof(string) }); Thread th=NewThread (() =                {                     while(true) {System.Messaging.Message msg=MQ.                        Receive (); if(msg! =NULL{MessageBox.Show (msg).                        Body.tostring ());            }                    }                }); Th. IsBackground=true; Th. Start ();

On this computer, you can create a new queue instance to point to the local queue, and then deserialize the message body in the previously agreed serialization format so that the Foarmatter property of the new queue instance is assigned to the formatter property at the time of the Send as shown in the code, and this time directly with receive () Get the message body, and then handle the information in the body of the message, I am here to open a thread to display the queue message, as long as there is a new message to write, I am in the message box output

At this time, the client may not be able to successfully send messages to the remote server because the permissions of the message queue of the server are not open to unauthenticated clients. You want to assign the corresponding permissions in the server queue if you want to read the contents of the queue, you also need to add system variables

Problem-solving approach

1. Server-side

        • Message Queuing permission settings on the server: Assign all permissions to anonymous logon;

        • Modify the server's registry to allow non-authenticated client access
          • Registry new HKLM\SOFTWARE\MICROSOFT\MSMQ\PARAMETERS\SECURITY\ALLOWNONAUTHENTICATEDRPC entry, set its DWORD value to 1
          • Registry new Hklm\software\microsoft\msmq\parameters\security\newremotereadserverdenyworkgroupclient entry, set its DWORD value to 1

            For security access control instructions for MSMQ see: http://msdn.microsoft.com/en-us/library/4108f68e-80f5-40e1-b3df-b713cc4dff79 (prot.20). aspx

This allows the client to read the queue information on the server. Of course, the general business logic does not do this because he is only responsible for sending messages, in summary, is the use of Message Queuing across the server to read and write the most basic usage

C # uses Message Queuing, including remote access

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.