emirkljucanin
emirkljucanin

Reputation: 814

Custom consumer implementation factory with Microsoft Dependency Injection

Is there a way to register Consumer like the service below:

services.AddTransient < IMyService > (provider => {
  return new MyServiceImplementation(2);
});

with AddConsumer<T>() method?

What I need is a custom implementation of Consumer factory because it will be injected with a different instance of one of its dependencies depending on the configuration.

Upvotes: 0

Views: 460

Answers (1)

Chris Patterson
Chris Patterson

Reputation: 33278

MassTransit registers the consumer added via AddConsumer as shown below:

collection.AddScoped<T>();

You're welcome to create your own register after configuring MassTransit, which should replace the one registered by MassTransit. In your example above, it could be something like:

services.AddScoped<TConsumer>(provider =>
{
    var options = provider.GetService<SomeOptions>();
    if (options.UseFirst)
        return new TConsumer(provider.GetRequiredService<Impl1>()
    
    return new TConsumer(provider.GetRequiredService<Impl2>()
});

You get the picture, right?

Upvotes: 1

Related Questions