Add SignalR Marker Service (#1573)

This commit is contained in:
Mikael Mengistu 2018-03-10 00:58:57 +00:00 committed by GitHub
parent d6178f2482
commit 0b81658850
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 0 deletions

View File

@ -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<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.UseSockets(routes =>
{
configure(new HubRouteBuilder(routes));

View File

@ -12,6 +12,7 @@ namespace Microsoft.Extensions.DependencyInjection
public static ISignalRBuilder AddSignalR(this IServiceCollection services)
{
services.AddSockets();
services.AddSingleton<SignalRMarkerService>();
services.AddSingleton<IConfigureOptions<HubOptions>, HubOptionsSetup>();
return services.AddSignalRCore()
.AddJsonProtocol();

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

@ -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<InvalidOperationException>(() => {
app.UseSignalR(options =>
{
options.MapHub<AuthHub>("/overloads");
});
});
Assert.Equal("Unable to find the SignalR service. Please add it by calling 'IServiceCollection.AddSignalR()'.", ex.Message);
})
.Build();
}
[Fact]
public void MapHubFindsAuthAttributeOnHub()
{