Thread.CurrentThread store custom data such as userid
In .net 4, Is it possible to store custom value such as userid in Thread.CurrentThread object?
I am trying to store userid for wcf client on the client side of MessageInspector in AfterReceiveRequest method as follows:
request.Headers.Add(MessageHeader.CreateHeader("userid", string.Empty, userid));
On the server side I would like to retrieve it in a Thread.CurrentThread in the method AfterReceiveRequest.
Is this possible?
ThreadStatic is not recommended because WCF will randomly use threads from the thread pool and ThreadStatic variables will not be reinitialized when the threads are recycled. Also if you have a session with multiple WCF calls, the same service instance is likely to switch threads from call to call.
You should create an OperationContext extension.