NetMsmqIntegrationBinding — No Data Being Passed

, ,

Recently, I encountered a strange problem with a simple WCF NetMsmqIntegrationBinding service. The data objects passed into my service contained no data! When a message was received, the service would create an instance of the appropriate DataContract class but then would not populate the just-constructed object with the data contained in the WCF message (i.e. the data object’s fields were all left at their default values—null, 0, empty string, etc.).

I’d hand-code the WCF client (vs. using Add Service Reference), so I knew that its data contract matched the one on the service. Well, it matched except that it was in a different C# namespace. When I’d copied the data contract code from service to client, I’d changed its namespace to match the namespace I was using for the rest of the client. Here lay the problem.

A WCF NetMsmqIntegrationBinding client encodes a namespace along with the data contract’s data in the message it delivers to the MSMQ queue. When the service retrieves a message from the queue, it can match the message to the appropriate service method and can figure out what data objects to initialize without looking at the namespace but it won’t copy data from the message into the data object unless the namespaces match.

By default, this web services data contract namespace is generated from the data contract’s C# namespace. In this situation, the client and service data contracts were in different C# namespaces and so had different web services data contract namespaces. Thus, the problem.

One way to remedy this is to make sure that both client and service contracts have the same C# namespace. This isn’t always desirable. Another option is to explicitly specify the web services data contract namespace using the DataContract attribute. The client contract’s namespace property could be explicitly set to match the auto-generated namespace name of the service (or vise versa). A better approach would be to set a non-C#-namespace-specific namespace on both client and service contracts.

[DataContract(Namespace = "http://mycompany/DataContracts/Entity")]
public class Entity
	public string Name { get; set; }

	public int Age { get; set; }

Interestingly, data contract namespace matching is not necessary with MsmqIntegrationBinding services for data contract namespace information is not sent across the wire when that binding type is used.

Leave a Reply

Your email address will not be published. Required fields are marked *