diff --git a/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs index 3271883a0a..efdeda8d18 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs @@ -20,7 +20,7 @@ namespace Microsoft.Extensions.DependencyInjection /// An that can be used to further configure the SignalR services. public static ISignalRServerBuilder AddSignalRCore(this IServiceCollection services) { - services.AddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>)); services.TryAddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver)); services.TryAddSingleton(typeof(IHubContext<>), typeof(HubContext<>)); diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs index dfc3c9e644..8974bd094f 100644 --- a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs @@ -4,6 +4,7 @@ using System; using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR.Internal; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; namespace Microsoft.Extensions.DependencyInjection @@ -35,8 +36,8 @@ namespace Microsoft.Extensions.DependencyInjection public static ISignalRServerBuilder AddSignalR(this IServiceCollection services) { services.AddConnections(); - services.AddSingleton(); - services.AddSingleton, HubOptionsSetup>(); + services.TryAddSingleton(); + services.TryAddEnumerable(ServiceDescriptor.Singleton, HubOptionsSetup>()); return services.AddSignalRCore(); } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/AddSignalRTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/AddSignalRTests.cs index 6d8360d745..f5e15fb0b0 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/AddSignalRTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/AddSignalRTests.cs @@ -4,8 +4,10 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR.Internal; using Microsoft.AspNetCore.SignalR.Protocol; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Xunit; namespace Microsoft.AspNetCore.SignalR.Tests @@ -17,12 +19,16 @@ namespace Microsoft.AspNetCore.SignalR.Tests { var serviceCollection = new ServiceCollection(); + var markerService = new SignalRCoreMarkerService(); + serviceCollection.AddSingleton(markerService); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(typeof(HubLifetimeManager<>), typeof(CustomHubLifetimeManager<>)); serviceCollection.AddSingleton(); serviceCollection.AddScoped(typeof(IHubActivator<>), typeof(CustomHubActivator<>)); serviceCollection.AddSingleton(typeof(IHubContext<>), typeof(CustomHubContext<>)); serviceCollection.AddSingleton(typeof(IHubContext<,>), typeof(CustomHubContext<,>)); + var hubOptions = new HubOptionsSetup(new List()); + serviceCollection.AddSingleton>(hubOptions); serviceCollection.AddSignalR(); var serviceProvider = serviceCollection.BuildServiceProvider(); @@ -33,6 +39,8 @@ namespace Microsoft.AspNetCore.SignalR.Tests Assert.IsType>(serviceProvider.GetRequiredService>()); Assert.IsType>(serviceProvider.GetRequiredService>()); Assert.IsType>(serviceProvider.GetRequiredService>()); + Assert.Equal(hubOptions, serviceProvider.GetRequiredService>()); + Assert.Equal(markerService, serviceProvider.GetRequiredService()); } [Fact]