Shiraz Bhaiji Shiraz Bhaiji - 1 month ago 9x
ASP.NET (C#) Question

Sharing information within a web service request

We have a solution where we use a WCF Client Message Inspector to read a correlation id (and other values) out of a http header.

We then store this value in the System.ServiceModel.OperationContext.Current.IncomingMessageProperties.

The OperationContext.Current is the execution context of the current thread.

However, based on Jon Skeet's answer the this question: Will a request in IIS run on a single thread?
The request will jump between threads.

This is exactly what we are experiencing, that the correlation id changes during the execution of the request.

Is there somewhere where we can store the values from the http request, so that they can be accessed lower down in the stack?


I've used the System.Web.HttpContext.Current.Items collection to store values that we've picked up in a MessageInspector, example

    public void AfterReceiveReply(ref Message reply, object correlationState)
        var httpResponse =
                   as HttpResponseMessageProperty;

        if (httpResponse != null)
            string cookie = httpResponse.Headers[HttpResponseHeader.SetCookie];

            if (!string.IsNullOrEmpty(cookie))
                System.Web.HttpContext.Current.Items.Add("MyCookies", cookie);

Elsewhere in the stack the values can be picked up from the Items collection using the same key.

Please check this article by Scott Hanselman for more details.