Custom consumer implementation factory with Microsoft Dependency Injection

Emir Kljucanin

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.

Chris Patterson

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?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Dependency Injection lifetimes for a Factory

How Can a Custom Controller Factory Relate to Dependency Injection?

How to Implement ViewController custom init using dependency injection and factory patterns?

Microsoft Dependency Injection Documentation

Which dependency injection "factory" to use?

Generic Interface dependency injection into factory

Angularjs dependency injection inside factory

PHP Dependency Injection Container With Factory

Dependency injection inside model factory

Dependency injection in custom attribute

Custom DataAnnotation and Dependency Injection

Jakarta EE Dependency Injection implementation

Producer/consumer Pattern with dependency injection error

Angularjs custom filter and dependency injection

Unity vs custom dependency injection?

Dependency Injection for custom validation attributes

Dependency Injection in EventFlow custom output

Spring Dependency injection with factory(dynamic value)

Dependency Injection, need factory like functionality

dependency injection in factory method causes NullPointerException

Angularjs dependency injection, array, $inject, factory

Using a Strategy and Factory Pattern with Dependency Injection

Handle dependency injection with factory method DP

Should I choose Dependency Injection or Factory Pattern

Why Dependency injection of a factory inside another factory failing?

Angular Dependency Injection -Can't Instantiate Factory/ Undefined Factory

Accessing Concrete Implementation of Interface, Dependency Injection

Spring dependency injection with multiple implementation for an interface

Simple Injector Dependency injection in a IPackage class implementation