【发布时间】:2014-08-05 06:22:41
【问题描述】:
我正在使用 XSockets 在 ASP.NET 网站和某些服务器之间进行通信。当我通过 XSockets 收到来自服务器的回调时,我想使用 Response() 通知 ASP.NET 网站的前端。
我的Default.aspx.cs 主页面如下所示:
// I want to access all these variables in the callback method
public HttpContext ctx;
public HttpSessionState session;
public int _theNumber;
public static CommHandler _commHandler
private void Page_Load(object sender, System.EventArgs e)
{
_theNumber = 4; // Random number
ctx = HttpContext.Current;
_commHandler = new CommHandler( ...
aPromptCallback: PromptCallback
...) //The callback, amongst othr parameters
session = HttpContext.Current.Session; //Try to access this in callback
HttpContext.Current.Session["SomeClass"] = _someObject;
}
CommHandler基本上使用 XSockets 将数据发送到某个服务器,并期望(异步)回复一些数据。
在同一个Default.aspx.cs 中,我有一个按钮单击功能,定义如下:
protected void Deploy(object sender, EventArgs e)
{
_commHandler.SendData()
}
CommHandler 使用 XSockets Send() 方法来发送数据。当 CommHandler 接收到数据时,会触发它内部的回调,调用 PromptCallback()
现在PromptCallback() 在同一个Default.aspx.cs 文件中定义如下:
protected void PromptCallback(string aStr)
{
var v = _theNumber; // This works as expected, the _theNumber is 4
var j = (SomeClass)session["SomeClass"] // I can still access the session..
ctx.Response.Write(aStr); // This does not work, amazingly ctx is now null
//HttpContext.Current.Response.Write(aStr); // This does not work either
}
为什么在回调的时候_theNumber还是可以访问到,但是此时HttpContext为null?
(我知道回调的生命周期可能比HttpContext 的生命周期更长,但为什么session/_theNumber 仍然可以访问?)
【问题讨论】:
标签: c# asp.net httpcontext