HttpContext.Current是异步回调空 [英] HttpContext.Current is null in an asynchronous Callback
问题描述
试图访问的 HttpContext.Current
在方法调用回,所以我可以修改会话
变量,我收到了异常 HttpContext.Current
是空
。这个回调方法异步发射,当 _anAgent
对象触发它。
Trying to access the HttpContext.Current
in a method call back so can I modify a Session
variable, however I receive the exception that HttpContext.Current
is null
. The callback method is fired asynchronously, when the _anAgent
object triggers it.
我仍然不能确定,要解决这个的观察后,<一个href="http://stackoverflow.com/questions/23242321/httpcontext-current-null-when-making-a-function-asynchronous-in-mvc4">similar <一href="http://stackoverflow.com/questions/2540370/wcf-and-asp-net-server-execute-throwing-object-reference-not-set-to-an-instanc">questions在左右。的
I'm still unsure of the solution to this after viewing similar questions on SO.
我的code的简化版本看起来像这样:
A simplified version of my code looks like so:
public partial class Index : System.Web.UI.Page
protected void Page_Load()
{
// aCallback is an Action<string>, triggered when a callback is received
_anAgent = new WorkAgent(...,
aCallback: Callback);
...
HttpContext.Current.Session["str_var"] = _someStrVariable;
}
protected void SendData() // Called on button click
{
...
var some_str_variable = HttpContext.Current.Session["str_var"];
// The agent sends a message to another server and waits for a call back
// which triggers a method, asynchronously.
_anAgent.DispatchMessage(some_str_variable, some_string_event)
}
// This method is triggered by the _webAgent
protected void Callback(string aStr)
{
// ** This culprit throws the null exception **
HttpContext.Current.Session["str_var"] = aStr;
}
[WebMethod(EnableSession = true)]
public static string GetSessionVar()
{
return HttpContext.Current.Session["str_var"]
}
}
不知道是否必要,但我的 WorkAgent
类看起来像这样:
public class WorkAgent
{
public Action<string> OnCallbackReceived { get; private set; }
public WorkAgent(...,
Action<string> aCallback = null)
{
...
OnCallbackReceived = aCallback;
}
...
// This method is triggered when a response is received from another server
public BackendReceived(...)
{
...
OnCallbackReceived(some_string);
}
}
在code,会发生什么:
点击一个按钮调用送出数据()
的方法,这里面的 _webAgent
调度信息到另一台服务器,并等待答复(在此期间,用户仍然可以使用此页互动,指的是相同的SessionID
)。一旦收到它调用 BackendReceived()
方法,该方法,早在.aspx.cs页面调用回调()
方法。
What happens in the code:
Clicking a button calls the SendData()
method, inside this the _webAgent
dispatches a message to another server and waits for reply (in the mean time the user can still interact with this page and refer to the same SessionID
). Once received it calls the BackendReceived()
method which, back in the .aspx.cs page calls the Callback()
method.
问:
当 WorkAgent
触发回调()
方法尝试访问 HttpContext.Current
是空
。这是为什么时,如果我继续,忽略了异常,我仍然可以得到相同的情况下的SessionID
和会话
使用ajax的变量返回 GetSessionVar()
方法。
Question:
When the WorkAgent
triggers the Callback()
method it tries to access HttpContext.Current
which is null
. Why is that the case when if I continue on, ignoring the exception, I can still obtain the same SessionID
and the Session
variable using the ajax returned GetSessionVar()
method.
我应该使 aspNetCompatibilityEnabled 设置?
我应该创造某种的<一个href="http://blogs.msdn.com/b/tmarq/archive/2010/04/14/performing-asynchronous-work-or-tasks-in-asp-net-applications.aspx">asynchronous模块处理程序?
难道这涉及到<一个href="http://msdn.microsoft.com/en-us/library/vstudio/bb515251%28v=vs.100%29.aspx">Integrated/Classic模式?
推荐答案
请参阅为什么会话变量为空的解释下面的文章,以及可能的变通
Please see the following article for an explanation on why the Session variable is null, and possible work arounds
的http://adventuresdotnet.blogspot.com/2010/10/httpcontextcurrent-and-threads-with.html
这引述从文章;
当前的HttpContext实际上是在线程本地存储,这就解释了为什么子线程没有访问它
和作为提出解决该作者说
And as a proposed work around the author says
引用传递给它的子线程。中提及的HttpContext在回调方法的状态对象,然后你可以将其存储到HttpContext.Current上线
这篇关于HttpContext.Current是异步回调空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!