使用SignalR& amp;交流网络应用程序和api项目StackExchangeRedis [英] Communicate web app and api project using SignalR & StackExchangeRedis

查看:84
本文介绍了使用SignalR& amp;交流网络应用程序和api项目StackExchangeRedis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

为了使其通信,我们将Radis与SignalR一起使用

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;
});

当我尝试在Radis中获取客户列表时,

When I try to get client list in Radis,

现在,如何从Web应用程序和API发送和接收消息?

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

推荐答案

您共享的代码用于

如何从Web应用程序和API发送和接收消息?

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

要达到上述要求,您可以执行以下操作:

To achieve above requirement, you can do:

对于您的API项目和中心服务器,您可以在API控制器中注入IHubContext实例,然后可以在控制器操作中访问IHubContext实例并将消息/通知推送到客户端.

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).

对于您的Web应用,您可以构建并实现SignalR JavaScript客户端功能来接收通知.

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

这篇关于使用SignalR& amp;交流网络应用程序和api项目StackExchangeRedis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆