For a long time, MSMQ has been a great message-based communication component on the Microsoft platform. And the Microsoft .NET framework has also provided managed programming interfaces for developers to develop distributed applications based on MSMQ. However, it is still a bit complicated and time consuming for developers to build a complete distributed service through the raw or encapsulated MSMQ programming interface. As the new unified communication development platform on Windows, WCF provides more convenient means for developing a distributed service over the underlying MSMQ component.
If you are not yet familiar with Microsoft Message Queuing (MSMQ), you can get useful information on the following site:
http://msdn.microsoft.com/en-us/library/ms711472(VS.85).aspx
Also, the MSDN library has provided detailed reference on the .NET Framework System.Messaging
namespace that encapsulates the raw MSMQ programming interfaces (visit http://msdn.microsoft.com/en-us/library/system.messaging.aspx).
To make the WCF client and service perform two-way communication over MSMQ, we need to set up two MSMQ-based endpoints—one for the service to receive client requests and the other for the client to get responses.
- Define the
ServiceContract
that will be used for the MSMQ-based services. The following code snippet shows the sampleServiceContract
(one for the service and another for the client):[ServiceContract] public interface INotificationReceiver { [OperationContract(IsOneWay = true)] void Notify(long id, string msg, DateTime time); } [ServiceContract] public interface INotificationSender { [OperationContract(IsOneWay=true)] void Ack(long id); }
- Create the MSMQ queues on the client and server machines. There are two means for us to create the queues. One way is to use the MMC snap-in. There are various ways to start up this snap-in, but the easiest is to open the Windows Computer Management utility in Administrative Tools, expand the Services and Applications section of the tree view on the left, and select the Message Queuing node. This is also a great way to verify that MSMQ is installed on a particular machine. The next screenshot shows the standard UI of the MSMQ snap-in.
Another way is to create the queue programmatically, is as shown in the following code. In the sample service here, we will create the MSMQ queues in code:
private static void Init() { // Ensure the message queue exists string qName = ConfigurationManager.AppSettings[“ReceiverQueue”]; if (MessageQueue.Exists(qName)) MessageQueue.Delete(qName); MessageQueue q = MessageQueue.Create(qName, false); }
- After the message queues have been created, we can start configuring the service and client endpoints and map them to the underlying MSMQ queues. For the receiver side, the service endpoint should use NetMsmqBinding and set the address in
net.msmq://
format. The following screenshot shows a sample service endpoint configuration:
The private
in the endpoint address indicates that the MSMQ queue is a private queue and NotificationReceiver
is the queue name.
The sender side will need to configure the client endpoint that uses the same configuration as the service endpoint at the receiver side, which is done as follows:

After the endpoints are correctly configured, we can host and consume the MSMQ-based service like any normal WCF service.
Since MSMQ physically only supports one-way message delivery, we need to host a MSMQ-based WCF service on both the client and service machine so as to establish the two-way communication.
Also, WCF NetMSMQBinding is a WCF natural binding, which completely hides the underlying MSMQ processing details; developers only need to concentrate on the ServiceContract
and service endpoint configuration, instead of the raw System.Messaging
programming interfaces. However, in some cases, if you need to establish communication between raw MSMQ application and WCF-based application, there is another built-in binding called MsmqIntegrationBinding, which is suitable for such scenarios.
You can have a look at the article How to: Exchange Messages with WCF Endpoints and Message Queuing Applications at http://msdn.microsoft.com/en-us/library/ms789008.aspx for more information.