【问题标题】:How to get delayed response with SignalR (Controller calls Background Service)?如何使用 SignalR 获得延迟响应(控制器调用后台服务)?
【发布时间】:2021-11-28 05:49:15
【问题描述】:

我阅读了有关 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 ....
}

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

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

我阅读了教程,Controller 动作可以加载 signalR:

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

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

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

对不起语言,我很难用英语解释情况。

【问题讨论】:

    标签: asp.net-core signalr backgroundworker


    【解决方案1】:

    在你相应的后台服务中使用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");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-19
      • 2016-10-09
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2013-06-25
      • 2021-05-20
      相关资源
      最近更新 更多