Поделиться через


Correlating Message Identifiers

I'm trying to send a correlated exchange of messages using the MessageId to perform the correlation. The MessageId appears on the first message but it looks like I have to copy it around manually because it's not on the reply messages. Why isn't this working?

Let's look at a simple message exchange using MessageId to see what is happening here. I'll use the WSHttpBinding to trigger the addition of a MessageId and a simple service contract with one operation. The operation has a request-reply exchange pattern over HTTP so we expect simple correlation between the request and reply messages.

 [ServiceContract]
public interface Service
{
   [OperationContract]
   string Operation(string parameter);
}

Inside the implementation for my operation, I'll print out the MessageId that's in OperationContext.Current.IncomingMessageHeaders. On the client side, I'll construct an OperationContextScope so that I get access to that same header on the return message.

 using (new OperationContextScope((IContextChannel)client.InnerChannel)) {
   client.Operation(parameter);
   Console.WriteLine("id: " + OperationContext.Current.IncomingMessageHeaders.MessageId);
}

As the questioner described, I get a MessageId header on the server that looks like urn:uuid:67a1b220-aaf2-4cbb-9273-92fb2af1d035 but I don't get any MessageId back in the response.

The MessageId header only applies to the first message in the sequence. Subsequent messages are sent to the ReplyTo address of the endpoint and with the RelatesTo header set to the original MessageId. Let's put a second debug statement on the client and server.

 Console.WriteLine("re: " + OperationContext.Current.IncomingMessageHeaders.RelatesTo);

We see that on the first message, the RelatesTo header is not set. On the response message, the RelatesTo header is set to my original MessageId. The solution to the question is that the automatic handling works by linking the MessageId header to RelatesTo in successive messages. You should be looking at the RelatesTo header for your correlation.

Next time: Which Client Credential Does TransportWithMessageCredential Use?

Comments

  • Anonymous
    October 09, 2006
    This article series summarizes the answers to WCF questions I've seen in the last few months. There are

  • Anonymous
    February 19, 2007
    I haven't forgotten about the goal to put together a table of contents for all of these articles. The