简体   繁体   English

MassTransit 自动添加消费者

[英]MassTransit automatically add consumers

Im using MassTransit to connect to our RabbitMQ.我使用 MassTransit 连接到我们的 RabbitMQ。 Currently im working on generalizing the code over the 15 solution that uses it.目前我正在努力将代码推广到使用它的 15 个解决方案上。 But i hit road block on the setup part.但是我在设置部分遇到了障碍。 Original it looked like this:原来它看起来像这样:

  services.AddMassTransit(c =>
            {
                c.AddConsumer<MoveMouldConsumer>();
                c.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(cfg =>
                {
                    cfg.UseHealthCheck(provider);
                    cfg.Host(MassTransitHelper.CreateRabbitMQEndpointUri(messageQueueSettings), h =>
                    {
                        h.Username(messageQueueSettings.Username);
                        h.Password(messageQueueSettings.Password);
                    });

                    cfg.PrefetchCount = (ushort) messageQueueSettings.PrefetchCount;
                    cfg.MessageTopology.SetEntityNameFormatter(new EntityNameFormatter());
                    cfg.ExchangeType = ExchangeType.Direct;
                    
                    cfg.ReceiveEndpoint(NameFormatter.FormatQueueName<MouldMovementEvent>("serviceName"), e =>
                    {
                        e.Bind(NameFormatter.FormatExchangeName<MouldMovementEvent>()); //Bind to Exchange
                        e.UseMessageRetry(r => r.Incremental(5, 5.Seconds(), 10.Seconds()));
                        e.ExclusiveConsumer = false;
                        e.PrefetchCount = messageQueueSettings.PrefetchCount;
                        e.Consumer<MoveMouldConsumer>(provider);
                        e.UseCircuitBreaker(cb =>
                        {
                            cb.TripThreshold = 15;
                            cb.ActiveThreshold = 10;
                            cb.ResetInterval = 5.Minutes();
                        });
                    });
                }));
            });

I have so far managed to generalize it to this:到目前为止,我已经设法将其概括为:

services.AddMassTransit(c =>
            {
                c.AddConsumer<MoveMouldConsumer>();
                c.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(cfg =>
                {
                    cfg.UseHealthCheck(provider);
                    MassTransitSetup.BasicSetup(cfg, messageQueueSettings);
                    MassTransitSetup.InitializeConsumer<MouldMovementEvent, MoveMouldConsumer>(cfg, messageQueueSettings, provider, 5, "report");
                }));
            }); 

But the part im missing is that i like to be able to automatically add all consumers by getting from Assemblies which i do like so:但是我缺少的部分是我喜欢能够通过从我喜欢的程序集中获取来自动添加所有消费者:

var types = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(s => s.GetTypes())
            .Where(p => typeof(IConsumer).IsAssignableFrom(p) && p.IsClass && !p.IsAbstract && p.Namespace!.Contains("MST.Hepper.")).ToList();

But my problem is how to iterate through and add the c.AddConsumer<>();但我的问题是如何迭代并添加c.AddConsumer<>(); or MassTransitSetup.InitializeConsumer<,>MassTransitSetup.InitializeConsumer<,>

MassTransit has methods to configure consumer automatically based on assemblies, types, etc. MassTransit 具有根据程序集、类型等自动配置消费者的方法。

services.AddMassTransit(x =>
{
    // Add a single consumer
    x.AddConsumer<SubmitOrderConsumer>(typeof(SubmitOrderConsumerDefinition));

    // Add a single consumer by type
    x.AddConsumer(typeof(SubmitOrderConsumer), typeof(SubmitOrderConsumerDefinition));

    // Add all consumers in the specified assembly
    x.AddConsumers(typeof(SubmitOrderConsumer).Assembly);

    // Add all consumers in the namespace containing the specified type
    x.AddConsumersFromNamespaceContaining<SubmitOrderConsumer>();
});

Related Documentation Link相关文档链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM