Masstransit EndpointConvention Azure服务总线 [英] Masstransit EndpointConvention Azure Service Bus

查看:51
本文介绍了Masstransit EndpointConvention Azure服务总线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否做错了什么,我期望 MassTransit 将自动注册 EndpointConvention 中的> ReceiveEndpoints .

I'm wondering if I'm doing something wrong, I expected MassTransit would automatically register ReceiveEndpoints in the EndpointConvention.

示例代码:

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")); 不需要在不指定队列的情况下发送到总线名字,还是我想念什么?

I thought this line EndpointConvention.Map<MyRequest>(new Uri("queue:queue-name")); wouldn't be necessary to allow sending to the bus without specifing the queue name, or am I missing something?

等待总线.发送< MyRequest>(新{...});

推荐答案

EndpointConvention 是一种便捷的方法,允许在不指定端点地址的情况下使用 Send .MassTransit中没有任何东西可以自动配置此设置,因为坦率地说,我不使用它.而且我也不认为其他任何人也应该这样做.也就是说,人们出于任何原因都会使用它.

The EndpointConvention is a convenience method that allows the use of Send without specifying the endpoint address. There is nothing in MassTransit that will automatically configured this because, frankly, I don't use it. And I don't think anyone else should either. That stated, people do use it for whatever reason.

首先,考虑一下后果-如果每种消息类型都注册为端点约定,那么在多个端点上发布和使用的消息又如何呢?那行不通.

First, think about the ramifications - if every message type was registered as an endpoint convention, what about messages that are published and consumed on multiple endpoints? That wouldn't work.

因此,如果您要按消息类型路由消息,MassTransit具有此功能.它称为 Publish ,效果很好.

So, if you want to route messages by message type, MassTransit has a feature for that. It's called Publish and it works great.

但是,等等,这是一个命令,命令应该已发送.

这是事实,但是,如果您控制应用程序,并且知道代码库中只有一个使用者消耗 KickTheTiresAndLightTheFires 消息约定,那么发布和发送一样好而且您不需要知道地址!

That is true, however, if you are in control of the application and you know that there is only one consumer in your code base that consumes the KickTheTiresAndLightTheFires message contract, publish is as good as send and you don't need to know the address!

好的,这是详细信息.使用ConfigureEndpoints()时,MassTransit使用 IEndpointNameFormatter 生成基于通过 AddConsumer AddSagaStateMachine 等注册的类型的接收端点队列名称.如果您要使用发送而不指定目标地址,则可以使用同一接口注册自己的端点约定.

Okay, fine, here are the details. When using ConfigureEndpoints(), MassTransit uses the IEndpointNameFormatter to generate the receive endpoint queue names based upon the types registered via AddConsumer, AddSagaStateMachine, etc. and that same interface can be used to register your own endpoint conventions if you want to use Send without specifying a destination address.

您当然可以将消费者和消息类型的知识结合起来,但这就是您的要求.您已经在处理魔术(通过使用不带显式目标的发送"),为什么不正确呢?

You are, of course, coupling the knowledge of your consumer and message types, but that's your call. You're already dealing with magic (by using Send without an explicit destination) so why not right?

string queueName = formatter.Consumer<T>()

将该字符串用于该使用者中的消息类型,作为 $" queue:{queueName}" 地址,并将其注册到EndpointConvention.

Use that string for the message types in that consumer as a $"queue:{queueName}" address and register it on the EndpointConvention.

或者,您只需使用 Publish .

这篇关于Masstransit EndpointConvention Azure服务总线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆