【问题标题】:Im usisng Masstransit Requst Response in asp.net core when I Send Request before get Response Request Canceling and throw Exception当我在获取响应请求取消并抛出异常之前发送请求时,我在 asp.net 核心中使用 Masstransit 请求响应
【发布时间】:2019-01-31 14:34:41
【问题描述】:

我在 Asp.NetCore 中使用 Masstransit Request-Response 当我在收到响应之前发送 Request 时,Request 会被 Cancelled 并抛出此异常

System.Threading.Tasks.TaskCanceledException: A task was canceled.

我已经下载了masstransit 代码并用它调试我的项目。 我了解ClientRequestHandle 在收到我的回复之前处理和取消请求。

我检查了 Consumer 方面,它成功运行并按预期发送响应。

【问题讨论】:

  • 这是我的代码:尝试 { using (var handle = _getOrderInfoRequestClient.Create(new GetThirdPersonInsuranceProductAmountCommand(productId))) { var response = await handle.GetResponse>(); if (!response.Message.IsSuccess) throw new DomainException(response.Message.Message);返回 Convert.ToDecimal(response.Message.ResponseValue.Value); } } 捕捉(异常前){ 抛出; }

标签: c# .net-core masstransit request-response


【解决方案1】:

使用RequestHandle<T>时,需要保持对句柄的引用,直到请求完成并收到响应(或发生超时或故障)。

您可以通过使用单行方法来简化:

var response = await requestClient.GetResponse<T>(request);

如果您需要在请求中添加内容,例如标头等,那么您需要保留句柄直到完成。

using(var handle = requestClient.Create(request))
{
    var response = await handle.GetResponse<T>();
}

【讨论】:

  • 感谢 Chris。但恢复响应后抛出异常。
猜你喜欢
  • 1970-01-01
  • 2019-02-23
  • 2023-02-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多