![](/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.