WebSocketException: "I/O operation has been aborted"
<p>Trying to implement a basic WebSockets channel with a JavScript client and an ASP.NET server. The client code goes:</p>

<pre><code>function Connect()
{
    var ws = new WebSocket("ws://" + window.location.host + "/callback.aspx");
    ws.onmessage = function (a) { alert("Message"); };
    ws.onopen = function (a) { alert("Open"); };
    ws.onerror = function (a) { alert("Error"); };
}
</code></pre>

<p>Callback.aspx is a basic ASP.NET web page, and it goes:</p>

<pre><code>protected void Page_Load(object sender, EventArgs e)
{
    if (HttpContext.Current.IsWebSocketRequest)
    {
        HttpContext.Current.AcceptWebSocketRequest(SocketProc);
    }
}

private static async Task SocketProc(AspNetWebSocketContext a)
{
    try
    {
        ArraySegment&lt;byte&gt; Buffer = new ArraySegment&lt;byte&gt;(new Byte[1024]);
        while (true)
        {
            WebSocketReceiveResult res = await a.WebSocket.ReceiveAsync(Buffer, CancellationToken.None);
            if (a.WebSocket.State != WebSocketState.Open)
                return;
        }
    }
    catch (Exception exc)
    {
        Debug.WriteLine(exc);
    }
}
</code></pre>

<p>When I try to connect from the client, the <code>Page_Load</code> is invoked, then <code>SocketProc</code> is invoked, but then <code>ReceiveAsync</code> immediately throws the following exception:</p>

<pre><code>System.Net.WebSockets.WebSocketException (0x800703E3): The I/O operation has been aborted because of either a thread exit or an application request
at System.Web.WebSockets.WebSocketPipe.&lt;&gt;c__DisplayClass9_0.&lt;ReadFragmentAsync&gt;b__0(Int32 hrError, Int32 cbIO, Boolean fUtf8Encoded, Boolean fFinalFragment, Boolean fClose)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.WebSockets.AspNetWebSocket.&lt;DoWork&gt;d__45`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.WebSockets.AspNetWebSocket.&lt;&gt;c__DisplayClass36_0.&lt;&lt;ReceiveAsyncImpl&gt;b__0&gt;d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at CleanProxy.callback.&lt;SocketProc&gt;d__2.MoveNext() in C:\dev\CleanModel\CleanProxy\callback.aspx.cs:line 37
</code></pre>

<p>On the client, OnOpen fires, then OnError.</p>

<p>If I catch the exception immediately around <code>ReceiveAsync</code>, the WebSocket object comes out of it closed.</p>

<p>What am I missing here? Testing on Windows Server 2016, both in IIS Express and IIS proper.</p>

<p>EDIT: I've been testing with built-in IE, but I've now tried with Chrome and got a different error. This time, there's no exception, <code>ReceiveAsync</code> returns an object with MessageType=Close, CloseStatus=ProtocolError. On the client, there's an error in Chrome console:</p>

<pre><code>WebSocket connection to 'ws://localhost:53329/callback.aspx' failed: Invalid frame header
</code></pre>

<p>EDIT2: it somehow worked with an HTTP handler instead of an ASPX page. Freaky. There's no requirement to use classic ASP.NET, so this'll do for now, but the issue will probably remain unsolved.</p>