Search code examples
asp.nethttpcontext

How do I retrieve the HttpContext properties when it returns null?


I am doing some asynchronous work on a separate thread using:

ThreadPool.QueueUserWorkItem()

and in this separate thread, I need to call HttpContext.Current so that I can access:

HttpContext.Current.Cache  
HttpContext.Current.Server  
HttpContext.Current.Request  

However, HttpContext.Current is null when I create this separate thread.

Question

How do I create a new thread so that HttpContext.Current is not null? Or is there another way I can access the Cache, Server, and Request objects?


Solution

  • I'd try not to hold a reference to an object that depends on the ASP.NET stack like the HttpContext. If you need to do some work in a different thread, it's because you don't want to wait in the ASP.NET one till your task is finished. And maybe the Request/Context/Session is terminated while your thread is not.

    You should pass an object with the data needed for your thread.