相关疑难解决方法(0)

如何在 ASP.NET Core 2.2 中使用来自不同托管项目的共享 SignalR Hub

我正在处理一个使用 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)

c# signalr asp.net-core asp.net-core-2.2

5
推荐指数
1
解决办法
1496
查看次数

标签 统计

asp.net-core ×1

asp.net-core-2.2 ×1

c# ×1

signalr ×1