在公共交通框架上的 Azure 队列接收器中获取 Api 响应
Get Api Response in Azure Queue Receiver on Masstransit framework
我是 Masstransit 的新手,有一次卡得很厉害。
下面是我的架构。
1) 我有 WebApi 控制器,它使用以下代码在 azure 队列中发送消息。
if (_dipDecisionSendersEnabled)
{
//If MassTransit Senders are enabled, send a "ApplicationUpgradeDecision" message to the Message Bus
Task<bool> downloading = SendDipDecisionMessagetoMessageBus(applicationNumber,
systemId.ToString(),
decisionId, externalApplicationReference);
done = await downloading.ConfigureAwait(false);
}
#endregion MassTransit Sender DipDecisionUpdated
try
{
if (done)
{
response = await UpdateDIPDecision(applicationNumber, systemId, decisionId, externalApplicationReference).ConfigureAwait(false);
}
}
这里我正在做的是在队列中推送消息后,我正在呼叫第 3 方更新决策并从他们那里得到回复作为回应。上面的代码在 EventController class.
2) 现在我在其他文件中有如下接收器 DipConsumer.cs 如下
public async Task Consume(ConsumeContext<DipDecision> context)
{
await _service.ServiceTheThing(context.Message.ApplicationNumber).ConfigureAwait(true);
await context.RespondAsync<IMassTransit>(new
{
applicationNumber = $"DipDecision - Consumer Received DIP Decision for application number : {context.Message.ApplicationNumber}",
systemId = $"DipDecision - Consumer Received DIP Decision against system : {context.Message.SystemId}",
decisionId = $"DipDecision - Consumer Received DIP Decision against system : {context.Message.DecisionId}",
externalApplicationReference = $"DipDecision - Consumer Received DIP Decision from external application reference number : {context.Message.ExternalApplicationReference}"
}).ConfigureAwait(true);
}
我希望仅当我在 EventController 中的响应变量中得到 "Ok" 作为响应时才执行我的消费者。但是我无法将我的 webapi 响应注入接收者上下文。
请各位指点或指点。
要在 API 控制器中等待响应,您可以使用请求客户端,如 documentation 中所述。
本质上,您的控制器将等待响应,然后继续处理。
public class RequestController :
Controller
{
IRequestClient<CheckOrderStatus> _client;
public RequestController(IRequestClient<CheckOrderStatus> client)
{
_client = client;
}
public async Task<ActionResult> Get(string id)
{
var response = await _client.GetResponse<OrderStatusResult>(new {OrderId = id});
// do the rest of the thing, based upon response.Ok
return View(response.Message);
}
}
上面链接的文档还展示了如何配置容器。
如果您希望有一个单独的控制器方法调用,您可以创建一个响应事件的消费者(您在上面概述的消费者随后将发布该事件,而不是调用响应),该事件将使用 HTTP 客户端调用你的控制器方法。
我是 Masstransit 的新手,有一次卡得很厉害。
下面是我的架构。
1) 我有 WebApi 控制器,它使用以下代码在 azure 队列中发送消息。
if (_dipDecisionSendersEnabled)
{
//If MassTransit Senders are enabled, send a "ApplicationUpgradeDecision" message to the Message Bus
Task<bool> downloading = SendDipDecisionMessagetoMessageBus(applicationNumber,
systemId.ToString(),
decisionId, externalApplicationReference);
done = await downloading.ConfigureAwait(false);
}
#endregion MassTransit Sender DipDecisionUpdated
try
{
if (done)
{
response = await UpdateDIPDecision(applicationNumber, systemId, decisionId, externalApplicationReference).ConfigureAwait(false);
}
}
这里我正在做的是在队列中推送消息后,我正在呼叫第 3 方更新决策并从他们那里得到回复作为回应。上面的代码在 EventController class.
2) 现在我在其他文件中有如下接收器 DipConsumer.cs 如下
public async Task Consume(ConsumeContext<DipDecision> context)
{
await _service.ServiceTheThing(context.Message.ApplicationNumber).ConfigureAwait(true);
await context.RespondAsync<IMassTransit>(new
{
applicationNumber = $"DipDecision - Consumer Received DIP Decision for application number : {context.Message.ApplicationNumber}",
systemId = $"DipDecision - Consumer Received DIP Decision against system : {context.Message.SystemId}",
decisionId = $"DipDecision - Consumer Received DIP Decision against system : {context.Message.DecisionId}",
externalApplicationReference = $"DipDecision - Consumer Received DIP Decision from external application reference number : {context.Message.ExternalApplicationReference}"
}).ConfigureAwait(true);
}
我希望仅当我在 EventController 中的响应变量中得到 "Ok" 作为响应时才执行我的消费者。但是我无法将我的 webapi 响应注入接收者上下文。
请各位指点或指点。
要在 API 控制器中等待响应,您可以使用请求客户端,如 documentation 中所述。
本质上,您的控制器将等待响应,然后继续处理。
public class RequestController :
Controller
{
IRequestClient<CheckOrderStatus> _client;
public RequestController(IRequestClient<CheckOrderStatus> client)
{
_client = client;
}
public async Task<ActionResult> Get(string id)
{
var response = await _client.GetResponse<OrderStatusResult>(new {OrderId = id});
// do the rest of the thing, based upon response.Ok
return View(response.Message);
}
}
上面链接的文档还展示了如何配置容器。
如果您希望有一个单独的控制器方法调用,您可以创建一个响应事件的消费者(您在上面概述的消费者随后将发布该事件,而不是调用响应),该事件将使用 HTTP 客户端调用你的控制器方法。