tags:

views:

1875

answers:

3

What are differences between CallContext and ThreadStatic?

I've understood that in an ASP.NET environment data stored in CallContext could be persisted throughout the request until it ends while ThreadStatic may or may not work since the request may switch threads. I've also learned that the HttpContext is internally stored using the CallContext.

In a regular application they both seem to persist throughout the same thread call. When isn't this the case?


Edit: In the comments I learned that the call context is an abstraction over a thread static store. The ASP.NET framework explicitly moves the data from one thread to the next that is to handle one request. Other framework that wants to provide thread agility could do the same to for contextual storage.

+6  A: 

Very often a request will use the same thread throughout, but it certainly won't always be the case - ASP.NET exhibits thread agility. There's a really useful discussion about this on the Spring.NET forums. This refers to an in-depth blog article about the matter. I suspect that goes into more details than you'll get here :)

What conflicting information did you see, out of interest?

Jon Skeet
That spring.net thread was in the back of my head when I asked (points for linking). I was particularly looking for details related to the non-web case. Are they essentially the same?
Cristian Libardo
What do you mean by the non-web case? It really depends on what you're doing - if you know you're in a single thread, it's okay to use ThreadStatic.
Jon Skeet
I mean an application not affected by the ASP.NET abstractions. CallContext and ThreadStatic seems to work the same way, i.e. they seem to have the same lifetime. I'm interested in learning the differences between the two.
Cristian Libardo
ThreadStatic is explicitly related to the thread. Any other framework might decide to use thread agility in the same way as ASP.NET. CallContext is a way of taking some context with you when you do.
Jon Skeet
+3  A: 

Items stored as ThreadStatic are available to more than one request. IIS reuses the thread after a request is complete to process subsequent requests. ASP.Net clears down the CallContext after each request.

Martin Brown
It's more than that though - one request can hop across threads, so what you put in a ThreadStatic in one phase not be available in a different one.
Jon Skeet
What is the mechanism that maintains the call context between threads serving the same request?
Cristian Libardo
ASP.NET's thread agility handling, basically. I suspect it's a ThreadStatic internally, and when one thread stops dealing with a request for whatever reason, ASP.NET keeps the request and context together, then resets it in whatever thread picks up the work.
Jon Skeet
Okay, I have learned that the call context is an abstraction over a thread static store and that the ASP.NET framework explicitly moves the data from one thread to the next one handling the request.
Cristian Libardo
@Jon: Is Thread Agility behaviour true for WCF as well? Is it safe to use ThreadStatic in WCF application to store request specific data?
Amitabh
A: 

i am also intereted in the answer about wcf service hosted in iis. i want to create , static class that will hold env. information to be used by the methods called from the main wcf method, without the need to pass it from method to method

may east