Developing message handlers using C # and MSMQ

Source: Internet
Author: User
Tags mail message queue reference serialization msmq visual studio
Program
Brief introduction



MSMQ (Microsoft Message Queuing) is the foundation of messaging applications in Windows operating systems and is a development tool for creating distributed, loosely-connected messaging applications. There are many similarities between Message Queuing and e-mail, they all contain multiple properties for saving messages, which indicate both the sender and the recipient's address, but their usefulness is very different: the sender and receiver of message queues are applications, and the sender and receiver of e-mail messages are usually people.



As with e-mail, Message Queuing sends and receives without the need for both the sender and receiver to be present, and can be stored in message queues or mail servers. Therefore, we can use the following figure to describe the architecture of an MSMQ application:


As you can see from the above illustration, it is not very difficult to develop an MSMQ application. However, to develop your message handlers using MSMQ, you must install Message Queuing on the host that is developing the system and using the program. The installation of Message Queuing belongs to the installation of Windows components, similar to the general method of installing components. Once you've installed the message queue, you can develop your own message handlers. However, it is important to note that if your computer is in a workgroup, not a domain, your public queues may not be available, but that does not affect your program development.



Message handlers are all about sending and receiving messages, however, to send and receive messages, you must also refer to a queue, which usually refers to public queues and private queues, both of which hold user-generated messages. After the queue is referenced, the message can be sent, received, and read. The message receiving service is located in System.Messaging, and if you cannot find this namespace, you must add it manually. Click [Add Reference] in [item], press browse button, find System.Messaging.dll file, add in.



Reference queues



There are three ways to reference queues, which refer to queues through paths, format names, and tags, and I'll just introduce the simplest and most common method: applying queues through paths. The queue path is in the form of machinename\queuename. The path to the queue is always unique. The following table lists the path information for each type of queue:



Queue type
Syntax used in the path

public queues
Machinename\queuename

Private queues
Machinename\private$\queuename

Journal queues
machinename\queuename\journal$




If you are sending to this computer, you can also use the "." Represents the native name. The specific reference method is done through the Path property, or it can be done when the message queue is initialized.



If a message queue is referenced at initialization time, Message Queuing must exist in the system, or an interruption will occur. Adding queues to the system is very simple, open [Computer Management] in Control Panel, expand Services and applications, locate and expand Message Queuing (if you cannot find it, say you haven't installed Message Queuing), right-click the category of Message Queuing you want to add, and select New queue. Of course, the creation of message queues can also be implemented in programs, as described below. The code that references Message Queuing at initialization is simple, as follows:



MessageQueue mq=new MessageQueue (". \\private$\\jiang");

The code that references Message Queuing through the path attribute is also simple:

MessageQueue mq=new MessageQueue ();

Mq.path= ". \\private$\\jiang";

Use the Create method to create queues on your computer:

System.Messaging.MessageQueue.Create (@ ". \private$\jiang");



Send a message



After the queue is referenced, the message can be sent. Message delivery can be divided into simple messages and complex messages, simple message types are commonly used data types, such as integers, strings and other data, the data types of complex messages typically correspond to complex data types in the system, such as structs, objects, and so on.



Examples of sending simple messages are as follows:

Mq.send (1000); Send integer data

Mq.send ("This is a Test message!"); Send string



The sending of complex messages is much the same as the sending of simple messages, but when they are sent, they are usually not given directly to the content of the message sent, but rather a variable representing the content of the message sent. The following code sends a complex message, respectively, through a message variable and a complex data type variable.



The message that is sent in the following code is represented by a message variable

Message MSG;

Msg=new message ("A Complex message!");

Msg.label= "This is the Label";

Msg.priority=messagepriority.high;

Mq.send (MSG);



The message sent in the following code is represented by a complex data type variable, and the customer is a custom class

Customer customer = new Customer ();
Customer. LastName = "Copernicus";

Customer. FirstName = "Nicolaus";

Mq.send (customer);



Receive Message



Sending messages is a bit more complicated than receiving a message. There are two ways to receive messages: To receive messages through the Receive method and to permanently remove messages from the queue, and to remove the message from the queue through the Peek method without removing the message from the queue. If you know the identifier (ID) of the message, you can also do so by using the ReceiveById method and the PeekById method.



The code to receive the message is simple:

Mq.receive (); or Mq.receivebyid (ID);

Mq.peek (); or Mq.peekbyid (ID);



Read the message



The message received is only useful if it is readable, so you must be able to read the message after receiving the message, and reading the message is the most complicated operation. Message formats are different in messages and message queues that the application can read. As a result, the messages sent by the application are serialized before they are sent to the message queue, which is done automatically by the system, which the program developer does not have to write code for, but is confronted with the problem of message serialization after receiving the message.



The serialization of messages can be accomplished by using the three predefined formatters that are included with visual Studio and the. NET Framework: The XMLMessageFormatter object (the default formatter setting for the MessageQueue component), BinaryMessageFormatter object, ActiveXMessageFormatter object. Since the later two formatted messages are usually unreadable, we often use the XMLMessageFormatter object.



The code that uses the XMLMessageFormatter object to format the message is as follows:

String[] types = {"System.String"};
((XMLMessageFormatter) MQ. Formatter). TargetTypeNames = types;

Message m=mq. Receive (New TimeSpan (0,0,3));
After the received message is passed to the message variable, the message can be read by the Body property of the message variable m:
MessageBox.Show ((string) m.body);
Close Message Queuing
The shutdown of Message Queuing is simple, and like other objects, the Close function enables:
Mq.close ();
So far, the basics of the MSMQ application are complete, but it is clearly not so easy to develop a powerful MSMQ application, and for more detailed information you can refer to the Help content for Message Queuing in the MSDN and Windows operating systems.


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.