Learn WCF with me (1) -- MSMQ message queue, wcfmsmq

Source: Internet
Author: User
Tags msmq

Learn WCF with me (1) -- MSMQ message queue, wcfmsmq
I. Introduction

Windows Communication Foundation (WCF) is a unified programming model provided by Microsoft to build service-oriented applications. The service model provides serialization functions that support loose coupling and version management, and provides Message Queue (MSMQ), COM +, Asp.net Web Services ,. NET Remoting and other existing Microsoft Distributed System Technologies. Using the WCF platform, developers can easily build service-oriented applications (SOA ). It can be considered that WCF refers to the existing distributed technology (MSMQ ,. NET Remoting and Web Services) integration and expansion. In this case, we need to first understand the previous distributed technology. Only in this way can we better understand the benefits of WCF. Today I will share with you the MSMQ distributed technology.

II. Introduction to MSMQ

The full name of MSMQ is Microsoft Message Queue-Microsoft Message Queue. It is an asynchronous transmission mode that enables communication between different applications. Applications that communicate with each other can be distributed on the same machine, it can also be distributed anywhere in the connected network space.

2.1 How MSMQ works

The implementation principle of MSMQ is: the message sender puts the information he wants to send into a container and saves it to the message queue in a public space of the system, the local or remote message receiving program then extracts the messages sent to it from the queue for processing.

A message queue is a public storage space that can exist in memory or physical files. Therefore, messages are sent in two ways: Express Delivery Mode and recoverable mode. The difference is that messages are stored in different locations. For the sake of fast message delivery, messages are stored in the memory instead of on the physical disk, in order to obtain high processing capability, while the recoverable mode writes messages to the physical disk in each step of the transfer process, in this way, when the machine that stores the Message Queue restarts after a fault occurs, the sent message can be restored to the status before the fault is sent for better message recovery capabilities. Message queues can be placed on the sender and receiver machines, or separately on another machine. In addition, with the message queue mechanism, the sender does not have to worry about factors such as the start and failure of the receiver. As long as the message is successfully sent out, the processing is complete, in fact, the other party may not even be on the machine, or the actual message delivered to the other party may be on the next day. The MSMQ mechanism is similar to the QQ message transmission mechanism. Demonstrate the implementation principle of MSMQ.

1 using System; 2 using System. messaging; 3 4 namespace MSMQServer 5 {6 class Program 7 {8 static void Main (string [] args) 9 {10 // create a public queue, public queues can only be created in the domain environment 11 // if (! MessageQueue. exists (@". \ LearningHardMSMQ ") // determines whether the queue already exists in this path 12 // {13 // using (MessageQueue mq = MessageQueue. create (@". \ LearningHardMSMQ ") 14 // {15 // mq. label = "LearningHardQueue"; // set the queue Label 16 // Console. writeLine ("A public queue has been created"); 17 // Console. writeLine ("Path: {0}", mq. path); 18 // Console. writeLine ("queue name: {0}", mq. queueName); 19 // mq. send ("MSMQ Message", "Leaning Hard"); // Send Message 20 //} 21 //} 22 23 // If (MessageQueue. exists (@". \ Private $ \ LearningHardMSMQ ") 24 // {25 // Delete message queue 26 // MessageQueue. delete (@". \ Private $ \ LearningHardMSMQ "); 27 //} 28 // create a Private message queue 29 if (! MessageQueue. exists (@". \ Private $ \ LearningHardMSMQ ") 30 {31 using (MessageQueue mq = MessageQueue. create (@". \ Private $ \ LearningHardMSMQ ") 32 {33 mq. label = "LearningHardPrivateQueue"; 34 Console. writeLine ("A private queue has been created"); 35 Console. writeLine ("Path: {0}", mq. path); 36 Console. writeLine ("Private queue name: {0}", mq. queueName); 37 mq. send ("MSMQ Private Message", "Leaning Hard "); // send message 38} 39} 40 41 // traverse all public message queues 42 // foreach (MessageQueue mq in MessageQueue. getPublicQueues () 43 // {44 // mq. send ("Sending MSMQ public message" + DateTime. now. toLongDateString (), "Learning Hard"); 45 // Console. writeLine ("Public Message is sent to {0}", mq. path); 46 //} 47 48 if (MessageQueue. exists (@". \ Private $ \ LearningHardMSMQ ") 49 {50 // get Private message queue 51 MessageQueue mq = new MessageQueue (@". \ Private $ \ LearningHardMSMQ "); 52 mq. send ("Sending MSMQ private message" + DateTime. now. toLongDateString (), "Leaning Hard"); 53 Console. writeLine ("Private Message is sent to {0}", mq. path); 54} 55 56 Console. read (); 57} 58} 59}

The server code must note that public queues can only be created in the domain environment. Because my PC is not added to the domain environment, public queues cannot be created, from the beginning of the message queue, we can see that there is no public queue installed in the figure.

After the server is implemented, the client is completed. The principle of the MSMQ program is that the server sends messages to the shared message queue, and then the client extracts the messages from the shared message queue for processing. The implementation code of the specific client is as follows:

1 using System; 2 using System. messaging; // you need to add System. messaging references 3 4 namespace MSMQClient 5 {6 class Program 7 {8 static void Main (string [] args) 9 {10 if (MessageQueue. exists (@". \ Private $ \ LearningHardMSMQ ") 11 {12 // create a message queue object 13 using (MessageQueue mq = new MessageQueue (@". \ Private $ \ LearningHardMSMQ ") 14 {15 // sets the Message Queue formatter 16 mq. formatter = new XmlMessageFormatter (new string [] {"System. string "}); 17 foreach (Message msg in mq. getAllMessages () 18 {19 Console. writeLine ("Received Private Message is: {0}", msg. body); 20} 21 22 Message firstmsg = mq. receive (); // obtain the first message 23 Console in the message queue. writeLine ("Received The first Private Message is: {0}", firstmsg. body); 24} 25} 26 Console. read (); 27} 28} 29}
4.3 run demo

After the above steps, we have completed the implementation of the MSMQ distributed program. Let's take a look at how to run the program to view the results.

First, you need to start the server, right-click the MSMQServer project-> debug-> Start a new instance to start the server, as shown in the following steps:

Sample Code File: MSMQSample.

 


Teach a Message Queue Msmq to receive messages

It's the peek method. Returns the copy of the first message in the queue without removing the message from the queue.

Windows does not support the Message Queue (Microsoft Message Queue (MSMQ) server) option.

Hello, this item exists in WIN7. The individual steps are as follows:
Start 1 Control Panel 1 program 1 click "Start and close WINDOWS function" under "programs and functions" on the right"
In this way, you can see the Microsoft Message Queue (MSMQ) server option. It will be OK ..

I hope my answers will help you !!

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.