22

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?

Community
  • 1
  • 1
makstaks
  • 2,111
  • 4
  • 24
  • 38

6 Answers6

37

You can access the ASP.NET cache with HttpRuntime.Cache even when you don't have a HttpContext, but unfortunately you cannot access Server or Request.

If you think about it, this make sense - you are not serving any page so you don't have a request.

Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
14

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.

Ed Schwehm
  • 2,163
  • 4
  • 32
  • 55
MatthieuGD
  • 4,552
  • 2
  • 30
  • 29
3

1- Add bottom code in <system.serviceModel> in Web.config file:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 

2- Add bottom code after NameSpace in web service file:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

3- Rebuild web part project. Done!

reference

TRR
  • 1,637
  • 2
  • 26
  • 43
Ali Rasouli
  • 1,705
  • 18
  • 25
1

There is a thread pool implementation here that provides propagation of the calling thread's HTTP context. I haven't used it yet but I plan to.

Andy Johnson
  • 7,938
  • 4
  • 33
  • 50
1

For HttpContext.Server services you can use HttpServerUtility class. For the Cache you can use HttpRuntime.Cache, as it has been said above. For the request object you can pass the data from the Request to the thread when it is created. Things like Request.QueryString or Request.Form... or whatever.

Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
  • 3
    To initiate an instance of HttpServerUtility outside of a web request, use: new HttpApplication().Server (via http://britishinside.com/archive/2005/08/20/HttpServerUtility.aspx) Some features won't work though. For example, Server.MapPath doesn't. But as a workaround use System.Web.Hosting.HostingEnvironment.MapPath instead. – Jon Adams Sep 19 '09 at 01:47
0

If the separate thread is trying to access those objects, then yes they will be null. Those objects are scoped at the thread level. If you want to use them in a new thread you will have to pass them into the method/class where you need them.

Typically ASP.Net doesn't allow you to spawn new threads... Here is a post on the subject.

Here is a nice write up on threading in ASP.NET from MSDN.

Chuck Conway
  • 16,287
  • 11
  • 58
  • 101
  • 1
    The post on not creating threads in ASP.Net says nothing of the sort. It simply states you shouldn't use the ThreadPool to create your threads, and that you should manager them yourself. – Kibbee Feb 09 '09 at 18:56