SignalR with web Api 作为客户端 [.Net Core]

SignalR with web Api as client [.Net Core]

我有一个 signalR 服务器工作正常,一些客户端(前端页面)正确接收来自服务器的消息或事件。

但是,现在其中一个客户端是 web api。但是我不确定如何进行相应的配置来实现网络api不断监听和接收来自服务器signalR的调用。

注意:SignalR 服务器和客户端在 .Net Core 上。

我找过这个场景,但没找到。

Microsoft 的 example 是某种“类似”的示例,但它使用 windows 形式。所以,我有点迷路了。

有人知道如何获得它吗?我会感激的。

我建议使用该 Microsoft 示例,但将其实现为后台服务。您只需在启动时使用 SignalR.Client 包和 运行 实现后台服务。然后您将拥有一个正在侦听 SignalR 调用的服务。

services.AddHostedService<SignalRService>();
public sealed class SignalRService : BackgroundService
{
    /// <summary>
    /// Executes the background service.
    /// </summary>
    /// <param name="cancellationToken">
    /// The cancellation token. This token signals that the background service is being stopped.
    /// </param>
    /// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
    /// <remarks>This method is automatically triggered after the background service is started.</remarks>
    protected override async Task ExecuteAsync(CancellationToken cancellationToken)
    {
        // your SignalR code
    }
}