问题
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