diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs index 627f3ee376..954a18087c 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 { @@ -10,6 +11,13 @@ namespace Microsoft.AspNetCore.Builder { public static IApplicationBuilder UseSignalR(this IApplicationBuilder app, Action configure) { + var marker = app.ApplicationServices.GetService(typeof(SignalRMarkerService)); + if (marker == null) + { + throw new InvalidOperationException("Unable to find the SignalR service. Please add it by " + + "calling 'IServiceCollection.AddSignalR()'."); + } + app.UseSockets(routes => { configure(new HubRouteBuilder(routes)); diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs index ad547f20fa..5872ff392c 100644 --- a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs @@ -12,6 +12,7 @@ namespace Microsoft.Extensions.DependencyInjection public static ISignalRBuilder AddSignalR(this IServiceCollection services) { services.AddSockets(); + services.AddSingleton(); services.AddSingleton, HubOptionsSetup>(); return services.AddSignalRCore() .AddJsonProtocol(); diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRMarkerService.cs b/src/Microsoft.AspNetCore.SignalR/SignalRMarkerService.cs new file mode 100644 index 0000000000..592296f03f --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR/SignalRMarkerService.cs @@ -0,0 +1,9 @@ +// 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. + +namespace Microsoft.Extensions.DependencyInjection +{ + internal class SignalRMarkerService + { + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs index 9308e71653..ec9811fe2b 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/MapSignalRTests.cs @@ -23,6 +23,26 @@ namespace Microsoft.AspNetCore.SignalR.Tests Assert.Equal("Duplicate definitions of 'OverloadedMethod'. Overloading is not supported.", ex.Message); } + [Fact] + public void NotAddingSignalRServiceThrows() + { + var t = new WebHostBuilder() + .UseKestrel() + .Configure(app => + { + var ex = Assert.Throws(() => { + app.UseSignalR(options => + { + options.MapHub("/overloads"); + }); + }); + + Assert.Equal("Unable to find the SignalR service. Please add it by calling 'IServiceCollection.AddSignalR()'.", ex.Message); + }) + .Build(); + + } + [Fact] public void MapHubFindsAuthAttributeOnHub() {