NServiceBus 6 回调客户端在请求处理程序失败时永远不会收到回调



使用 NServiceBus 6 的回调功能,我发现没有办法提醒客户端请求处理程序失败。请求处理程序将完成所有可恢复性步骤,并最终将消息放入错误队列中。同时,客户只是坐在那里等待它的回复。

// Client code (e.g. in an MVC Controller)
var message = new FooRequest();
var response = await endpoint.Request<FooReponse>(message);
// Handler code
public class FooRequestHandler : IHandleMessages<FooRequest>
{
Task Handle(FooRequest message, IMessageHandlerContext context)
{
throw new Exception("Fails before the reply");
return context.Reply(new FooResponse());
}
}

在上述情况下,如何让 MVC 控制器/调用代码知道处理程序已永久失败?

这是设计使然。从客户端的角度来看,我建议您始终传入一个CancellationToken,该定义允许请求者等待请求调用回复的时间。

var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(5)); // your SLA timeout
var message = new Message();
try
{
var response = await endpoint.Request<FooRequest>(message, cancellationTokenSource.Token)
.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// Exception that is raised when the CancellationTokenSource is canceled
}

客户端域定义允许客户端请求异步等待应答的时间。有关取消的更多信息,请参阅 https://docs.particular.net/nservicebus/messaging/callbacks?version=callbacks_3#cancellation

最新更新