From 6af6db67f4148ca471a8c04091cd0fd8d60d5086 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Wed, 2 Nov 2016 03:03:35 -0700 Subject: [PATCH] More refactoring - Moved the IHubConnectionContext implementation out of HubEndPoint - Added IHubContext to allow getting at the publish side of things without being in side the hub. The HubEndPoint now injects this as well. - HubContext has the implementation of the IHubConnectionContext - Moved ISignalRBuilder and SignalRBuilder into their own files --- .../DependencyInjectionExtensions.cs | 16 +------- .../HubContext.cs | 38 +++++++++++++++++++ .../HubEndPoint.cs | 25 +++--------- .../IHubContext.cs | 12 ++++++ .../ISignalRBuilder.cs | 13 +++++++ .../SignalRBuilder.cs | 18 +++++++++ 6 files changed, 87 insertions(+), 35 deletions(-) create mode 100644 src/Microsoft.AspNetCore.SignalR/HubContext.cs create mode 100644 src/Microsoft.AspNetCore.SignalR/IHubContext.cs create mode 100644 src/Microsoft.AspNetCore.SignalR/ISignalRBuilder.cs create mode 100644 src/Microsoft.AspNetCore.SignalR/SignalRBuilder.cs diff --git a/src/Microsoft.AspNetCore.SignalR/DependencyInjectionExtensions.cs b/src/Microsoft.AspNetCore.SignalR/DependencyInjectionExtensions.cs index 7bc3ec6a30..125372ee6c 100644 --- a/src/Microsoft.AspNetCore.SignalR/DependencyInjectionExtensions.cs +++ b/src/Microsoft.AspNetCore.SignalR/DependencyInjectionExtensions.cs @@ -11,25 +11,11 @@ namespace Microsoft.Extensions.DependencyInjection public static ISignalRBuilder AddSignalR(this IServiceCollection services) { services.AddSingleton(typeof(HubLifetimeManager<>), typeof(DefaultHubLifetimeManager<>)); + services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>)); services.AddSingleton(typeof(HubEndPoint<>), typeof(HubEndPoint<>)); services.AddSingleton(typeof(RpcEndpoint<>), typeof(RpcEndpoint<>)); return new SignalRBuilder(services); } } - - public interface ISignalRBuilder - { - IServiceCollection Services { get; } - } - - public class SignalRBuilder : ISignalRBuilder - { - public SignalRBuilder(IServiceCollection services) - { - Services = services; - } - - public IServiceCollection Services { get; } - } } diff --git a/src/Microsoft.AspNetCore.SignalR/HubContext.cs b/src/Microsoft.AspNetCore.SignalR/HubContext.cs new file mode 100644 index 0000000000..6a30e74391 --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR/HubContext.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Microsoft.AspNetCore.SignalR +{ + public class HubContext : IHubContext, IHubConnectionContext + { + private readonly HubLifetimeManager _lifetimeManager; + private readonly AllClientProxy _all; + + public HubContext(HubLifetimeManager lifetimeManager) + { + _lifetimeManager = lifetimeManager; + _all = new AllClientProxy(_lifetimeManager); + } + + public IHubConnectionContext Clients => this; + + public virtual IClientProxy All => _all; + + public virtual IClientProxy Client(string connectionId) + { + return new SingleClientProxy(_lifetimeManager, connectionId); + } + + public virtual IClientProxy Group(string groupName) + { + return new GroupProxy(_lifetimeManager, groupName); + } + + public virtual IClientProxy User(string userId) + { + return new UserProxy(_lifetimeManager, userId); + } + } +} diff --git a/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs b/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs index c2758b6137..62da466e2d 100644 --- a/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs +++ b/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs @@ -6,36 +6,21 @@ using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.SignalR { - public class HubEndPoint : RpcEndpoint, IHubConnectionContext where THub : Hub + public class HubEndPoint : RpcEndpoint where THub : Hub { private readonly AllClientProxy _all; private readonly HubLifetimeManager _lifetimeManager; + private readonly IHubContext _hubContext; public HubEndPoint(HubLifetimeManager lifetimeManager, + IHubContext hubContext, InvocationAdapterRegistry registry, ILoggerFactory loggerFactory, IServiceScopeFactory serviceScopeFactory) : base(registry, loggerFactory, serviceScopeFactory) { _lifetimeManager = lifetimeManager; - _all = new AllClientProxy(_lifetimeManager); - } - - public virtual IClientProxy All => _all; - - public virtual IClientProxy Client(string connectionId) - { - return new SingleClientProxy(_lifetimeManager, connectionId); - } - - public virtual IClientProxy Group(string groupName) - { - return new GroupProxy(_lifetimeManager, groupName); - } - - public virtual IClientProxy User(string userId) - { - return new UserProxy(_lifetimeManager, userId); + _hubContext = hubContext; } public override async Task OnConnected(Connection connection) @@ -73,7 +58,7 @@ namespace Microsoft.AspNetCore.SignalR private void Initialize(Connection connection, THub hub) { - hub.Clients = this; + hub.Clients = _hubContext.Clients; hub.Context = new HubCallerContext(connection); hub.Groups = new GroupManager(connection, _lifetimeManager); } diff --git a/src/Microsoft.AspNetCore.SignalR/IHubContext.cs b/src/Microsoft.AspNetCore.SignalR/IHubContext.cs new file mode 100644 index 0000000000..0c9f3f62a1 --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR/IHubContext.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Microsoft.AspNetCore.SignalR +{ + public interface IHubContext + { + IHubConnectionContext Clients { get; } + } +} diff --git a/src/Microsoft.AspNetCore.SignalR/ISignalRBuilder.cs b/src/Microsoft.AspNetCore.SignalR/ISignalRBuilder.cs new file mode 100644 index 0000000000..4107d37d0a --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR/ISignalRBuilder.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.AspNetCore.SignalR +{ + public interface ISignalRBuilder + { + IServiceCollection Services { get; } + } +} diff --git a/src/Microsoft.AspNetCore.SignalR/SignalRBuilder.cs b/src/Microsoft.AspNetCore.SignalR/SignalRBuilder.cs new file mode 100644 index 0000000000..714fb5956a --- /dev/null +++ b/src/Microsoft.AspNetCore.SignalR/SignalRBuilder.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.AspNetCore.SignalR +{ + public class SignalRBuilder : ISignalRBuilder + { + public SignalRBuilder(IServiceCollection services) + { + Services = services; + } + + public IServiceCollection Services { get; } + } +}