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; } + } +}