Use an MSMQ Queue as a Message Store
As always, the logical first step in creating a WCF application is defining the contract, as shown in Listing 1
Unlike a contract designed for use with MsmqIntegrationBinding, a contract destined for NetMsmqBinding can contain multiple operations (see Listing 1). However, it is still limited to only one-way operations, as indicated by the presence of the IsOneWay parameter on the OperationContract attribute. The use of the DeliveryRequirements attribute is purely optional, but it will force any implementers of the contract to support queued delivery. Finally, the TransactionFlow attribute indicates that transactions can be flowed to this service operation.
While not required, it is important to understand that by default NetMsmqBinding has an exactlyOnce value of "true." This means that each message is delivered once and only once. It also means that if this attribute is left at its default value, the queue used for holding messages must be transactional.
After you have defined the contract, you can create a concrete implementation as follows:
[ServiceBehavior(TransactionAutoCompleteOnSessionClose = false, TransactionIsolationLevel = IsolationLevel.Unspecified)]
public class QueueService : ISendMessage
[OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
public void SubmitMessage(ApplicationMessage message)
//TODO Do some actual work with the message
While there are some transaction-specific attributes within the service implementation, there is nothing to indicate the usage of MSMQ. It is completely transparent to the actual implementation.
Finally, you need the configuration file (see Listing 2). One interesting thing to note in Listing 2 is the baseAddress of the NetMsmqBinding endpoint: net.msmq://localhost/private/WcfMsMqSampleService/QueueService.svc. It makes use of the net.msmq protocol, which needs to be specifically enabled within IIS. (The binding configuration for NetMsmqBinding will be discussed later.)
Before you can debug the service or make it available to clients, you must configure IIS and MSMQ to work together. First, you must create a queue. When using NetMsmqBinding, the name of the queue must match the URI of the svc file. In this case, you would need to create a queue named "WcfMsMqSampleService/QueueService.svc." Assuming exactlyOnce has been left at its default value of "true," the queue must be transactional. You could use a non-transactional queue, but you would have to set the exactlyOnce attribute to false in the binding configuration. In addition, you would need to give the process identity used by IIS permission to the queue (see Figure 1).
|Figure 1. Securing the Queue: Use the computer management console to set appropriate permissions on the queue.|
You must enable the net.msmq protocol for the IIS site that will host the WCF service. You can accomplish this via the following two command-line operations:
C:\Windows\System32\inetsrv>appcmd set site "Default Web Site" -+bindings.[proto
C:\Windows\System32\inetsrv>appcmd set app "Default Web Site/WcfMsMqSampleService
Notice in the second command that both net.msmq and HTTP are listed. Omitting HTTP would make the site unreachable via that protocol. Also be sure to replace "WcfMsMqSampleService" with whatever name you are using.
Finally, you need to start the NetMsmqActivator service. The identity used by this service will also need permissions to the appropriate queue.