diff --git a/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs b/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs index 98583d9903f4..6dc3a9362059 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/HubConnectionContext.cs @@ -21,6 +21,7 @@ namespace Microsoft.AspNetCore.SignalR { public class HubConnectionContext { + private static readonly Action _cancelReader = state => ((PipeReader)state).CancelPendingRead(); private static readonly WaitCallback _abortedCallback = AbortConnection; private readonly ConnectionContext _connectionContext; @@ -344,7 +345,10 @@ internal async Task HandshakeAsync(TimeSpan timeout, IReadOnlyList { try { + var input = Input; + using (var cts = new CancellationTokenSource()) + using (var registration = cts.Token.Register(_cancelReader, input)) { if (!Debugger.IsAttached) { @@ -353,7 +357,8 @@ internal async Task HandshakeAsync(TimeSpan timeout, IReadOnlyList while (true) { - var result = await _connectionContext.Transport.Input.ReadAsync(cts.Token); + var result = await input.ReadAsync(); + var buffer = result.Buffer; var consumed = buffer.Start; var examined = buffer.End; @@ -363,6 +368,7 @@ internal async Task HandshakeAsync(TimeSpan timeout, IReadOnlyList if (result.IsCanceled) { Log.HandshakeCanceled(_logger); + await WriteHandshakeResponseAsync(new HandshakeResponseMessage("Handshake was canceled.")); return false; } @@ -434,7 +440,7 @@ await WriteHandshakeResponseAsync(new HandshakeResponseMessage( } finally { - _connectionContext.Transport.Input.AdvanceTo(consumed, examined); + input.AdvanceTo(consumed, examined); } } } diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs index 8974bd094f9b..f663ac725431 100644 --- a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR.Internal; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -36,6 +37,8 @@ public static ISignalRServerBuilder AddHubOptions(this ISignalRServerBuild public static ISignalRServerBuilder AddSignalR(this IServiceCollection services) { services.AddConnections(); + // Disable the WebSocket keep alive since SignalR has it's own + services.Configure(o => o.KeepAliveInterval = TimeSpan.Zero); services.TryAddSingleton(); services.TryAddEnumerable(ServiceDescriptor.Singleton, HubOptionsSetup>()); return services.AddSignalRCore();