How to configure WCF for MSMQ

Hello everybody,

today I want to document how to configure WCF for usage with MSMQ.

Of course I suppose you know how to add in windows features MSMQ at each instance where WCF service will work.

So, first of all, let's see how to configure basic interface:

[ServiceContract(SessionMode = SessionMode.Required)]
    public interface IService1
    {
        [OperationContract(IsOneWay = true)]
        void GetData(int value);

        [OperationContract(IsOneWay = true)]
        void GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }

Take not on two features of each method: 

  1. Each method has IsOneWay set to true in OperationContract
  2. Each method returns void. 

Both of those limiations is caused by MSMQ architecture. MSMQ reliably delivers messages, but doesn't tell you when this message is delivered. That's why you need to configure it in the mentioned way.

But that is not the end of the story. For MSMQ you'll need also to add few more attributes to class implementation:

[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void GetData(int value)
{
  //some code
}

Then in your endpoint configuration you can write something like this:

<endpoint address="net.msmq://localhost/public/msmqService"
       bindingConfiguration="NetMsmqBinding"
       binding="netMsmqBinding"
       contract="WcfServiceServer.IService1">
</endpoint>

Hope it can help somebody to save his day.

No Comments

Add a Comment