首页 > 解决方案 > MassTransit RespondAsync() 引发 Microsoft.Azure.ServiceBus.MessagingEntityNotFoundException

问题描述

具有以下启动配置的 Web API:

services.AddMassTransit(
 x =>
 {
    x.AddBus(provider => this.CreateBus(provider));
    string queueName = settings.InputQueueName;
    var serviceBusUri = new Uri($"{settings.ServiceBusUri}/{queueName}");
    CommandEndpointConvention.MapAll(serviceBusUri);
    x.AddRequestClient<CreateThingCommand>(serviceBusUri);
});

将请求客户端注入控制器:

IRequestClient<CreateThingCommand> requestClient;

..构造一个命令并将该命令放在 Azure 服务总线上并等待响应:

var busResponse = await this.requestClient.GetResponse<CommandResponse>(command);

触发 Azure 函数并处理请求,然后调用RespondAsync

 await context.RespondAsync(response);

每次,该RespondAsync行都会引发以下异常。

Microsoft.Azure.ServiceBus.MessagingEntityNotFoundException: 'Put token failed. status-code: 404, status-description: The messaging entity 'sb://dev-xxxxx-bus.servicebus.windows.net/DESKTOPXXYYZZ_iisexpress_bus_xrsyyydbnyyx9a4pbdcxkg6udd' could not be found. To know more visit https://aka.ms/sbResourceMgrExceptions

我希望将响应消息放在input-queue并传递给调用者。

我究竟做错了什么?

有一些值contextRespondAsync

在此处输入图像描述

我无法在 Service Bus Explorer 中观察到临时队列的创建。

在此处输入图像描述

MessagingEntityNotFoundException抛出后,GetResponse调用最终在 30 秒后超时,表明它仍在运行,在范围内并正在侦听。

标签: azure-functionsazureservicebusmasstransit

解决方案


使用请求客户端时,responseAddress 设置为总线端点的地址,这是一个临时的、自动删除的队列。请求客户端在发送请求时动态地将响应/故障处理程序连接到总线端点,并在GetResponse完成后断开这些处理程序。

另外一点,您可以使用短地址轻松地为您的服务端点创建目标地址。例如:

x.AddRequestClient<CreateThingCommand>(new Uri($"queue:{queueName}"));

如果RespondAsync抛出 anEntityNotFoundException那么可以肯定地说原始请求客户端不再连接/侦听响应。


推荐阅读