![](/img/trans.png)
[英]MassTransit republish message for another consumer when a consumer throws exception
[英]MassTransit Consumer throws "A convention for the message type {type} was not found" exception for a registered consumer
我有一個簡單的服務,可以在 HTTP 端點接受請求。 端點的操作使用 MassTransit 發布事件,提醒消費者實體已更新。 我發布事件的消費者然后向我的同步消費者發送同步請求以完成工作單元。
但是,當事件消費者嘗試分派請求時,MassTransit 會引發異常,指出A convention for the message type {type} was not found
。 這似乎意味着我的消費者沒有注冊,但我相信它是。
這是我的Startup
注冊碼:
public void ConfigureServices(IServiceCollection services)
{
// -- removed non MassTransit code
services.AddMassTransit(x =>
{
x.SetKebabCaseEndpointNameFormatter();
x.AddConsumers(typeof(Startup).Assembly);
x.UsingRabbitMq((context, cfg) =>
{
var rabbitMq = Configuration.GetSection("RabbitMq");
var url = rabbitMq.GetValue<string>("Url");
var username = rabbitMq.GetValue<string>("Username");
var password = rabbitMq.GetValue<string>("Password");
cfg.Host(url, h =>
{
h.Username(username);
h.Password(password);
});
cfg.ConfigureEndpoints(context);
});
});
services.AddMassTransitHostedService();
}
我的 API controller 看起來像這樣:
[Route("~/api/[controller]")]
public class JobSchedulingController : ApiControllerBase
{
private readonly IPublishEndpoint _publishEndpoint;
public JobSchedulingController(IPublishEndpoint publishEndpoint)
{
_publishEndpoint = publishEndpoint;
}
[HttpPost]
public async Task<IActionResult> UpdateJob(JobSchedulingInputModel model)
{
await _publishEndpoint.Publish<JobSchedulerJobUpdated>(model);
return Ok();
}
}
這是事件消費者:
public class JobSchedulerJobUpdatedConsumer
: IConsumer<JobSchedulerJobUpdated>
{
public async Task Consume(
ConsumeContext<JobSchedulerJobUpdated> context)
{
await context.Send<SyncJobSchedulingToLacrm>(context.Message);
}
}
最后,同步請求消費者:
public class SyncJobSchedulingToLacrmConsumer
: IConsumer<SyncJobSchedulingToLacrm>
{
private readonly LacrmClient _client;
private readonly JobSchedulingContext _context;
public SyncJobSchedulingToLacrmConsumer(
LacrmClient client,
JobSchedulingContext context)
{
_client = client;
_context = context;
}
public async Task Consume(ConsumeContext<SyncJobSchedulingToLacrm> context)
{
await context.Publish<JobSchedulerSyncedToLacrm>(new
{
LacrmPipelineItemId = ""
});
}
}
我從事件消費者內部收到錯誤,它永遠不會到達同步消費者。 什么可能導致這種行為?
您正在調用Send這是一種方便的方法。
await context.Send<SyncJobSchedulingToLacrm>(context.Message);
如果您尚未為該消息類型配置 EndpointConvention,它將找不到。
我建議閱讀這個答案: https://stackoverflow.com/a/62714778/1882
我不確定在IConsumer
內,但通常您可以在構造函數中獲取IBus
(使用依賴注入)並調用GetPublishSendEndpoint<T>()
方法。
var sendEndpoint = await bus.GetPublishSendEndpoint<IMessage>();
await sendEndpoint.SendBatch<IMessage>(messages);
await sendEndpoint.Send<IMessage>(message);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.