HttpContext.Current.Session 在 aync Task 中为空,同时尝试发出 WebSocket



我正在使用通用处理程序创建从客户端到服务器的 WebSocket 请求

HttpContext.Current.Session在任务方法中为空

我的服务器代码是

<%@ 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.Current"属性和相关事物的跨线程用法

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

public async Task SyncDataTask(WebSocketContext context, HttpSessionState session)

然后替换此调用:

context.AcceptWebSocketRequest(SyncDataTask);

对于这个:

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

相关内容

  • 没有找到相关文章

最新更新