I want to share how to use
netMsmqBinding in WCF today. I need to create a solution which able to support
asynchronous operation, which mean the WCF service is down but the client still able to make request. The client request will be kept in a queue in
MSMQ, and then when the WCF service is back online, it will process whatever requests which are pending in the queue.
First, we need to configure MSMQ. But before that, we need to make sure MSMQ had already been installed. My operating system is Windows 2008 R2, here are the steps to check whether MSMQ had been installed, otherwise we need to install it.
1. Open up
Server Manager.
2. Check whether you see Message Queuing is shown in the features list? If yes, skip to step 6.
3. Click the
Add Features link button.
4. Check the
Message Queuing checkbox.
5. Click the [Install] button. You may need to reboot your server.
6. After the installation is done, you will notice "
Message Queuing" is appearing under the Features tree.
7. Proceed to collapse it and then try to create a new queue by right clicking the Private Queue, and then click the New ->
Private Queue.
8. Give a name for your new queue and then click the [OK] button.
9. Right click the newly created queue, and then click the
Properties.
10. If your server is not joined with any domain and it is running in
WORKGROUP, you need to set the Privacy level to
None. And, un-check the
Authenticated box.
11. Then, go to Security tab, set
Allow Full Control to Everyone. For better security control, it is recommended to grant Allow Full Control permission to the
application pool identity account only (for example, IIS APPPOOL\AppPoolName) which is going to be used to host the WCF service.
Now, it is time to code your WCF service. After you are done with your coding, here are the web configuration that you need for
netMsmqBinding. For
WORKGROUP server, you need to set security mode, protection level as
None.
<bindings>
<netMsmqBinding>
<binding name="basicMsmq" exactlyOnce="false" >
<security mode="None">
<transport msmqAuthenticationMode="None" msmqProtectionLevel="None"/>
</security>
</binding>
</netMsmqBinding>
</bindings>
Then, for the service endpoint configuration, you need to set your queue name which you had created just now at the endpoint address:
<services>
<service name="<YourServiceName>"
behaviorConfiguration="<YourServiceBehavior>">
<endpoint name="netMsmqServiceEndpoint"
address="net.msmq://localhost/private/<YourQueueName>"
binding="netMsmqBinding" bindingConfiguration="basicMsmq"
contract="<YourServiceContractName>" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
When you are done, host your WCF service to IIS. Before that, you need to make sure IIS had been enabled with
net.msmq binding. You can perform the following steps to check.
1. Open up
IIS Manager.
2. Right click the
Default Web Site (assume you are going to host your application in a virtual directory in default website).
3. Click the
Edit Bindings... menu.
3. If you see
net.msmq binding type is in the list, you skip to steps 6. Otherwise, click the [Add] button.
4. Select
net.msmq type from the drop down list, and then enter localhost as binding information.
5. Click the [OK] button.
6. Now, create a
new application in default website that host your WCF service.
7. Select the newly created application, and then open
Advanced Settings.
8. At the
Enabled Protocols properties, append
net.msmq to the existing value with comma delimiter.
9. Click the [OK] button, and your application is enabled with MSMQ binding.
Before proceed for testing, need to make sure Net.Msmq Listener Adapter windows service is running.
Now, you can start testing your WCF service with a browser, go to the service url and try to get the
WSDL. After tested everything works fine, you are good to go to create a WCF client application.
However, there are a few common errors that you may encounter during the service setup, but the given error message is actually not really meant for it.
Common Error 1:
An error occurred while opening the queue:Access is denied. (-1072824283, 0xc00e0025). The message cannot be sent or received from the queue. Ensure that MSMQ is installed and running. Also ensure that the queue is available to open with the required access mode and authorization.
Root Cause:
The IIS process worker has insufficient permission to read the queue.
Resolution:
You need to make sure the application pool identity account or everyone is given appropriate access.
Common Error 2:
An error occurred while opening the queue:This operation is not supported for Message Queuing installed in workgroup mode. (-1072824214, 0xc00e006a). The message cannot be sent or received from the queue. Ensure that MSMQ is installed and running. Also ensure that the queue is available to open with the required access mode and authorization.
Root Cause:
The service endpoint address is invalid.
Resolution:
Make sure the endpoint address is in the following format:
For example:
net.msmq://localhost/private/<YourQueueName>
<endpoint name="netMsmqServiceEndpoint"
address="net.msmq://localhost/private/<YourQueueName>"
binding="netMsmqBinding" bindingConfiguration="basicMsmq"
contract="<YourServiceContractName>" />
Back for the testing, after you have created a service client and
added the service reference to your WCF service host. Make a few requests first from your client to make sure the WCF service is working fine.
After that, you can purposely
stop the IIS service for your WCF service, and then use your client to submit a few more requests. At this time, you should expect to see your client is still working fine and the queue is piling up with requests.