From 555c1fd7208d6e64f0c93a42ff7d76fa234bb85e Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Tue, 1 May 2018 17:42:00 -0700 Subject: [PATCH] Fix not detecting marker service (#2149) --- ...Service.cs => SignalRCoreMarkerService.cs} | 2 +- .../SignalRConnectionBuilderExtensions.cs | 2 +- .../SignalRDependencyInjectionExtensions.cs | 2 +- .../SignalRAppBuilderExtensions.cs | 8 +++ .../SignalRDependencyInjectionExtensions.cs | 1 + .../MapSignalRTests.cs | 50 ++++++++++++------- 6 files changed, 43 insertions(+), 22 deletions(-) rename src/Microsoft.AspNetCore.SignalR.Core/Internal/{SignalRMarkerService.cs => SignalRCoreMarkerService.cs} (83%) diff --git a/src/Microsoft.AspNetCore.SignalR.Core/Internal/SignalRMarkerService.cs b/src/Microsoft.AspNetCore.SignalR.Core/Internal/SignalRCoreMarkerService.cs similarity index 83% rename from src/Microsoft.AspNetCore.SignalR.Core/Internal/SignalRMarkerService.cs rename to src/Microsoft.AspNetCore.SignalR.Core/Internal/SignalRCoreMarkerService.cs index 06720fe924..eb46a4d764 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/Internal/SignalRMarkerService.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/Internal/SignalRCoreMarkerService.cs @@ -3,7 +3,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal { - internal class SignalRMarkerService + internal class SignalRCoreMarkerService { } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.SignalR.Core/SignalRConnectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Core/SignalRConnectionBuilderExtensions.cs index 2e8f076af8..58888470cf 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/SignalRConnectionBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/SignalRConnectionBuilderExtensions.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.SignalR /// The same instance of the for chaining. public static IConnectionBuilder UseHub(this IConnectionBuilder connectionBuilder) where THub : Hub { - var marker = connectionBuilder.ApplicationServices.GetService(typeof(SignalRMarkerService)); + var marker = connectionBuilder.ApplicationServices.GetService(typeof(SignalRCoreMarkerService)); if (marker == null) { throw new InvalidOperationException("Unable to find the required services. Please add all the required services by calling " + diff --git a/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs index 5240748836..3e5587cc5b 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs @@ -19,7 +19,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.AddSingleton(); services.AddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>)); services.AddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver)); services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>)); diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs index c7f5afa84e..8a85de1a70 100644 --- a/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs @@ -3,6 +3,7 @@ using System; using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Builder { @@ -19,6 +20,13 @@ namespace Microsoft.AspNetCore.Builder /// The same instance of the for chaining. public static IApplicationBuilder UseSignalR(this IApplicationBuilder app, Action configure) { + var marker = app.ApplicationServices.GetService(); + if (marker == null) + { + throw new InvalidOperationException("Unable to find the required services. Please add all the required services by calling " + + "'IServiceCollection.AddSignalR' inside the call to 'ConfigureServices(...)' in the application startup code."); + } + app.UseConnections(routes => { configure(new HubRouteBuilder(routes)); diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs index a521bea32c..76fde233f5 100644 --- a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs @@ -35,6 +35,7 @@ namespace Microsoft.Extensions.DependencyInjection public static ISignalRServerBuilder AddSignalR(this IServiceCollection services) { services.AddConnections(); + services.AddSingleton(); services.AddSingleton, HubOptionsSetup>(); return services.AddSignalRCore(); } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs index 3e28106832..5895333d8f 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -14,9 +15,9 @@ namespace Microsoft.AspNetCore.SignalR.Tests { var ex = Assert.Throws(() => { - using (var builder = BuildWebHost(routes => routes.MapHub("/overloads"))) + using (var host = BuildWebHost(routes => routes.MapHub("/overloads"))) { - builder.Start(); + host.Start(); } }); @@ -26,33 +27,44 @@ namespace Microsoft.AspNetCore.SignalR.Tests [Fact] public void NotAddingSignalRServiceThrows() { - var t = new WebHostBuilder() - .UseKestrel() - .Configure(app => - { - var ex = Assert.Throws(() => { - app.UseSignalR(routes => - { - routes.MapHub("/overloads"); + var executedConfigure = false; + var builder = new WebHostBuilder(); + + builder + .UseKestrel() + .Configure(app => + { + executedConfigure = true; + + var ex = Assert.Throws(() => { + app.UseSignalR(routes => + { + routes.MapHub("/overloads"); + }); }); + + Assert.Equal("Unable to find the required services. Please add all the required services by calling " + + "'IServiceCollection.AddSignalR' inside the call to 'ConfigureServices(...)' in the application startup code.", ex.Message); }); - Assert.Equal("Unable to find the SignalR service. Please add it by calling 'IServiceCollection.AddSignalR()'.", ex.Message); - }) - .Build(); + using (var host = builder.Build()) + { + host.Start(); + } + Assert.True(executedConfigure); } [Fact] public void MapHubFindsAuthAttributeOnHub() { var authCount = 0; - using (var builder = BuildWebHost(routes => routes.MapHub("/path", options => + using (var host = BuildWebHost(routes => routes.MapHub("/path", options => { authCount += options.AuthorizationData.Count; }))) { - builder.Start(); + host.Start(); } Assert.Equal(1, authCount); @@ -62,12 +74,12 @@ namespace Microsoft.AspNetCore.SignalR.Tests public void MapHubFindsAuthAttributeOnInheritedHub() { var authCount = 0; - using (var builder = BuildWebHost(routes => routes.MapHub("/path", options => + using (var host = BuildWebHost(routes => routes.MapHub("/path", options => { authCount += options.AuthorizationData.Count; }))) { - builder.Start(); + host.Start(); } Assert.Equal(1, authCount); @@ -77,12 +89,12 @@ namespace Microsoft.AspNetCore.SignalR.Tests public void MapHubFindsMultipleAuthAttributesOnDoubleAuthHub() { var authCount = 0; - using (var builder = BuildWebHost(routes => routes.MapHub("/path", options => + using (var host = BuildWebHost(routes => routes.MapHub("/path", options => { authCount += options.AuthorizationData.Count; }))) { - builder.Start(); + host.Start(); } Assert.Equal(2, authCount);