Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I rely on CallContext using Web API?

Can CallContext be relied upon, through-out the whole request, when using asp.net Web API?

I have read the decade-old blog post and I'm not sure it still applies (as asked there).

Assuming Thread-Agility kicks in, if I set data in a global filter, is it safe to assume it will be there through-out the request?

like image 588
Berlo Avatar asked Mar 16 '16 20:03

Berlo


1 Answers

You lose your CallContext if ASP.Net switches threads. In an asynchronous model, the asp.net task scheduler will take care of joining async calls back to a request thread with the same HttpContext, but not necessarily the same thread.

Example: A request starts and then you go off to asynchronously wait for some slow IO before returning - while you're waiting for that slow IO there's no reason for your request thread to be sitting around doing nothing so it may get used for another request.

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

like image 69
caesay Avatar answered Oct 16 '22 22:10

caesay