Question

Edited my Question with more info below regarding the "another thread" after usr pointed out that my question wasn't clear.

How can I pass data using the ThreadLocal<T> across different classes?

Basically, I need to pass the LogContext info from a class in the Webservice to a TraceExtension:SoapExtension class (The TraceExtension is to log request/responses of another WebService that this WebService calls).

Since the functionality runs on another thread and not the thread that initiated the request, I cannot get it from a ServiceContext:IExtension <InstanceContext> , that I would normally use for such a thing.

I have a working implementation using the Thread.GetNamedDataSlot() way, but in performance testing we found there is a memory leak problem. (Yes, I have called the Thread.FreeNamedDataSlot() and the problem persisted). This lead to MS post http://support.microsoft.com/kb/2540745 that a leak problem may exist, but in our current timeframe for the release it will not be possible to deploy the fix on all the production servers. The work around suggested is to use the System.Threading.ThreadLocal class.

All the resources/examples I have found so far have instances of ThreadLocal in the same class and of Parallelism and PLinq. An example of initializing/setting data in one class and retrieving it from another will be very helpful. Any other way of passing data to the TraceExtension : SoapExtension will also be equally helpful.

We are a bit tight on schedule and any help will be much appreciated.

Edit with more info:

The thread of our Webservice on which the request comes creates a new Thread on which we run some background processing, while returning the necessary response. The new Thread invokes calls to other backend web services. We also have a TraceExtension class to trace the Request/Response to the backend web serivces. We pass the LogContext info from the original Thread to the method on the new Thread using a ContextInfo : ISynchronizeInvoke.

The problem is getting the LogContext object From the class invoking the backend webservice call To the TraceExtension class (which runs on the new Thread and not the original Thread on which the client request came having the ServiceContext:IExtension<InstanceContext> which does hold the LogContext)

Was it helpful?

Solution

You want the ThreadStaticAttribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top