WCF Msmq problem reading messages using netMsmqBinding

1.3k views Asked by At

I have a WCF service using netMsmqBinding that I am using to add messages of Msmq<string> to a queue. The messages are added fine and I can see them in the queue via the computer management console.

I have another WCF service that is trying to retrieve the messages from the queue, this is where I'm having a problem. My method in my service is getting called whenever a message is added to the queue (that bit is working fine) but the Msmq<string> message seems to have all null values.

I'm not sure how I can get the message from that Msmq<string>? Here is my service details... any help appreciated..

[ServiceContract]
[ServiceKnownType(typeof(Msmq<string>))]
public interface IMessageListener
{
   [OperationContract(IsOneWay = true, Action = "*")]
    void ListenForMessage(Msmq<string> msg);
}

public class MessageListener : IMessageListener
{
    [OperationBehavior(TransactionScopeRequired = false, TransactionAutoComplete = true)]
    public void ListenForMessage(MsmqMessage<string> msg)
    {
         //this gets called and seems to remove the message from the queue, but message attributes are all null
    }
}
1

There are 1 answers

0
marc_s On

I think you're not quite "getting" the idea of WCF over MSMQ.

When using WCF with the netMsmqBinding, the whole idea is that you don't need to deal with the details of MSMQ - let the WCF runtime handle that!

So basically, your approach should be as with any WCF service:

  • define your service contract and its methods (operation contract)
  • define your data structures as a [DataContract] and use those in your service methods
  • implement the service

So your service should be something like:

[DataContract]
public class Customer
{
   [DataMember]
   public int ID { get; set; }

   [DataMember]
   public string Name { get; set; }

   ...
}

[ServiceContract]
public interface ICustomerService
{
    [OperationContract(IsOneWay=true)]
    void SaveCustomer(Customer myCustomer)

    [OperationContract(IsOneWay=true)]
    void CreateCustomer(int ID, string name);
}

You should have a data contract to describe your data - just your data, no MSMQ details needed here! Then you should have one set of service methods that will deal with the Customer object - you can put it into the queue for storing, create a new one etc.

You would then implement the client and the server side for this service contract, and the WCF runtime will handle all the details of MSMQ transport, putting the payload (the Customer object) into a MSMQ message and getting it back out again and so on... you don't have to deal with that, really.