WebSocketException: "I/O operation has been aborted"

1

Trying to implement a basic WebSockets channel with a JavScript client and an ASP.NET server. The client code goes:

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"); };
}

Callback.aspx is a basic ASP.NET web page, and it goes:

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<byte> Buffer = new ArraySegment<byte>(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);
    }
}

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

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.<>c__DisplayClass9_0.<ReadFragmentAsync>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.<DoWork>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.<>c__DisplayClass36_0.<<ReceiveAsyncImpl>b__0>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.<SocketProc>d__2.MoveNext() in C:\dev\CleanModel\CleanProxy\callback.aspx.cs:line 37

On the client, OnOpen fires, then OnError.

If I catch the exception immediately around ReceiveAsync, the WebSocket object comes out of it closed.

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

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, ReceiveAsync returns an object with MessageType=Close, CloseStatus=ProtocolError. On the client, there's an error in Chrome console:

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

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.

javascript
c#
asp.net
iis
websocket
asked on Stack Overflow Nov 17, 2016 by Seva Alekseyev • edited Nov 17, 2016 by Seva Alekseyev

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0