Fix not detecting marker service (#2149)

This commit is contained in:
James Newton-King 2018-05-01 17:42:00 -07:00 committed by GitHub
parent ae329edd2a
commit 555c1fd720
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 22 deletions

View File

@ -3,7 +3,7 @@
namespace Microsoft.AspNetCore.SignalR.Internal
{
internal class SignalRMarkerService
internal class SignalRCoreMarkerService
{
}
}

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.SignalR
/// <returns>The same instance of the <see cref="IConnectionBuilder"/> for chaining.</returns>
public static IConnectionBuilder UseHub<THub>(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 " +

View File

@ -19,7 +19,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// <returns>An <see cref="ISignalRServerBuilder"/> that can be used to further configure the SignalR services.</returns>
public static ISignalRServerBuilder AddSignalRCore(this IServiceCollection services)
{
services.AddSingleton<SignalRMarkerService>();
services.AddSingleton<SignalRCoreMarkerService>();
services.AddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>));
services.AddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver));
services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>));

View File

@ -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
/// <returns>The same instance of the <see cref="IApplicationBuilder"/> for chaining.</returns>
public static IApplicationBuilder UseSignalR(this IApplicationBuilder app, Action<HubRouteBuilder> configure)
{
var marker = app.ApplicationServices.GetService<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.");
}
app.UseConnections(routes =>
{
configure(new HubRouteBuilder(routes));

View File

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

View File

@ -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<NotSupportedException>(() =>
{
using (var builder = BuildWebHost(routes => routes.MapHub<InvalidHub>("/overloads")))
using (var host = BuildWebHost(routes => routes.MapHub<InvalidHub>("/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<InvalidOperationException>(() => {
app.UseSignalR(routes =>
{
routes.MapHub<AuthHub>("/overloads");
var executedConfigure = false;
var builder = new WebHostBuilder();
builder
.UseKestrel()
.Configure(app =>
{
executedConfigure = true;
var ex = Assert.Throws<InvalidOperationException>(() => {
app.UseSignalR(routes =>
{
routes.MapHub<AuthHub>("/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<AuthHub>("/path", options =>
using (var host = BuildWebHost(routes => routes.MapHub<AuthHub>("/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<InheritedAuthHub>("/path", options =>
using (var host = BuildWebHost(routes => routes.MapHub<InheritedAuthHub>("/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<DoubleAuthHub>("/path", options =>
using (var host = BuildWebHost(routes => routes.MapHub<DoubleAuthHub>("/path", options =>
{
authCount += options.AuthorizationData.Count;
})))
{
builder.Start();
host.Start();
}
Assert.Equal(2, authCount);