Asynchronous HTTP Handler and using HttpContext in a background thread? Asynchronous HTTP Handler and using HttpContext in a background thread? multithreading multithreading

Asynchronous HTTP Handler and using HttpContext in a background thread?


The HttpContext and all its properties are not thread safe, so you should be very careful. Reading data simultaneously from different threads can do no harm, but you have to be sure that there are no write operations happening. Still, even when you're sure the Items property is not changed, I'd prefer to make a copy and supply that to the background threads. That communicates clearly the intend and saves you from having discussions during code reviews or people reevaluating whether this code is really thread safe.

Now about using HttpContext in asynchronous requests; Accessing the HttpContext from different threads would be dangerous, but in this case ASP.NET controls the threads and makes sure that only one thread is processing the request. It would be different when you, for instance, spin up a new thread manually (by using the thread pool or new Thread()) and supplying HttpContext to that thread, while continuing execution.