如何使用 SignalR 获得延迟响应(控制器调用后台服务)?

How to get delayed response with SignalR (Controller calls Background Service)?

我阅读了有关 SignalR 的文档和教程。但是我不明白在我的情况下如何得到回应。

.Net5.

Reactjs 向 Controller 发送请求。

控制器动作:

[HttpGet("compared")]
public async Task<ActionResult> GetComparedText([FromQuery] PrevDraftVersionDto dto) =>
            Ok(await _mediator.Send(new GetComparedTextQuery(dto.InitialDraftId, dto.DraftId, dto.CurrVer, dto.PrevVer)));

GetComparedTextHandler

public class GetComparedTextHandler : IRequestHandler<GetComparedTextQuery, ComparedDraftDto>
    {
        private readonly AppDbContext _appDbContext;
        private readonly TpaChannel _tpaChannel;

        public GetComparedTextHandler(AppDbContext appDbContext, TpaChannel tpaChannel)
        {
            _appDbContext = appDbContext;
            _tpaChannel = tpaChannel;
        }

 public async Task<ComparedDraftDto> Handle(GetComparedTextQuery request, CancellationToken cancellationToken)
{
   var edDraftComparison = ...// get compared text from repository
   if (edDraftComparison == null) {
      Repository doesn't have record yet.

   // 1) Adding to repository request.InitialDraftId, request.DraftId
   .........

  // 2) Create Channel for background Service
   message = "added_to_queue";
                var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
                cts.CancelAfter(TimeSpan.FromSeconds(180)); // wait max  seconds
                var versionDto = new VersionDto(request.InitialDraftId, request.DraftId, request.CurrVer, request.PrevVer);
                await _tpaChannel.AddVersionsAsync(versionDto, cts.Token);

} // if (edDraftComparison == null)
else {...}
return ....
}

处理程序向控制器和 Web 客户端发送响应。但毕竟 tpaChannel 启动了后台服务。它更新了存储库中的一条记录(通过 InitialDraftId)。

然后我需要使用 SignalR 获取更新的记录。

看了教程,说Controller action可以加载signalR:

hubContext.Clients.User(UserId).SendAsync(
            "receiveUpdatedText",
            new Article<LikeRelatedPayload>
            {
                DraftId = draftId,
                Payload = payload
            }
        );

但这是错误的想法,因为,我必须在后台服务作业后从存储库中获取记录。

我需要在哪里创建一个 singnalR 服务,它将记录从后台服务发送到当前用户?

抱歉语言问题,我很难用英语解释情况。

在你相应的后台服务中使用IHubContext怎么样。我不知道我们的后台服务是什么样的...所以我只描述一些虚拟服务

public abstract class DummyProcessor : Microsoft.Extensions.Hosting.BackgroundService
{
    IHubContext<MyHub> _myHub;

    protected DummyProcessor(IHubContext<MyHub> myHub)
    {
        ServiceScopeFactory = serviceScopeFactory;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        // some logic...
        await _myHub.Clients.All.SendAsync("As we usually do");
    }
}