From c213737ffd41b616ae3071d22b35e06ba4cf0055 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Wed, 10 Dec 2014 18:21:53 -0800 Subject: [PATCH] Revert "Stop processing messages before ending the WebSocketFunc Task" This reverts commit 654bfb4198c7f20c49c068335c29d47c5011cb97. --- .../Owin/OwinWebSocketHandler.cs | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.AspNet.SignalR.Core/Owin/OwinWebSocketHandler.cs b/src/Microsoft.AspNet.SignalR.Core/Owin/OwinWebSocketHandler.cs index 1e67073c8a..7ff5ea2c2a 100644 --- a/src/Microsoft.AspNet.SignalR.Core/Owin/OwinWebSocketHandler.cs +++ b/src/Microsoft.AspNet.SignalR.Core/Owin/OwinWebSocketHandler.cs @@ -74,29 +74,35 @@ public Task ProcessRequest(IDictionary environment) var cts = new CancellationTokenSource(); var webSocketHandler = new DefaultWebSocketHandler(_maxIncomingMessageSize); + var task = webSocketHandler.ProcessWebSocketRequestAsync(webSocket, cts.Token); - return Task.WhenAll(webSocketHandler.ProcessWebSocketRequestAsync(webSocket, cts.Token), - RunWebSocketHandler(webSocketHandler, cts)); + RunWebSocketHandler(webSocketHandler, cts); + + return task; } - private async Task RunWebSocketHandler(DefaultWebSocketHandler handler, CancellationTokenSource cts) + private void RunWebSocketHandler(DefaultWebSocketHandler handler, CancellationTokenSource cts) { - try - { - await _callback(handler).PreserveCulture(); - } - catch + // async void methods are not supported in ASP.NET and they throw a InvalidOperationException. + Task.Run(async () => { - // This error was already handled by other layers - // we can no-op here so we don't cause an unobserved exception - } + try + { + await _callback(handler).PreserveCulture(); + } + catch + { + // This error was already handled by other layers + // we can no-op here so we don't cause an unobserved exception + } - // Always try to close async, if the websocket already closed - // then this will no-op - await handler.CloseAsync().PreserveCulture(); + // Always try to close async, if the websocket already closed + // then this will no-op + await handler.CloseAsync().PreserveCulture(); - // Cancel the token - cts.Cancel(); + // Cancel the token + cts.Cancel(); + }); } private class OwinWebSocket : WebSocket