From 3f0a6ebc0de79c2b433c6bc5ea6f115819e024f2 Mon Sep 17 00:00:00 2001 From: BrennanConroy Date: Fri, 13 Apr 2018 17:21:48 -0700 Subject: [PATCH] API Review Dependency Injection (#2015) --- .../SignalRConnectionBuilderExtensions.cs | 9 +++++++++ .../SignalRDependencyInjectionExtensions.cs | 1 + .../SignalRMarkerService.cs | 9 +++++++++ .../RedisDependencyInjectionExtensions.cs | 9 +++++++++ .../SignalRAppBuilderExtensions.cs | 8 -------- .../SignalRDependencyInjectionExtensions.cs | 1 - 6 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 src/Microsoft.AspNetCore.SignalR.Core/SignalRMarkerService.cs diff --git a/src/Microsoft.AspNetCore.SignalR.Core/SignalRConnectionBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Core/SignalRConnectionBuilderExtensions.cs index 8be4f78179..f2701781a9 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/SignalRConnectionBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/SignalRConnectionBuilderExtensions.cs @@ -1,7 +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. +using System; using Microsoft.AspNetCore.Connections; +using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.SignalR { @@ -9,6 +11,13 @@ namespace Microsoft.AspNetCore.SignalR { public static IConnectionBuilder UseHub(this IConnectionBuilder connectionBuilder) where THub : Hub { + var marker = connectionBuilder.ApplicationServices.GetService(typeof(SignalRMarkerService)); + 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."); + } + return connectionBuilder.UseConnectionHandler>(); } } diff --git a/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs index b81859c791..78a6266b4d 100644 --- a/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Core/SignalRDependencyInjectionExtensions.cs @@ -11,6 +11,7 @@ namespace Microsoft.Extensions.DependencyInjection { public static ISignalRServerBuilder AddSignalRCore(this IServiceCollection services) { + 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.Core/SignalRMarkerService.cs b/src/Microsoft.AspNetCore.SignalR.Core/SignalRMarkerService.cs new file mode 100644 index 0000000000..592296f03f --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR.Core/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/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs index 300970ea16..75bc28b017 100644 --- a/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR.Redis/RedisDependencyInjectionExtensions.cs @@ -29,5 +29,14 @@ namespace Microsoft.Extensions.DependencyInjection builder.Services.AddSingleton(typeof(HubLifetimeManager<>), typeof(RedisHubLifetimeManager<>)); return builder; } + + public static ISignalRServerBuilder AddRedis(this ISignalRServerBuilder builder, string redisConnectionString, Action configure) + { + return AddRedis(builder, o => + { + o.Options = ConfigurationOptions.Parse(redisConnectionString); + configure(o); + }); + } } } diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs index d687ad2701..441e4e2467 100644 --- a/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/SignalRAppBuilderExtensions.cs @@ -3,7 +3,6 @@ using System; using Microsoft.AspNetCore.SignalR; -using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Builder { @@ -11,13 +10,6 @@ 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.UseConnections(routes => { configure(new HubRouteBuilder(routes)); diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs index 9799583ec7..77caeec93a 100644 --- a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs @@ -20,7 +20,6 @@ namespace Microsoft.Extensions.DependencyInjection public static ISignalRServerBuilder AddSignalR(this IServiceCollection services) { services.AddConnections(); - services.AddSingleton(); services.AddSingleton, HubOptionsSetup>(); return services.AddSignalRCore(); }