Idempotentize AddSignalR (#2972)
This commit is contained in:
parent
f27df1d61e
commit
8be051ce34
|
|
@ -20,7 +20,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
/// <returns>An <see cref="ISignalRServerBuilder"/> that can be used to further configure the SignalR services.</returns>
|
/// <returns>An <see cref="ISignalRServerBuilder"/> that can be used to further configure the SignalR services.</returns>
|
||||||
public static ISignalRServerBuilder AddSignalRCore(this IServiceCollection services)
|
public static ISignalRServerBuilder AddSignalRCore(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddSingleton<SignalRCoreMarkerService>();
|
services.TryAddSingleton<SignalRCoreMarkerService>();
|
||||||
services.TryAddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>));
|
services.TryAddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>));
|
||||||
services.TryAddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver));
|
services.TryAddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver));
|
||||||
services.TryAddSingleton(typeof(IHubContext<>), typeof(HubContext<>));
|
services.TryAddSingleton(typeof(IHubContext<>), typeof(HubContext<>));
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using Microsoft.AspNetCore.SignalR.Internal;
|
using Microsoft.AspNetCore.SignalR.Internal;
|
||||||
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace Microsoft.Extensions.DependencyInjection
|
namespace Microsoft.Extensions.DependencyInjection
|
||||||
|
|
@ -35,8 +36,8 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
public static ISignalRServerBuilder AddSignalR(this IServiceCollection services)
|
public static ISignalRServerBuilder AddSignalR(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddConnections();
|
services.AddConnections();
|
||||||
services.AddSingleton<SignalRMarkerService>();
|
services.TryAddSingleton<SignalRMarkerService>();
|
||||||
services.AddSingleton<IConfigureOptions<HubOptions>, HubOptionsSetup>();
|
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<HubOptions>, HubOptionsSetup>());
|
||||||
return services.AddSignalRCore();
|
return services.AddSignalRCore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,10 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.SignalR.Internal;
|
||||||
using Microsoft.AspNetCore.SignalR.Protocol;
|
using Microsoft.AspNetCore.SignalR.Protocol;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.SignalR.Tests
|
namespace Microsoft.AspNetCore.SignalR.Tests
|
||||||
|
|
@ -17,12 +19,16 @@ namespace Microsoft.AspNetCore.SignalR.Tests
|
||||||
{
|
{
|
||||||
var serviceCollection = new ServiceCollection();
|
var serviceCollection = new ServiceCollection();
|
||||||
|
|
||||||
|
var markerService = new SignalRCoreMarkerService();
|
||||||
|
serviceCollection.AddSingleton(markerService);
|
||||||
serviceCollection.AddSingleton<IUserIdProvider, CustomIdProvider>();
|
serviceCollection.AddSingleton<IUserIdProvider, CustomIdProvider>();
|
||||||
serviceCollection.AddSingleton(typeof(HubLifetimeManager<>), typeof(CustomHubLifetimeManager<>));
|
serviceCollection.AddSingleton(typeof(HubLifetimeManager<>), typeof(CustomHubLifetimeManager<>));
|
||||||
serviceCollection.AddSingleton<IHubProtocolResolver, CustomHubProtocolResolver>();
|
serviceCollection.AddSingleton<IHubProtocolResolver, CustomHubProtocolResolver>();
|
||||||
serviceCollection.AddScoped(typeof(IHubActivator<>), typeof(CustomHubActivator<>));
|
serviceCollection.AddScoped(typeof(IHubActivator<>), typeof(CustomHubActivator<>));
|
||||||
serviceCollection.AddSingleton(typeof(IHubContext<>), typeof(CustomHubContext<>));
|
serviceCollection.AddSingleton(typeof(IHubContext<>), typeof(CustomHubContext<>));
|
||||||
serviceCollection.AddSingleton(typeof(IHubContext<,>), typeof(CustomHubContext<,>));
|
serviceCollection.AddSingleton(typeof(IHubContext<,>), typeof(CustomHubContext<,>));
|
||||||
|
var hubOptions = new HubOptionsSetup(new List<IHubProtocol>());
|
||||||
|
serviceCollection.AddSingleton<IConfigureOptions<HubOptions>>(hubOptions);
|
||||||
serviceCollection.AddSignalR();
|
serviceCollection.AddSignalR();
|
||||||
|
|
||||||
var serviceProvider = serviceCollection.BuildServiceProvider();
|
var serviceProvider = serviceCollection.BuildServiceProvider();
|
||||||
|
|
@ -33,6 +39,8 @@ namespace Microsoft.AspNetCore.SignalR.Tests
|
||||||
Assert.IsType<CustomHubContext<CustomHub>>(serviceProvider.GetRequiredService<IHubContext<CustomHub>>());
|
Assert.IsType<CustomHubContext<CustomHub>>(serviceProvider.GetRequiredService<IHubContext<CustomHub>>());
|
||||||
Assert.IsType<CustomHubContext<CustomTHub, string>>(serviceProvider.GetRequiredService<IHubContext<CustomTHub, string>>());
|
Assert.IsType<CustomHubContext<CustomTHub, string>>(serviceProvider.GetRequiredService<IHubContext<CustomTHub, string>>());
|
||||||
Assert.IsType<CustomHubContext<CustomDynamicHub>>(serviceProvider.GetRequiredService<IHubContext<CustomDynamicHub>>());
|
Assert.IsType<CustomHubContext<CustomDynamicHub>>(serviceProvider.GetRequiredService<IHubContext<CustomDynamicHub>>());
|
||||||
|
Assert.Equal(hubOptions, serviceProvider.GetRequiredService<IConfigureOptions<HubOptions>>());
|
||||||
|
Assert.Equal(markerService, serviceProvider.GetRequiredService<SignalRCoreMarkerService>());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue