From cfaa4b69d7db134a759da920fa80d74374cc6b3a Mon Sep 17 00:00:00 2001 From: Mikael Mengistu Date: Wed, 5 Jul 2017 22:55:16 -0700 Subject: [PATCH] Dynamic Hub Work Part 1 (#636) Getting rid of TClient --- samples/ChatSample/HubWithPresence.cs | 13 ++------- .../ChatSample/PresenceHubLifetimeManager.cs | 2 +- .../HubRouteBuilder.cs | 4 +-- .../DefaultHubActivator.cs | 3 +- src/Microsoft.AspNetCore.SignalR/Hub.cs | 10 ++----- .../HubContext.cs | 9 ++---- .../HubEndPoint.cs | 28 ++++++------------- .../IHubActivator.cs | 2 +- ...HubConnectionContext.cs => IHubClients.cs} | 10 +++---- .../IHubContext.cs | 8 ++---- .../Internal/HubReflectionHelper.cs | 5 ++-- .../SignalRDependencyInjectionExtensions.cs | 2 +- .../SignalRSocketBuilderExtensions.cs | 2 +- .../DefaultHubActivatorTests.cs | 16 +++++------ .../HubEndpointTests.cs | 2 +- 15 files changed, 41 insertions(+), 75 deletions(-) rename src/Microsoft.AspNetCore.SignalR/{IHubConnectionContext.cs => IHubClients.cs} (52%) diff --git a/samples/ChatSample/HubWithPresence.cs b/samples/ChatSample/HubWithPresence.cs index ca8f0641a5..e54d8911d7 100644 --- a/samples/ChatSample/HubWithPresence.cs +++ b/samples/ChatSample/HubWithPresence.cs @@ -7,18 +7,11 @@ using Microsoft.AspNetCore.SignalR; namespace ChatSample { - public class HubWithPresence : HubWithPresence + public class HubWithPresence : Hub { + private IUserTracker _userTracker; + public HubWithPresence(IUserTracker userTracker) - : base(userTracker) - { } - } - - public class HubWithPresence : Hub - { - private IUserTracker> _userTracker; - - public HubWithPresence(IUserTracker> userTracker) { _userTracker = userTracker; } diff --git a/samples/ChatSample/PresenceHubLifetimeManager.cs b/samples/ChatSample/PresenceHubLifetimeManager.cs index a1d5bfbc22..991f46a168 100644 --- a/samples/ChatSample/PresenceHubLifetimeManager.cs +++ b/samples/ChatSample/PresenceHubLifetimeManager.cs @@ -102,7 +102,7 @@ namespace ChatSample { using (var scope = _serviceScopeFactory.CreateScope()) { - var hubActivator = scope.ServiceProvider.GetRequiredService>(); + var hubActivator = scope.ServiceProvider.GetRequiredService>(); var hub = hubActivator.Create(); if (_hubContext == null) diff --git a/src/Microsoft.AspNetCore.SignalR.Http/HubRouteBuilder.cs b/src/Microsoft.AspNetCore.SignalR.Http/HubRouteBuilder.cs index d50dd2b17f..2224d54631 100644 --- a/src/Microsoft.AspNetCore.SignalR.Http/HubRouteBuilder.cs +++ b/src/Microsoft.AspNetCore.SignalR.Http/HubRouteBuilder.cs @@ -17,12 +17,12 @@ namespace Microsoft.AspNetCore.SignalR _routes = routes; } - public void MapHub(string path) where THub : Hub + public void MapHub(string path) where THub : Hub { MapHub(path, socketOptions: null); } - public void MapHub(string path, Action socketOptions) where THub : Hub + public void MapHub(string path, Action socketOptions) where THub : Hub { // find auth attributes var authorizeAttributes = typeof(THub).GetCustomAttributes(inherit: true); diff --git a/src/Microsoft.AspNetCore.SignalR/DefaultHubActivator.cs b/src/Microsoft.AspNetCore.SignalR/DefaultHubActivator.cs index 84687e1f2e..cea440670b 100644 --- a/src/Microsoft.AspNetCore.SignalR/DefaultHubActivator.cs +++ b/src/Microsoft.AspNetCore.SignalR/DefaultHubActivator.cs @@ -7,8 +7,7 @@ using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.SignalR { - public class DefaultHubActivator : IHubActivator - where THub: Hub + public class DefaultHubActivator : IHubActivator where THub: Hub { private readonly IServiceProvider _serviceProvider; private bool? _created; diff --git a/src/Microsoft.AspNetCore.SignalR/Hub.cs b/src/Microsoft.AspNetCore.SignalR/Hub.cs index 3ecc69bf51..22ddb91471 100644 --- a/src/Microsoft.AspNetCore.SignalR/Hub.cs +++ b/src/Microsoft.AspNetCore.SignalR/Hub.cs @@ -6,18 +6,14 @@ using System.Threading.Tasks; namespace Microsoft.AspNetCore.SignalR { - public class Hub : Hub - { - } - - public class Hub : IDisposable + public class Hub : IDisposable { private bool _disposed; - private IHubConnectionContext _clients; + private IHubClients _clients; private HubCallerContext _context; private IGroupManager _groups; - public IHubConnectionContext Clients + public IHubClients Clients { get { diff --git a/src/Microsoft.AspNetCore.SignalR/HubContext.cs b/src/Microsoft.AspNetCore.SignalR/HubContext.cs index ce96c9249a..3464e78417 100644 --- a/src/Microsoft.AspNetCore.SignalR/HubContext.cs +++ b/src/Microsoft.AspNetCore.SignalR/HubContext.cs @@ -1,14 +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 System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - namespace Microsoft.AspNetCore.SignalR { - public class HubContext : IHubContext, IHubConnectionContext + public class HubContext : IHubContext, IHubClients where THub : Hub { private readonly HubLifetimeManager _lifetimeManager; private readonly AllClientProxy _all; @@ -19,7 +14,7 @@ namespace Microsoft.AspNetCore.SignalR _all = new AllClientProxy(_lifetimeManager); } - public IHubConnectionContext Clients => this; + public IHubClients Clients => this; public virtual IClientProxy All => _all; diff --git a/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs b/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs index 53df3de1e4..0560101270 100644 --- a/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs +++ b/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs @@ -20,32 +20,20 @@ using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.SignalR { - public class HubEndPoint : HubEndPoint where THub : Hub - { - public HubEndPoint(HubLifetimeManager lifetimeManager, - IHubProtocolResolver protocolResolver, - IHubContext hubContext, - ILogger> logger, - IServiceScopeFactory serviceScopeFactory) - : base(lifetimeManager, protocolResolver, hubContext, logger, serviceScopeFactory) - { - } - } - - public class HubEndPoint : IInvocationBinder where THub : Hub + public class HubEndPoint : IInvocationBinder where THub : Hub { private readonly Dictionary _methods = new Dictionary(StringComparer.OrdinalIgnoreCase); private readonly HubLifetimeManager _lifetimeManager; - private readonly IHubContext _hubContext; - private readonly ILogger> _logger; + private readonly IHubContext _hubContext; + private readonly ILogger> _logger; private readonly IServiceScopeFactory _serviceScopeFactory; private readonly IHubProtocolResolver _protocolResolver; public HubEndPoint(HubLifetimeManager lifetimeManager, IHubProtocolResolver protocolResolver, - IHubContext hubContext, - ILogger> logger, + IHubContext hubContext, + ILogger> logger, IServiceScopeFactory serviceScopeFactory) { _protocolResolver = protocolResolver; @@ -146,7 +134,7 @@ namespace Microsoft.AspNetCore.SignalR { using (var scope = _serviceScopeFactory.CreateScope()) { - var hubActivator = scope.ServiceProvider.GetRequiredService>(); + var hubActivator = scope.ServiceProvider.GetRequiredService>(); var hub = hubActivator.Create(); try { @@ -172,7 +160,7 @@ namespace Microsoft.AspNetCore.SignalR { using (var scope = _serviceScopeFactory.CreateScope()) { - var hubActivator = scope.ServiceProvider.GetRequiredService>(); + var hubActivator = scope.ServiceProvider.GetRequiredService>(); var hub = hubActivator.Create(); try { @@ -311,7 +299,7 @@ namespace Microsoft.AspNetCore.SignalR return; } - var hubActivator = scope.ServiceProvider.GetRequiredService>(); + var hubActivator = scope.ServiceProvider.GetRequiredService>(); var hub = hubActivator.Create(); try diff --git a/src/Microsoft.AspNetCore.SignalR/IHubActivator.cs b/src/Microsoft.AspNetCore.SignalR/IHubActivator.cs index 72d42e5574..c4bdc2a6a4 100644 --- a/src/Microsoft.AspNetCore.SignalR/IHubActivator.cs +++ b/src/Microsoft.AspNetCore.SignalR/IHubActivator.cs @@ -5,7 +5,7 @@ using System; namespace Microsoft.AspNetCore.SignalR { - public interface IHubActivator where THub : Hub + public interface IHubActivator where THub : Hub { THub Create(); void Release(THub hub); diff --git a/src/Microsoft.AspNetCore.SignalR/IHubConnectionContext.cs b/src/Microsoft.AspNetCore.SignalR/IHubClients.cs similarity index 52% rename from src/Microsoft.AspNetCore.SignalR/IHubConnectionContext.cs rename to src/Microsoft.AspNetCore.SignalR/IHubClients.cs index 6dfd59f446..7721dc1c88 100644 --- a/src/Microsoft.AspNetCore.SignalR/IHubConnectionContext.cs +++ b/src/Microsoft.AspNetCore.SignalR/IHubClients.cs @@ -3,14 +3,14 @@ namespace Microsoft.AspNetCore.SignalR { - public interface IHubConnectionContext + public interface IHubClients { - TClient All { get; } + IClientProxy All { get; } - TClient Client(string connectionId); + IClientProxy Client(string connectionId); - TClient Group(string groupName); + IClientProxy Group(string groupName); - TClient User(string userId); + IClientProxy User(string userId); } } diff --git a/src/Microsoft.AspNetCore.SignalR/IHubContext.cs b/src/Microsoft.AspNetCore.SignalR/IHubContext.cs index 578ccc8d36..77b1ba181a 100644 --- a/src/Microsoft.AspNetCore.SignalR/IHubContext.cs +++ b/src/Microsoft.AspNetCore.SignalR/IHubContext.cs @@ -8,12 +8,8 @@ using System.Threading.Tasks; namespace Microsoft.AspNetCore.SignalR { - public interface IHubContext - { - IHubConnectionContext Clients { get; } - } - - public interface IHubContext : IHubContext + public interface IHubContext where THub: Hub { + IHubClients Clients { get; } } } diff --git a/src/Microsoft.AspNetCore.SignalR/Internal/HubReflectionHelper.cs b/src/Microsoft.AspNetCore.SignalR/Internal/HubReflectionHelper.cs index 5ed3f98829..eb1e272a66 100644 --- a/src/Microsoft.AspNetCore.SignalR/Internal/HubReflectionHelper.cs +++ b/src/Microsoft.AspNetCore.SignalR/Internal/HubReflectionHelper.cs @@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.SignalR.Internal { public static class HubReflectionHelper { - private static readonly Type[] _excludeInterfaces = new[] { typeof(Hub<>), typeof(IDisposable) }; + private static readonly Type[] _excludeInterfaces = new[] { typeof(IDisposable) }; public static IEnumerable GetHubMethods(Type hubType) { @@ -38,9 +38,8 @@ namespace Microsoft.AspNetCore.SignalR.Internal return false; } - // removes methods such as Hub.OnConnectedAsync var baseType = baseDefinition.GetTypeInfo().IsGenericType ? baseDefinition.GetGenericTypeDefinition() : baseDefinition; - return typeof(Hub<>) != baseType; + return typeof(Hub) != baseType; } } } diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs index 5961e221fe..f9d4038789 100644 --- a/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/SignalRDependencyInjectionExtensions.cs @@ -14,7 +14,7 @@ namespace Microsoft.Extensions.DependencyInjection services.AddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver)); services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>)); services.AddSingleton(typeof(HubEndPoint<>), typeof(HubEndPoint<>)); - services.AddScoped(typeof(IHubActivator<,>), typeof(DefaultHubActivator<,>)); + services.AddScoped(typeof(IHubActivator<>), typeof(DefaultHubActivator<>)); services.AddAuthorization(); diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRSocketBuilderExtensions.cs b/src/Microsoft.AspNetCore.SignalR/SignalRSocketBuilderExtensions.cs index 11574c5306..eee93cc206 100644 --- a/src/Microsoft.AspNetCore.SignalR/SignalRSocketBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/SignalRSocketBuilderExtensions.cs @@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.SignalR { public static class SignalRSocketBuilderExtensions { - public static ISocketBuilder UseHub(this ISocketBuilder socketBuilder) where THub : Hub + public static ISocketBuilder UseHub(this ISocketBuilder socketBuilder) where THub : Hub { var endpoint = socketBuilder.ApplicationServices.GetRequiredService>(); return socketBuilder.Run(connection => endpoint.OnConnectedAsync(connection)); diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/DefaultHubActivatorTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/DefaultHubActivatorTests.cs index cde3c44f51..e0917698d2 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/DefaultHubActivatorTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/DefaultHubActivatorTests.cs @@ -14,20 +14,20 @@ namespace Microsoft.AspNetCore.SignalR.Tests public void HubCreatedIfNotResolvedFromServiceProvider() { Assert.NotNull( - new DefaultHubActivator, object>(Mock.Of()).Create()); + new DefaultHubActivator(Mock.Of()).Create()); } [Fact] public void HubCanBeResolvedFromServiceProvider() { - var hub = Mock.Of>(); + var hub = Mock.Of(); var mockServiceProvider = new Mock(); mockServiceProvider - .Setup(sp => sp.GetService(typeof(Hub))) + .Setup(sp => sp.GetService(typeof(Hub))) .Returns(hub); Assert.Same(hub, - new DefaultHubActivator, object>(mockServiceProvider.Object).Create()); + new DefaultHubActivator(mockServiceProvider.Object).Create()); } @@ -36,15 +36,15 @@ namespace Microsoft.AspNetCore.SignalR.Tests { var mockServiceProvider = new Mock(); mockServiceProvider - .Setup(sp => sp.GetService(typeof(Hub))) + .Setup(sp => sp.GetService(typeof(Hub))) .Returns(() => { - var m = new Mock>(); + var m = new Mock(); m.Protected().Setup("Dispose", ItExpr.IsAny()); return m.Object; }); - var hubActivator = new DefaultHubActivator, object>(mockServiceProvider.Object); + var hubActivator = new DefaultHubActivator(mockServiceProvider.Object); var hub = hubActivator.Create(); hubActivator.Release(hub); Mock.Get(hub).Protected().Verify("Dispose", Times.Never(), ItExpr.IsAny()); @@ -55,7 +55,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests { Assert.Equal("hub", Assert.Throws( - () => new DefaultHubActivator, object>(Mock.Of()).Release(null)).ParamName); + () => new DefaultHubActivator(Mock.Of()).Release(null)).ParamName); } } } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs index d35f34a061..a47c7ad13c 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs @@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests mockLifetimeManager .Setup(m => m.OnConnectedAsync(It.IsAny())) .Throws(new InvalidOperationException("Lifetime manager OnConnectedAsync failed.")); - var mockHubActivator = new Mock>(); + var mockHubActivator = new Mock>(); var serviceProvider = CreateServiceProvider(services => {