Can I rely on CallContext using the Web API?

Is it possible to call CallContext using the whole request when using asp.net web API?

I read a ten-year blog post and I'm not sure if it still applies (on request there ).

Assuming Thread-Agility fires if I set the data in a global filter, is it possible to assume that it will be there, regardless of the request?

+6
source share
1 answer

You lose your CallContext if ASP.Net is switching threads. In the asynchronous model, the asp.net task scheduler will take care of attaching asynchronous calls to the request stream with the same HttpContext, but not necessarily to the same stream.

Example: a request starts, and then you switch to asynchronously waiting for slow I / O before returning - while you wait for this slow I / O, there is no reason why your request stream will sit idle so that it can be used for another request.

ASP.Net is a great exercise in Thread Agility (google it), and there is also a great discussion here: CallContext vs ThreadStatic vs HttpContext

+2
source

All Articles