After run the Part A and click the button1, I get the error
Object reference not set to an instance of an object.
I think the error is caused by the function HttpContext.Current.Server.MapPath
Is it seems hat HttpContext.Current can't use in asynchronous function?
How can I get a server physical path in a asynchronous function? Many Thanks!
=============================Part A=======================
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpContext.Current is only available on threads executing http requests. When you invoke your method with BeginInvoke, your method is executed on another thread from the thread pool. This thread (while executing your method) is not associated with any
HttpContexts (BeginInvoke does not propagate it).
Moreover, if you don't wait for your method to complete in your page code, it is possible that your catchemailcallback will start executing when current context is already disposed.
If you transfer execution to another thread via async delegate call, I would suggest passing all prepared paths to this method directly.
mycwcgr
Participant
1646 Points
924 Posts
Why is HttpContext.Current.Server.MapPath invalid in a asynchronous function?
Jul 08, 2007 10:54 PM|LINK
After run the Part A and click the button1, I get the error
Object reference not set to an instance of an object.
I think the error is caused by the function HttpContext.Current.Server.MapPath
Is it seems hat HttpContext.Current can't use in asynchronous function?
How can I get a server physical path in a asynchronous function? Many Thanks!
=============================Part A=======================
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
AsynCatchEmailToDB();
}
private delegate void CatchEmailCallBack();
public static void AsynCatchEmailToDB()
{
CatchEmailCallBack myCatchEmailCallBack = new CatchEmailCallBack(Sync);
myCatchEmailCallBack.BeginInvoke(null, null);
}
private static void Sync()
{
string path = HttpContext.Current.Server.MapPath(@"~/Temp");
}
}
=============================Part A=======================
REA_ANDREW
Member
472 Points
119 Posts
Re: Why is HttpContext.Current.Server.MapPath invalid in a asynchronous function?
Jul 08, 2007 11:15 PM|LINK
try removing static from both methods.
public void AsynCatchEmailToDB()
{
CatchEmailCallBack myCatchEmailCallBack = new CatchEmailCallBack(Sync);
myCatchEmailCallBack.BeginInvoke(null, null);
}
private void Sync()
{
string path = HttpContext.Current.Server.MapPath(@"~/Temp");
}
Andrew
Blog : andrewrea.co.uk
ysw
Contributor
2784 Points
515 Posts
Re: Why is HttpContext.Current.Server.MapPath invalid in a asynchronous function?
Jul 08, 2007 11:21 PM|LINK
Hi
HttpContext.Current is only available on threads executing http requests. When you invoke your method with BeginInvoke, your method is executed on another thread from the thread pool. This thread (while executing your method) is not associated with any HttpContexts (BeginInvoke does not propagate it).
Moreover, if you don't wait for your method to complete in your page code, it is possible that your catchemailcallback will start executing when current context is already disposed.
If you transfer execution to another thread via async delegate call, I would suggest passing all prepared paths to this method directly.
-yuriy
http://couldbedone.blogspot.com
hnbi4u
Member
4 Points
2 Posts
Re: Why is HttpContext.Current.Server.MapPath invalid in a asynchronous function?
Jul 11, 2008 09:18 AM|LINK
hello there,
i am getting {"Failed to map the path '/iCal/'."} exception when i tried using this statement
string
path = HttpContext.Current.Server.MapPath(@"\iCal\");any help???