Ads Top

Setup MSMQ with WCF using Net.Msmq binding

Message Queuing provides guaranteed message delivery, efficient routing, security, and priority-based messaging. For this purpose Microsoft MSMQ's are widely used in many applications. However, reading messages from queue can be tough & a lengthy process. To read MSMQ messages using C# /VB, refer Read messages from MSMQ using C#/VB

However, if you need to process queue messages from within a service, WCF (Windows Communication Foundation) provides a binding Net.MSMQ specifically for this purpose. In this blog i will take you through the step by step process to setup your WCF services with MSMQ.

  1. Create a WCF service which needs to real messages from MSMQ. Here, i am creating an Agreement service, which picks up the request from the queues and processes the request. Create an operation contract with IsOneWay = true.OperationContract

  2. Create the service which implements the operation contract created above.ServiceContract.png

  3. Open web.config for your WCF Service project. Within <system.serviceModel>, specify <netMsmqBinding> settings with exactlyOnce="true". Here in this example to keep things simple, we do not have any security implemented. Bindings

  4. Now, before defining the endpoint, let's create a queue. In order to enable MSMQ support on Windows platform (Windows Server 2012/2008 R2, 7, Vista), enable MSMQ from "Turn Windows Features on or off" from your control panel.MSMQ.png

  5. To add a private queue, Goto Computer Management->Service and Applications->Message Queuing. Right click on Private Queues option, New->Private Queues.Here to keep the things simple, we are dealing with Non-Transactional queues, will discuss about transactional queue in future post.Here i have provided the name of the queue as <virtualdirectory_name>/<service_name.svc>. Please follow the naming convention to keep the things simple. The name of the message queue must be the same as of the service file name, including the .svcNew Queue

  6. Now right click on the queue created, select properties and add full permissions for IIS_IUSRS & NETWORK SERVICE.

  7. Now that we are done with creating the queues, let's add this endpoint to WCF web.config. Define the endpoint with address as net.msmq://localhost/private/<virtualdirectory_name>/<service_name.svc>. This refers to the queue created above.

  8. Now that you are done with configuration, host the WCF service on IIS with <virtualdirectory_name>. The last step is to configure IIS 7 to use WAS to listen to the message queue and activate your service when new messages arrive. There are two parts to this: first you need to activate the net.msmq listener for the entire web site, and second you need to enable the protocols needed for your specific application. You can perform both of these steps using either the appcmd.exe tool (located under C:\Windows\System32\Inetsrv) or by editing the C:\Windows\System32\Inetsrv\config\ApplicationHost.config file in a text editor. Let's go with the former, since it's a bit less dangerous.To enable the net.msmq listener for the entire web site, use the following command. Note that the bindingInformation='localhost' bit is what tells the listener which machine is hosting the queues that it should listen to. This will be important when we want to start listening to remote queues.appcmd set site "Default Web Site" -+bindings.[protocol='net.msmq',bindingInformation='localhost']To enable the net.msmq protocol for our specific application, use the following command. Note that you can configure multiple protocols for a single application, should you want it to be activated in more than one way (for example, to allow either MSMQ or HTTP you could say/enabledProtocols:net.msmq,http).appcmd set app "Default Web Site/MsmqService" /enabledProtocols:net.msmq

  9. Enable the Net.Msmq Listener Adapter Windows service & ensure this is running.

  10. We are done with the setup. Push the message to MSMQ & the message will pe picked up by the WCF service for processing.

No comments:

Powered by Blogger.