MassTransit – Any way to inject parametric settings from appsettings to ConfigureConsumer?

Total
1
Shares

As explained here, I’d like to set batch settings from appSettings.json. Is there any way to do this using ConfigureConsumer by DI?

protected override void ConfigureConsumer(IReceiveEndpointConfigurator endpointConfigurator,
            IConsumerConfigurator<OrderAuditConsumer> consumerConfigurator)
        {
            consumerConfigurator.Options<BatchOptions>(options => options
                .SetMessageLimit(100)
                .SetTimeLimit(1000)
                .SetConcurrencyLimit(10));
        }

Solution

You can add dependencies to the constructor of the ConsumerDefinition (the class that has the method shown in the question) as the definitions are resolved from the container during bus configuration. It’s a pretty common use case, and easily handled that way.

The extensions IOptions types are good for this, which are part of .NET (Microsoft.Extensions.Configuration I think).

Leave a Reply

Your email address will not be published. Required fields are marked *