Run users options last (#15121)

This commit is contained in:
Brennan 2019-10-22 09:53:14 -07:00 committed by GitHub
parent 08608af68a
commit 1ea47f119b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 113 additions and 13 deletions

View File

@ -37,8 +37,7 @@ namespace Microsoft.AspNetCore.SignalR.Microbenchmarks
_dispatcher = new DefaultHubDispatcher<TestHub>( _dispatcher = new DefaultHubDispatcher<TestHub>(
serviceScopeFactory, serviceScopeFactory,
new HubContext<TestHub>(new DefaultHubLifetimeManager<TestHub>(NullLogger<DefaultHubLifetimeManager<TestHub>>.Instance)), new HubContext<TestHub>(new DefaultHubLifetimeManager<TestHub>(NullLogger<DefaultHubLifetimeManager<TestHub>>.Instance)),
Options.Create(new HubOptions<TestHub>()), enableDetailedErrors: false,
Options.Create(new HubOptions()),
new Logger<DefaultHubDispatcher<TestHub>>(NullLoggerFactory.Instance)); new Logger<DefaultHubDispatcher<TestHub>>(NullLoggerFactory.Instance));
var pair = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default); var pair = DuplexPipe.CreateConnectionPair(PipeOptions.Default, PipeOptions.Default);

View File

@ -59,14 +59,22 @@ namespace Microsoft.AspNetCore.SignalR
_logger = loggerFactory.CreateLogger<HubConnectionHandler<THub>>(); _logger = loggerFactory.CreateLogger<HubConnectionHandler<THub>>();
_userIdProvider = userIdProvider; _userIdProvider = userIdProvider;
_enableDetailedErrors = _hubOptions.EnableDetailedErrors ?? _globalHubOptions.EnableDetailedErrors ?? false; _enableDetailedErrors = false;
_maximumMessageSize = _hubOptions.MaximumReceiveMessageSize ?? _globalHubOptions.MaximumReceiveMessageSize; if (_hubOptions.UserHasSetValues)
{
_maximumMessageSize = _hubOptions.MaximumReceiveMessageSize;
_enableDetailedErrors = _hubOptions.EnableDetailedErrors ?? _enableDetailedErrors;
}
else
{
_maximumMessageSize = _globalHubOptions.MaximumReceiveMessageSize;
_enableDetailedErrors = _globalHubOptions.EnableDetailedErrors ?? _enableDetailedErrors;
}
_dispatcher = new DefaultHubDispatcher<THub>( _dispatcher = new DefaultHubDispatcher<THub>(
serviceScopeFactory, serviceScopeFactory,
new HubContext<THub>(lifetimeManager), new HubContext<THub>(lifetimeManager),
hubOptions, _enableDetailedErrors,
globalHubOptions,
new Logger<DefaultHubDispatcher<THub>>(loggerFactory)); new Logger<DefaultHubDispatcher<THub>>(loggerFactory));
} }

View File

@ -23,6 +23,12 @@ namespace Microsoft.AspNetCore.SignalR
} }
options.KeepAliveInterval = _hubOptions.KeepAliveInterval; options.KeepAliveInterval = _hubOptions.KeepAliveInterval;
options.HandshakeTimeout = _hubOptions.HandshakeTimeout; options.HandshakeTimeout = _hubOptions.HandshakeTimeout;
options.ClientTimeoutInterval = _hubOptions.ClientTimeoutInterval;
options.EnableDetailedErrors = _hubOptions.EnableDetailedErrors;
options.MaximumReceiveMessageSize = _hubOptions.MaximumReceiveMessageSize;
options.StreamBufferCapacity = _hubOptions.StreamBufferCapacity;
options.UserHasSetValues = true;
} }
} }
} }

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.SignalR namespace Microsoft.AspNetCore.SignalR
@ -9,5 +9,6 @@ namespace Microsoft.AspNetCore.SignalR
/// <typeparam name="THub">The hub type to configure.</typeparam> /// <typeparam name="THub">The hub type to configure.</typeparam>
public class HubOptions<THub> : HubOptions where THub : Hub public class HubOptions<THub> : HubOptions where THub : Hub
{ {
internal bool UserHasSetValues { get; set; }
} }
} }

View File

@ -28,12 +28,11 @@ namespace Microsoft.AspNetCore.SignalR.Internal
private readonly ILogger<HubDispatcher<THub>> _logger; private readonly ILogger<HubDispatcher<THub>> _logger;
private readonly bool _enableDetailedErrors; private readonly bool _enableDetailedErrors;
public DefaultHubDispatcher(IServiceScopeFactory serviceScopeFactory, IHubContext<THub> hubContext, IOptions<HubOptions<THub>> hubOptions, public DefaultHubDispatcher(IServiceScopeFactory serviceScopeFactory, IHubContext<THub> hubContext, bool enableDetailedErrors, ILogger<DefaultHubDispatcher<THub>> logger)
IOptions<HubOptions> globalHubOptions, ILogger<DefaultHubDispatcher<THub>> logger)
{ {
_serviceScopeFactory = serviceScopeFactory; _serviceScopeFactory = serviceScopeFactory;
_hubContext = hubContext; _hubContext = hubContext;
_enableDetailedErrors = hubOptions.Value.EnableDetailedErrors ?? globalHubOptions.Value.EnableDetailedErrors ?? false; _enableDetailedErrors = enableDetailedErrors;
_logger = logger; _logger = logger;
DiscoverHubMethods(); DiscoverHubMethods();
} }

View File

@ -4,7 +4,6 @@
using System; using System;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Internal;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -67,8 +66,10 @@ namespace Microsoft.Extensions.DependencyInjection
throw new ArgumentNullException(nameof(services)); throw new ArgumentNullException(nameof(services));
} }
return services.Configure(configure) var signalrBuilder = services.AddSignalR();
.AddSignalR(); // Setup users settings after we've setup ours
services.Configure(configure);
return signalrBuilder;
} }
} }
} }

View File

@ -1,6 +1,7 @@
// Copyright(c) .NET Foundation.All rights reserved. // Copyright(c) .NET Foundation.All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -81,6 +82,29 @@ namespace Microsoft.AspNetCore.SignalR.Tests
Assert.Equal(0, serviceProvider.GetRequiredService<IOptions<HubOptions<CustomHub>>>().Value.SupportedProtocols.Count); Assert.Equal(0, serviceProvider.GetRequiredService<IOptions<HubOptions<CustomHub>>>().Value.SupportedProtocols.Count);
} }
[Fact]
public void HubSpecificOptionsHaveSameValuesAsGlobalHubOptions()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSignalR().AddHubOptions<CustomHub>(options =>
{
});
var serviceProvider = serviceCollection.BuildServiceProvider();
var hubOptions = serviceProvider.GetRequiredService<IOptions<HubOptions<CustomHub>>>().Value;
var globalHubOptions = serviceProvider.GetRequiredService<IOptions<HubOptions>>().Value;
Assert.Equal(globalHubOptions.MaximumReceiveMessageSize, hubOptions.MaximumReceiveMessageSize);
Assert.Equal(globalHubOptions.StreamBufferCapacity, hubOptions.StreamBufferCapacity);
Assert.Equal(globalHubOptions.EnableDetailedErrors, hubOptions.EnableDetailedErrors);
Assert.Equal(globalHubOptions.KeepAliveInterval, hubOptions.KeepAliveInterval);
Assert.Equal(globalHubOptions.HandshakeTimeout, hubOptions.HandshakeTimeout);
Assert.Equal(globalHubOptions.SupportedProtocols, hubOptions.SupportedProtocols);
Assert.Equal(globalHubOptions.ClientTimeoutInterval, hubOptions.ClientTimeoutInterval);
Assert.True(hubOptions.UserHasSetValues);
}
[Fact] [Fact]
public void StreamBufferCapacityGetSet() public void StreamBufferCapacityGetSet()
{ {
@ -94,6 +118,36 @@ namespace Microsoft.AspNetCore.SignalR.Tests
var serviceProvider = serviceCollection.BuildServiceProvider(); var serviceProvider = serviceCollection.BuildServiceProvider();
Assert.Equal(42, serviceProvider.GetRequiredService<IOptions<HubOptions<CustomHub>>>().Value.StreamBufferCapacity); Assert.Equal(42, serviceProvider.GetRequiredService<IOptions<HubOptions<CustomHub>>>().Value.StreamBufferCapacity);
} }
[Fact]
public void UserSpecifiedOptionsRunAfterDefaultOptions()
{
var serviceCollection = new ServiceCollection();
// null is special when the default options setup runs, so we set to null to verify that our options run after the default
// setup runs
serviceCollection.AddSignalR(options =>
{
options.MaximumReceiveMessageSize = null;
options.StreamBufferCapacity = null;
options.EnableDetailedErrors = null;
options.KeepAliveInterval = null;
options.HandshakeTimeout = null;
options.SupportedProtocols = null;
options.ClientTimeoutInterval = TimeSpan.FromSeconds(1);
});
var serviceProvider = serviceCollection.BuildServiceProvider();
var globalOptions = serviceProvider.GetRequiredService<IOptions<HubOptions>>().Value;
Assert.Null(globalOptions.MaximumReceiveMessageSize);
Assert.Null(globalOptions.StreamBufferCapacity);
Assert.Null(globalOptions.EnableDetailedErrors);
Assert.Null(globalOptions.KeepAliveInterval);
Assert.Null(globalOptions.HandshakeTimeout);
Assert.Null(globalOptions.SupportedProtocols);
Assert.Equal(TimeSpan.FromSeconds(1), globalOptions.ClientTimeoutInterval);
}
} }
public class CustomHub : Hub public class CustomHub : Hub

View File

@ -3834,6 +3834,38 @@ namespace Microsoft.AspNetCore.SignalR.Tests
} }
} }
[Fact]
public async Task SpecificHubOptionForMaximumReceiveMessageSizeIsUsedOverGlobalHubOption()
{
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(serviceBuilder =>
{
serviceBuilder.AddSignalR(o =>
{
// ConnectAsync would fail if this value was used
o.MaximumReceiveMessageSize = 1;
}).AddHubOptions<MethodHub>(o =>
{
// null is treated as both no-limit and not set, this test verifies that we track if the user explicitly sets the value
o.MaximumReceiveMessageSize = null;
});
});
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<MethodHub>>();
using (StartVerifiableLog())
{
using var client = new TestClient();
var connectionHandlerTask = await client.ConnectAsync(connectionHandler);
// Wait for a connection, or for the endpoint to fail.
await client.Connected.OrThrowIfOtherFails(connectionHandlerTask).OrTimeout();
await client.DisposeAsync().OrTimeout();
await connectionHandlerTask.OrTimeout();
}
}
private class CustomHubActivator<THub> : IHubActivator<THub> where THub : Hub private class CustomHubActivator<THub> : IHubActivator<THub> where THub : Hub
{ {
public int ReleaseCount; public int ReleaseCount;