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

问题描述 投票:4回答:2

我正在使用ASP.NET Core 2.2构建的项目。主要解决方案包含多个项目,其中包括API,Web和其他类库。

我们已经使用SignalR在API项目和Web项目之间显示共享的消息/通知。例如,从API添加新员工记录应调用SignalR Hub,所有Web客户端都应收到通知。

这是我们项目的当前结构

|- API
|- Hub_ClassLibrary
|- Services
|- Web

流量:

Web > services > Hub 
API > services > Hub

集线器:

public class NotificationHub : Hub
{
    public async Task SendNotification(List<DocumentHistoryModel> notifications)
    {
        await Clients.All.SendAsync(Constants.ReceiveNotification, notifications);
    }
}

Web启动类:

app.UseSignalR(routes =>
{
    routes.MapHub<NotificationHub>("/notificationHub");
});

API启动类

app.UseSignalR(routes =>
{
    routes.MapHub<NotificationHub>("/notificationHub");
});

服务

private readonly IHubContext<NotificationHub> _hubContext;

public MyService(IHubContext<NotificationHub> hubContext)
{
    _hubContext = hubContext;
}

await _hubContext.Clients.All.SendAsync(ReceiveNotification, notifications);

问题是,我可以从Web发送和接收通知,但是通过api调用,Web没有收到任何通知。我认为问题在于它为每个项目创建了两个单独的连接,但是处理这种情况的最佳方法是什么?

编辑:在此api代码中,我可以获得连接ID和状态“已连接”,但是,Web仍未收到任何通知。也尝试过connection.InvokeAsync

var connection = new HubConnectionBuilder()
     .WithUrl("https://localhost:44330/notificationhub")
     .Build();

connection.StartAsync().ContinueWith(task =>
{
    if (task.IsFaulted)
    {                    
    }
    else
    {                    
        connection.SendAsync("UpdateDashboardCount", "hello world");
    }
}).Wait();

enter image description here

c# asp.net-core signalr asp.net-core-2.2
2个回答
0
投票

为此,最好使用pub / sub系统,例如Rabbitmq,kafka,akka.net,redis。但是,如果您坚持使用Signalr,最好的解决方案是与Signalr创建一个消息协调器并将其设置为Signalr主机,然后您的其他服务将充当客户端,将其消息发送到该Signalr服务并从该服务接收消息(您可以甚至可能需要实施一些逻辑,使您的按摩协调员将消息排队并使其持久化)此外,如果您可以迁移到asp.net core 3,则还应该明确检查grpc双向流传输,这可以完美解决您的问题。


0
投票

您可以将HubContext注入API项目中的控制器,然后尝试点击'InvokeAsync'

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.