从域服务和应用层访问SignalR [英] Access SignalR from domain services and application layer
问题描述
This is directly related to calling a SignalR Hub from the Application Service Layer in ASP.NET Boilerplate .NET Core version. As per the solution, SignalR hub implementation should be done in Web layer. But the dependency structure of the projects are like:
- 应用程序依赖于
Domain.Core
-
Web.Core
取决于应用程序 -
Web.Host
取决于Web.Core
- App depends on
Domain.Core
Web.Core
depends on AppWeb.Host
depends onWeb.Core
两个问题:
-
要想在
Domain.Core
和App项目中都使用Hub
,我应该如何将它们全部连接起来?如果我使用空模式在App层中定义接口,则可以在Web.Core
中实现该接口.但是后来我不能在域服务(例如EventBus
)中使用它.
To be able to use the
Hub
in bothDomain.Core
and App projects, how should I wire it all up? If I define the interface in App layer with a null pattern, I can implement it inWeb.Core
. But then I can't use it in domain services (e.g.EventBus
).
我可以将整个SignalR集线器移至新模块并从App,Domain和Web层引用它吗?
Can I move entire SignalR hub to a new module and reference it from App, Domain and Web layers?
推荐答案
- 要想同时在
Domain.Core
和App项目中使用Hub
,我应该如何将它们全部连接起来?
- To be able to use the
Hub
in bothDomain.Core
and App projects, how should I wire it all up?
域层:
-
IMyNotifier
接口 -
NullMyNotifier
空实现
public interface IMyNotifier
{
Task SendMessage(IUserIdentifier user, string message);
}
public class NullMyNotifier : IMyNotifier
{
public static NullMyNotifier Instance { get; } = new NullMyNotifier();
private NullMyNotifier()
{
}
public Task SendMessage(IUserIdentifier user, string message)
{
return Task.FromResult(0);
}
}
网页层:
- SignalR集线器实施,例如
MyChatHub
-
SignalRMyNotifier
具体实现
- SignalR hub implementation, e.g.
MyChatHub
SignalRMyNotifier
concrete implementation
public class SignalRMyNotifier : IMyNotifier, ITransientDependency
{
private readonly IOnlineClientManager _onlineClientManager;
private readonly IHubContext<MyChatHub> _hubContext;
public SignalRMyNotifier(
IOnlineClientManager onlineClientManager,
IHubContext<MyChatHub> hubContext)
{
_onlineClientManager = onlineClientManager;
_hubContext = hubContext;
}
public async Task SendMessage(IUserIdentifier user, string message)
{
var onlineClients = _onlineClientManager.GetAllByUserId(user);
foreach (var onlineClient in onlineClients)
{
var signalRClient = _hubContext.Clients.Client(onlineClient.ConnectionId);
await signalRClient.SendAsync("getMessage", message);
}
}
}
在引用域层的任何层中的用法:
Usage, in any layer that references Domain layer:
public class MyDomainService : DomainService, IMyManager
{
public IMyNotifier MyNotifier { get; set; }
public MyDomainService()
{
MyNotifier = NullMyNotifier.Instance;
}
public async Task DoSomething()
{
// Do something
// ...
var hostAdmin = new UserIdentifier(null, 1);
var message = "Something done";
await MyNotifier.SendMessage(hostAdmin, message);
}
}
- 我可以将整个SignalR集线器移至新模块并从App,Domain和Web层引用它吗?
您可以,但是包含SignalR集线器(以及您的App和Domain层)的新模块将取决于 Microsoft.AspNetCore.SignalR
,而后者取决于 Microsoft.AspNetCore.Http.Connections
.域层不应依赖 Http
.
You can, but the new module that contains the SignalR hub (and thus your App and Domain layers) would depend on Microsoft.AspNetCore.SignalR
, which depends on Microsoft.AspNetCore.Http.Connections
. Domain layer should not depend on Http
.
这篇关于从域服务和应用层访问SignalR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!