配置通用消费者的公共交通

Gam*_*vil 5 c# dependency-injection masstransit

我正在使用 MassTransit 将消息传递代理集成到我们现有的应用程序中。我们已经实现了一种具有通用实现的命令处理程序,如下所示:

public class MyCommandHandler: CommandHandlerBase<MyCommand>
Run Code Online (Sandbox Code Playgroud)

现在,制作一个通用 Consumer 会相对容易一些,它会执行一些锅炉电镀,并将工作交给 DI 容器请求的就绪命令处理程序。

public class CommandConsumer<TCommand> : IConsumer<TCommand>
Run Code Online (Sandbox Code Playgroud)

然后我可以通过 Microsoft DI 轻松注册:

cfg.AddConsumer<CommandConsumer<MyCommand>>(x => some configuration...);
Run Code Online (Sandbox Code Playgroud)

这一切都很有效,所以我继续下一步,即将消费者注册提取到一个通用的帮助器方法中,这就是我有点困惑的地方。该方法(当前)看起来有点像这样

public static IServiceCollection ConfigureMassTransit(this IServiceCollection services, params Type[] consumerTypes)
{
        return 
            services.AddMassTransit(cfg =>
            {
                foreach (var consumerType in consumerTypes)
                {
                    cfg.AddConsumer(consumerType);
                }
                // or cfg.AddConsumers(consumerTypes);
                cfg.AddBus(context => Bus.Factory.CreateUsingRabbitMq(config =>
                {
                    var host = config.Host("localhost", "/",
                        h =>
                        {
                            h.Username("guest");
                            h.Password("guest");
                        });
                    config.ConfigureEndpoints(context);
                }));

            });
    }
Run Code Online (Sandbox Code Playgroud)

这将被称为services.ConfigureMassTransit(typeof(CommandConsumer<MyCommand>)); 这再次有效,但我不知道如何将附加配置添加到注册中;需要 Action 的重载仅在使用通用签名时才可用,当您只有可用的签名时,您不能直接使用通用签名Type。我尝试CommandConsumer: IConsumerCommandConsumer<TCommand>和making添加一个标记类CommandConsumerDefinition : ConsumerDefinition<CommandConsumer>,并将上面的内容更改为cfg.AddConsumer(consumerType, typeof(CommandConsumerDefinition));,但这不起作用,因为永远不会命中ConfigureConsumer 覆盖。

如果我在编译时不知道其类型,我该如何向消费者添加额外的配置?

Gam*_*vil 2

克里斯的回答让我走上了可行的解决方案。使 CommandConsumerDefinition 变得通用允许我在运行时使用反射以相同的方式构造这两种类型。这使得 MassTransit 能够以预期的方式连接配置。

最后,我还使用了一个“标记”属性来保存命令合约的类型,这样它们就可以被发现,而不必在启动时作为参数输入。

public static IServiceCollectionConfigurator ConfigureMassTransitConsumers(this IServiceCollectionConfigurator serviceConfigurator, Assembly assembly)
    {
        foreach (var type in assembly.GetTypes())
        {
            var attributes = type.GetCustomAttributes(typeof(RegisterCommandConsumerAttribute), false);
            if (attributes.Length <= 0) continue;
            foreach (var attribute in attributes)
            {
                if (attribute is RegisterCommandConsumerAttribute registerCommandConsumerAttribute)
                {
                    Type consumerType = typeof(CommandConsumer<>).MakeGenericType(registerCommandConsumerAttribute.CommandType);
                    Type consumerDefinitionType = typeof(CommandConsumerDefinition<>).MakeGenericType(registerCommandConsumerAttribute.CommandType);
                    serviceConfigurator.AddConsumer(consumerType, consumerDefinitionType);
                }
            }
        }
        return serviceConfigurator;
    }
Run Code Online (Sandbox Code Playgroud)

由于自动发现,我们已经处于反射领域,所以这似乎是一个可以接受的解决方案。这样我们就可以拥有通用的消费者和定义,而不必为我们拥有的每个命令合约添加一个新类。