gpt4 book ai didi

Masstransit EndpointConvention Azure 服务总线

转载 作者:行者123 更新时间:2023-12-04 11:49:48 28 4
gpt4 key购买 nike

我想知道我是否做错了什么,我预计 MassTransit会自动注册 ReceiveEndpointsEndpointConvention .
示例代码:

services.AddMassTransit(x =>
{
x.AddServiceBusMessageScheduler();
x.AddConsumersFromNamespaceContaining<MyNamespace.MyRequestConsumer>();
x.UsingAzureServiceBus((context, cfg) =>
{
// Load the connection string from the configuration.
cfg.Host(context.GetRequiredService<IConfiguration>().GetValue<string>("ServiceBus:ConnectionString"));
cfg.UseServiceBusMessageScheduler();

// Without this line I'm getting an error complaining about no endpoint convention for x could be found.
EndpointConvention.Map<MyRequest>(new Uri("queue:queue-name"));

cfg.ReceiveEndpoint("queue-name", e =>
{
e.MaxConcurrentCalls = 1;
e.ConfigureConsumer<MyRequestConsumer>(context);
});

cfg.ConfigureEndpoints(context);
});
});
我以为这条线 EndpointConvention.Map<MyRequest>(new Uri("queue:queue-name"));没有必要在不指定队列名称的情况下允许发送到总线,或者我错过了什么? await bus.Send<MyRequest>(new { ...});

最佳答案

EndpointConvention是一种方便的方法,它允许在不指定端点地址的情况下使用 Send。 MassTransit 中没有任何东西会自动配置它,因为坦率地说,我不使用它。而且我认为其他人也不应该这样做。也就是说,人们确实出于任何原因使用它。
首先,考虑一下后果——如果每个消息类型都注册为端点约定,那么在多个端点上发布和使用的消息呢?那行不通。
因此,如果您想按消息类型路由消息,MassTransit 有一个功能。它叫Publish而且效果很好。

But wait, it's a command, and commands should be Sent.


但是,如果您控制应用程序并且您知道您的代码库中只有一个使用者使用 KickTheTiresAndLightTheFires,这是正确的。消息合约,发布和发送一样好,你不需要知道地址!
不,老兄,我想使用发送!
好的,好的,这是细节。使用 ConfigureEndpoints() 时,MassTransit 使用 IEndpointNameFormatter根据通过 AddConsumer 注册的类型生成接收端点队列名称, AddSagaStateMachine等,如果您想在不指定目标地址的情况下使用 Send,则可以使用相同的接口(interface)来注册您自己的端点约定。
当然,您将消费者和消息类型的知识结合起来,但这是您的要求。您已经在处理魔法(通过使用没有明确目的地的发送)那么为什么不正确呢?
string queueName = formatter.Consumer<T>()
将该字符串用于该消费者中的消息类型作为 $"queue:{queueName}"地址并将其注册到 EndpointConvention 上。
或者,您知道,只需使用 Publish .

关于Masstransit EndpointConvention Azure 服务总线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62713786/

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