Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpContext回调时为空,XSockets回调_C#_Asp.net_Httpcontext - Fatal编程技术网

C# HttpContext回调时为空,XSockets回调

C# HttpContext回调时为空,XSockets回调,c#,asp.net,httpcontext,C#,Asp.net,Httpcontext,我正在使用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 _th

我正在使用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()

现在,在相同的
Default.aspx.cs
文件中定义了
PromptCallback()

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
}
为什么在回调时仍然可以访问
\u theNumber
,但是HttpContext在那时是空的

(我认为回调的生存期可能比
HttpContext
的生存期长,但是为什么
会话仍然可以访问
/
\u theNumber
呢?)


我读过类似的问题(,)/文章(,),但仍然不确定如何解决这个问题。

使用的浏览器不支持WebSocket吗,因为您使用的是服务器端的通信而不是JavaScript

您现在可能可以使用HttpContext,因为对XSockets的调用(在版本3.0.6中)总是异步的

使用XSockets 4.0(目前处于预发布阶段),您将能够进行同步通信并等待响应。。然后将其发送回响应中。搜索此页面以查找“同步向调用者返回数据” C#和JS都有示例


仍然不理解用例,但我确信您有一个很好的理由来解释这个落后的东西:)

+1 for:但是我确信您有一个很好的理由来解释这个落后的东西:)