API Review Dependency Injection (#2015)

This commit is contained in:
BrennanConroy 2018-04-13 17:21:48 -07:00 committed by GitHub
parent 1fbb940b82
commit 3f0a6ebc0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 9 deletions

View File

@ -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<THub>(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<HubConnectionHandler<THub>>();
}
}

View File

@ -11,6 +11,7 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static ISignalRServerBuilder AddSignalRCore(this IServiceCollection services)
{
services.AddSingleton<SignalRMarkerService>();
services.AddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>));
services.AddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver));
services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>));

View File

@ -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
{
}
}

View File

@ -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<RedisOptions> configure)
{
return AddRedis(builder, o =>
{
o.Options = ConfigurationOptions.Parse(redisConnectionString);
configure(o);
});
}
}
}

View File

@ -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<HubRouteBuilder> 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));

View File

@ -20,7 +20,6 @@ namespace Microsoft.Extensions.DependencyInjection
public static ISignalRServerBuilder AddSignalR(this IServiceCollection services)
{
services.AddConnections();
services.AddSingleton<SignalRMarkerService>();
services.AddSingleton<IConfigureOptions<HubOptions>, HubOptionsSetup>();
return services.AddSignalRCore();
}