Marwan
Marwan

Reputation: 2402

HttpContext.Current.Session is null inside the aync Task while try to Make a WebSocket Request

I'm Creating a WebSocket Request From The Client To the Server Using Generic Handler

HttpContext.Current.Session is null inside the Task Method

My Server Code is

<%@ 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;
        }
    }

}

My Web Config

<?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>

Upvotes: 1

Views: 1485

Answers (1)

vtortola
vtortola

Reputation: 35963

I think the problem is related with this: The cross-thread usage of "HttpContext.Current" property and related things

A workaround may be replacing the SyncDataTask signature for:

public async Task SyncDataTask(WebSocketContext context, HttpSessionState session)

And then replace this call:

context.AcceptWebSocketRequest(SyncDataTask);

for this one:

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

Upvotes: 1

Related Questions