我是Masstransit的新手用户,一时陷入严重困境。
下面是我的体系结构。
1)我有WebApi COntroller,它正在使用下面的代码在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);
}
}
这里我正在做的是在将消息推送到队列后,我打电话给第三方以更新决策并得到他们的答复。上面的代码在EventController类中。
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中的响应变量中获得“确定”作为响应时才执行我的使用者。但是我无法将我的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);
}
}
上面的链接文档还显示了如何配置容器。
[如果您想单独调用一个控制器方法,则可以创建一个使用者来响应一个事件(使用上面概述的使用者然后将其发布,而不是调用Respond),该事件将使用HTTP客户端来调用您的控制器方法。