Communicate web app and api project using SignalR & StackExchangeRedis

时光怂恿深爱的人放手 提交于 2021-01-27 21:23:51

问题


I'm working with a project built with ASP.NET Core 2.2. The main solution contains multiple projects, which includes API, web and other class libraries.

We've used SignalR to displaying shared messages/notifications between the API project and the web project. For example, adding a new employee record from an API should call SignalR Hub and all the web client should received the notification.

To make it communicate, we have used Radis with SignalR

// SignalR Configuration
services.AddSignalR(option =>
{
    option.EnableDetailedErrors = true;
    option.KeepAliveInterval = TimeSpan.FromSeconds(3);
}).AddStackExchangeRedis(options =>
{
    options.Configuration.ChannelPrefix = "TestChannel";
    options.Configuration.EndPoints.Add("127.0.0.1", 6379);
    options.Configuration.ClientName = "ClientNameSignalR";
    options.Configuration.AllowAdmin = true;
});

When I try to get client list in Radis,

Now, how to sending and receiving messages from web app and API?


回答1:


The code you share is used to set up a Redis backplane for scaling out your ASP.NET Core SignalR app.

how to sending and receiving messages from web app and API?

To achieve above requirement, you can do:

For your API project and Hub server, you can inject an instance of IHubContext in your API controller, then you can access to an instance of IHubContext in controller action and push message/notification to client(s).

  • Access a SignalR IHubContext to send notifications to clients from outside a hub

For your web app, you can built and implement SignalR JavaScript client functionality to receive notifications.

  • SignalR JavaScript client


来源:https://stackoverflow.com/questions/60052889/communicate-web-app-and-api-project-using-signalr-stackexchangeredis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!