none
OperationContext.Current.Extensions contamination RRS feed

  • Question

  • Hi,

    I need to save HttpContext during each request to a WCF backend, I have a message inspector which looks like this:

    public class MyMessageInspector : IClientMessageInspector { public object BeforeSendRequest(ref Message request, IClientChannel channel) { OperationContext.Current.Extensions.Add(new HttpContextSaverExtension()); } public void AfterReceiveReply(ref Message reply, object correlationState) { var contextSaver = OperationContext.Current.Extensions.Find<HttpContextSaverExtension>();

    // contextSaver is null sometimes

    OperationContext.Current.Extensions.Remove(contextSaver); } } public class HttpContextSaverExtension : IExtension<OperationContext> { public HttpContext HttpContext { get; set; } public void Attach(OperationContext owner) { HttpContext = HttpContext.Current; } public void Detach(OperationContext owner) { HttpContext = null; } }

    The problem is that sometimes the collection OperationContext.Current.Extensions does not contain my extension object. 

    I use async proxy's methods to call the WCF service.

    Am I doing something wrong? If not then highly appreciate any ideas how to figure out the problem.

    Regards,

    Nick




    Monday, March 4, 2013 4:27 PM

All replies

  • Hi,

    >>The problem is that sometimes the collection OperationContext.Current.Extensions
    does not contain my extension object.

    Do you mean sometimes you can get that object but sometimes not? You may take a look at the discussions here that talks on how to store data for current call.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 5, 2013 10:00 AM
    Moderator
  • Hi Haixia,

    Thanks for reply.

    Yes, the collection sometimes contains the extension I've added and sometimes not.

    I've looked into the discussion and they do a check for null, so they simply avoid the case when the Extensions collection does not contain the extension.

    I believe I cannot use the OperationContext.Current this way as BeforeSendRequest and AfterReceiveReply can be run in different threads for a single request.

    I've changed the code:

    public class MyMessageInspector : IClientMessageInspector { public object BeforeSendRequest(ref Message request, IClientChannel channel) {

    return HttpContext.Current; } public void AfterReceiveReply(ref Message reply, object correlationState) { var httpContext = (HttpContext)correlationState;

    // ...

    } }

    Can I be sure that the HttpContext in AfterReceiveReply method will be the one that I've returned from BeforeSendRequest?

    Regards,

    Nick

    Wednesday, March 13, 2013 9:33 AM
  • Hi, you may try set a breakpoint to check those values.
    Thursday, March 14, 2013 2:56 AM