5/29/2023 0 Comments Masstransit connectconsumerGet started by reading through the documentation. Fully-supported, widely-adopted, a complete end-to-end solution.Observability via Open Telemetry (OTEL).Write once, then deploy using RabbitMQ, Azure Service Bus, and Amazon SQS.This way we can have generic consumers and definitions without having to add a new class for every command contract we have.MassTransit provides a developer-focused, modern platform for creating distributed applications without complexity. ServiceConfigurator.AddConsumer(consumerType, consumerDefinitionType) īecause of the automatic discovery, we were already in the realm of reflection, so this seemed like an acceptable solution. Type consumerDefinitionType = typeof(CommandConsumerDefinition).MakeGenericType(registerCommandConsumerAttribute.CommandType) If (attributes.Length ).MakeGenericType(registerCommandConsumerAttribute.CommandType) Var attributes = type.GetCustomAttributes(typeof(RegisterCommandConsumerAttribute), false) public static IServiceCollectionConfigurator ConfigureMassTransitConsumers(this IServiceCollectionConfigurator serviceConfigurator, Assembly assembly)įoreach (var type in assembly.GetTypes()) In the end, I also used a "marker" attribute that would hold the type of the command contract, so they could be discovered rather than having to be entered as a parameter at startup. This allowed MassTransit to wire up the configuration in the expected manner. Making the CommandConsumerDefinition generic allowed me to use reflection to construct both types in the same way at runtime. How am I supposed to add additional configuration to a Consumer for which I don't know the type at compile time?Ĭhris' answer put me on the path to a working solution. I tried adding a marker class CommandConsumer: IConsumer to the CommandConsumer and making CommandConsumerDefinition : ConsumerDefinition, and changing the above to cfg.AddConsumer(consumerType, typeof(CommandConsumerDefinition)), but that doesn't work as the ConfigureConsumer override is never hit. This again works, but what I can't figure out is how to add the additional configuration to the registration the overload that takes an Action is only available when using the generic signature, which you can't use directly when you only have the Type available. Which would be called as services.ConfigureMassTransit(typeof(CommandConsumer)) The method (currently) looks a bit like this public static IServiceCollection ConfigureMassTransit(this IServiceCollection services, params Type consumerTypes)įoreach (var consumerType in consumerTypes)Ĭfg.AddBus(context => (config => This all worked great, so I moved on to the next step, namely to extract the consumer registration(s) to a common helper method and this is where I'm a bit stumped. Which I could then easily register through the Microsoft DI thusly: cfg.AddConsumer>(x => some configuration.) Now it was relatively easy to make a generic Consumer that would do some boiler plating and would hand off the work to the ready command handler, requested from the DI container. We had already implemented a kind of command handler that had generic implementations, like this: public class M圜ommandHandler: CommandHandlerBase I'm in the process of integrating a messaging broker within our existing application using MassTransit.
0 Comments
Leave a Reply. |