我正在处理一个使用 ASP.NET Core 2.2 构建的项目。主解决方案包含多个项目,其中包括API、Web等类库。
我们已经使用 SignalR 来显示 API 项目和 Web 项目之间的共享消息/通知。例如,从 API 添加新员工记录应调用 SignalR Hub,所有 Web 客户端都应收到通知。
这是我们项目的当前结构
|- API
|- Hub_ClassLibrary
|- Services
|- Web
Run Code Online (Sandbox Code Playgroud)
流动:
Web > services > Hub
API > services > Hub
Run Code Online (Sandbox Code Playgroud)
中心:
public class NotificationHub : Hub
{
public async Task SendNotification(List<DocumentHistoryModel> notifications)
{
await Clients.All.SendAsync(Constants.ReceiveNotification, notifications);
}
}
Run Code Online (Sandbox Code Playgroud)
Web启动类:
app.UseSignalR(routes =>
{
routes.MapHub<NotificationHub>("/notificationHub");
});
Run Code Online (Sandbox Code Playgroud)
API启动类
app.UseSignalR(routes =>
{
routes.MapHub<NotificationHub>("/notificationHub");
});
Run Code Online (Sandbox Code Playgroud)
服务
private readonly IHubContext<NotificationHub> _hubContext;
public MyService(IHubContext<NotificationHub> hubContext)
{
_hubContext = hubContext; …Run Code Online (Sandbox Code Playgroud)