gpt4 book ai didi

c# - MassTransit Consumer 为注册的消费者抛出 "A convention for the message type {type} was not found"异常

转载 作者:行者123 更新时间:2023-12-04 12:24:01 25 4
gpt4 key购买 nike

我有一个简单的服务,它在 HTTP 端点接受请求。端点的操作使用 MassTransit 发布一个事件,提醒消费者实体已更新。我的已发布事件的使用者然后向我的同步使用者发送同步请求以完成工作单元。
但是,当事件使用者尝试分派(dispatch)请求时,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

关于c# - MassTransit Consumer 为注册的消费者抛出 "A convention for the message type {type} was not found"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62759029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com