Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How HttpContext.Current works on each request in IIS pipeline?

As you know, the HttpContext.Current returns the current context in the application pipeline.
Also this property is static, so logically any changes on that or on its properties should affect the other pipelines.

A static field identifies exactly one storage location. No matter how many instances of a class are created, there is only ever one copy of a static field. More

How IIS handle this to prevent conflict on the other pipelines and every HttpContext.Current be unique on each pipeline?

For example for two users that already logged into the system, the HttpContext.Current.User.Identity.Name gives the username of the user who sent the request to the server.

ASP.NET Pipeline:
enter image description here

like image 629
Bagherani Avatar asked Dec 19 '22 07:12

Bagherani


2 Answers

Current is the property, not field, so it's a static method actually.

This method can return different instances for different threads, and it really does.

If you're developing multithread web application, keep in mind a few things.

  1. Don't use ThreadStaticAttribute. It works in Windows and console applications, but it may not work in web applications, since a single request can be handled by different threads, if you use async, await, and Task<T>.

  2. Use HttpContext.Current.Items instead of ThreadStaticAttribute. These Items are "static" in each HttpContext.

  3. Use SynchronizationContext if you need important settings of HttpContext (regional settings, logged user, and your own HttpContext.Items) after asynchronous calls (if you're not using await).

The reason why you should be careful is a thread pool. It's quite possible that your asynchronous method starts to run in a first thread, continues in a second, and ends in a third. Since each thread has its own copy of the thread static field, you can get unpredictable different values of the field in different locations of your method. SynchronizationContext allows you to return to the initial thread with correct values of regional settings, HttpContext.Items, etc. The await operator does it work for you, so you shouldn't care about context, if you're using the await (thanks to @StephenCleary for the correction).

Now for the thread-static fields. When ASP.NET gets a HTTP request, it creates the new instance of HttpContext with empty HttpContext.Items collection. At the same time ThreadStatic fields are initialized already by previous HTTP request. Therefore f.e. a Singleton class, based on a thread-static field may not work properly. It's important both in synchronous and asynchronous methods of a web application.

like image 163
Mark Shevchenko Avatar answered Jan 30 '23 01:01

Mark Shevchenko


The answer lies in thread-local storage, implemented with ThreadStatic in .NET. The ambient context design pattern is also relevant here.

like image 43
fejesjoco Avatar answered Jan 29 '23 23:01

fejesjoco