Is it safe to store an ObjectContext in a static thread variable in ASP.NET?

I read what I should store ObjectContextin HttpContext.Currentto split mine ObjectContextinto different services / repositories called in the request. I am wondering if it can be used ObjectContextwith an attribute [ThreadStatic]in a class variable static. Is this a safe thing? Each request is processed in its own thread?

+3
source share
2 answers

No, there can be several requests in one thread, and, more importantly, one request can be processed by several threads. This is called thread flexibility , and you run into problems when storing stuff in stream-static variables instead of context: when ASP.NET moves from one stream to another during the same request, the HttpContext is still available, but the static stream variable is not .

Some links with additional information:

+7
source

, , . , , , - . , . , .

+3

Source: https://habr.com/ru/post/1723695/


All Articles