HttpContext.Current is null in asynch method?

G

Guest

I'm trying call a couple WebRequests asynchronously using the
QueueUserWorkItem, but one thing I noticed is that the HttpContext is null
within the asynchronous call. I was using HttpContext.Current.Server.MapPath
to load an Xslt file to format and return Xml after the webrequest is made.

Is there a way around this?

Here some code...

stateInfo = new StateInfo(manualEvents[0], strUrl);
System.Threading.ThreadPool.QueueUserWorkItem(new
WaitCallback(this.MyWebRequestCallback), stateInfo);

private void MyWebRequestCallback(object state)
{
StateInfo stateInfo = (StateInfo)state;
MakeWebRequestAndTransformXML(stateInfo.Url); <--NULL where this calls
Server.MapPath
stateInfo.manualEvent.Set();
}

class StateInfo
{
public string Url;
public ManualResetEvent manualEvent;

public StateInfo(ManualResetEvent manualEvent, string Url)
{
this.manualEvent = manualEvent;
this.Url = Url;
}
}
 
J

Joerg Jooss

Dave said:
I'm trying call a couple WebRequests asynchronously using the
QueueUserWorkItem, but one thing I noticed is that the HttpContext is
null within the asynchronous call. I was using
HttpContext.Current.Server.MapPath to load an Xslt file to format and
return Xml after the webrequest is made.

Is there a way around this?

Here some code...

stateInfo = new StateInfo(manualEvents[0], strUrl);
System.Threading.ThreadPool.QueueUserWorkItem(new
WaitCallback(this.MyWebRequestCallback), stateInfo);

private void MyWebRequestCallback(object state)
{
StateInfo stateInfo = (StateInfo)state;
MakeWebRequestAndTransformXML(stateInfo.Url); <--NULL where this
calls Server.MapPath
stateInfo.manualEvent.Set();
}

class StateInfo
{
public string Url;
public ManualResetEvent manualEvent;

public StateInfo(ManualResetEvent manualEvent, string Url)
{
this.manualEvent = manualEvent;
this.Url = Url;
}
}

HttpContext is quite likely a thread-local object. You could make the
HttpContext part of the StateInfo, but I'm not sure about HttpContext's
lifecycle and if it can be used across threads.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top