官术网_书友最值得收藏!

Setting up two-way communication over MSMQ

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.

Getting ready

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).

How to do it...

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.

  1. Define the ServiceContract that will be used for the MSMQ-based services. The following code snippet shows the sample ServiceContract (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);
        }

    Note

    All the service operations over MSMQ should be marked as one-way style.

  2. 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.
    How to do it...

    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); 
    }
    
  3. 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:
    How to do it...

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:

How to do it...

After the endpoints are correctly configured, we can host and consume the MSMQ-based service like any normal WCF service.

How it works...

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.

There’s more...

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.

See also

  • Complete source code for this recipe can be found in the \Chapter 2\recipe2\ folder
主站蜘蛛池模板: 徐汇区| 都匀市| 阳泉市| 兰坪| 克山县| 长丰县| 成都市| 新巴尔虎右旗| 梨树县| 舒兰市| 安徽省| 宿州市| 天气| 田阳县| 全州县| 辽宁省| 洛隆县| 顺昌县| 额尔古纳市| 海伦市| 昌都县| 历史| 海城市| 长治县| 进贤县| 三穗县| 同德县| 丰原市| 万安县| 随州市| 灵川县| 城市| 东港市| 湖州市| 灵川县| 苏尼特左旗| 青铜峡市| 榆树市| 遵化市| 大新县| 龙岩市|