javascript - javascript HttpContext.Current.Session 是空 inside,而aync任务试图生成 web socket请求

  显示原文与译文双语对照的内容

我正在使用通用处理器从客户机向服务器创建一个 web socket请求

HttpContext.Current.Session 为空 inside 任务方法

我的服务器代码是


<%@ WebHandler Language="C#" Class="SyncDataHandler" %>



using System;


using System.Web;


using System.Net.WebSockets;


using System.Threading;


using System.Threading.Tasks;


using System.Web.Script.Serialization;


using System.Text;


using System.Web.SessionState;



public class SyncDataHandler : IHttpHandler, IRequiresSessionState


{



 public void ProcessRequest(HttpContext context)


 {


 if (context.IsWebSocketRequest)


 {


 context.AcceptWebSocketRequest(SyncDataTask);


 }


 }



 public static string getLastComment()


 {


 return new JavaScriptSerializer().Serialize(DH_Details.GetLastComment());


 }



 public async Task SyncDataTask(WebSocketContext context)


 {


 WebSocket socket = context.WebSocket;



 var x = HttpContext.Current.Session;//x is null why???



 while (true)


 {


 System.Threading.Thread.Sleep(1000);


 ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);



//If the socket is still open, echo the message back to the client


 if (socket.State == WebSocketState.Open)


 {



 string SyncedObject = getLastComment();



 buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(SyncedObject));



//Asynchronously send a message to the client


 await socket.SendAsync(buffer, WebSocketMessageType.Text,


 true, CancellationToken.None);


 }


 else { break; }


 }


 }



 public bool IsReusable


 {


 get


 {


 return false;


 }


 }



}



我的网站配置


<?xml version="1.0"?>


<!--


 For more information on how to configure your ASP.NET application, please visit


 http://go.microsoft.com/fwlink/?LinkId=169433


 -->


<configuration>


 <system.web>


 <compilation debug="true" targetFramework="4.5">


 <assemblies>


 <add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>


 <add assembly="Microsoft.TeamFoundation.WorkItemTracking.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>


 <add assembly="Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>


 </assemblies>


 </compilation>


 <httpRuntime targetFramework="4.5"/>


 </system.web>


 <appSettings>


 <add key="TFS_URL" value="http://192.168.1.10:8080/tfs/"/>


 <add key="serverPath" value="D:/"/>


 <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>


 <add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms"/>


 </appSettings>


</configuration>



时间: 原作者:

我认为问题与这里有关: :"当前的httpcontext"属性的交叉线程使用和相关事物。

解决方法可能是替换以下 SyncDataTask 签名:


public async Task SyncDataTask(WebSocketContext context, HttpSessionState session)



然后替换这里调用:


context.AcceptWebSocketRequest(SyncDataTask);



对于这个:


context.AcceptWebSocketRequest(wsc => SyncDataTask(wsc, context.Session));



原作者:
...