This is only safe if you have full control over the flow. This means that you must create a thread, and the thread should end after completion of work. If you use a thread pool to execute a request, you should not use a context for the thread. A thread pool is used, for example, for threads serving ASP.NET, ASP.NET MVC, or WCF requests. The context is not thread safe, so do not pass it among threads.
To save something in the stream, use the static
variable and mark it with ThreadStaticAttribute
.
source share