Third-Party TLS, SynchronizationContext, etc. RRS feed

  • Question

  • User-1161032924 posted

    I am currently working on an ASP.NET MVC 4 with WebAPI application which must make use of a third-party unmanaged library.  This library stores and tracks context information internally using thread-local storage which affects other calls to the library.  I am aware of the dangers of using TLS with ASP.NET and don't really want to argue the merits, but rather determine if what I'd like to do is possible at all.

    This library does allow for me to modify the data stored in TLS at any point.  What I would like to do is track that data in HttpContext and at the point that the context is associated with a thread to copy that data into the TLS of that thread.

    With async I think I've managed to get this functioning well enough through wrapping the SynchronizationContext so that callback passed to Send and Post is wrapped in a function that sets and unsets the TLS data.  I'd love to get more granular than that and remove any TLS data when a thread is disassociated from the HttpContext or is returned to the thread pool but I'm not seeing any good interception points short of potentially mucking about with reflection.

    Any ideas?

    Thursday, May 23, 2013 4:23 PM

All replies

  • User-488622176 posted

    Can you please clarify what you mean by "remove any TLS data when a thread is disassociated from the HttpContext" ? In your WebAPI service/app? 

    Friday, May 24, 2013 4:41 AM