Problem registering two instances of IEnumerable<Type>

73 views Asked by At

I have a problem with SimpleInjector and I don't know how to resolve it. I'm getting the following error:

The configuration is invalid. Creating the instance for type IMiddleware failed. The constructor of type LogContextMiddleware contains the parameter with name 'controllerTypeExclusionList' and type IEnumerable<Type>, but IEnumerable<Type> is not registered. For IEnumerable<Type> to be resolved, it must be registered in the container. You can use one of the Container.Collection.Register overloads to register a collection of Type types, or one of the Container.Collection.Append overloads to append a single registration to a collection. In case you intend to resolve an empty collection of Type elements, make sure you register an empty collection; Simple Injector requires a call to Container.Collection.Register to be made, even in the absence of any instances. Please see https://simpleinjector.org/collections for more information about registering and resolving collections.

Can anyone help me? Thanks in advance, my code is:

// Log Context Middleware
var controllerTypeLogExclusionList = new List<Type> { typeof(StatusController) };
container.Register<LogContextMiddleware>(
   instanceCreator: () => new LogContextMiddleware(
       controllerTypeExclusionList: controllerTypeLogExclusionList,
   logger: container.GetInstance<ILogger>()),
   lifestyle: Lifestyle.Transient);

// Authorization Middleware
var controllerTypeAuthorizationExclusionList = new List<Type>
{
    typeof(StatusController),
    typeof(PruebaSinAuthController)
};
container.Register<AuthorizationMiddleware<AppToken>>(
   instanceCreator: () => new AuthorizationMiddleware<AppToken>(
       appTokenRepository: container.GetInstance<IAppTokenRepository<AppToken>>(),
       controllerTypeExclusionList: controllerTypeAuthorizationExclusionList,
   logger: container.GetInstance<ILogger>()),
   lifestyle: Lifestyle.Transient);

controllerTypeExclusionList in both cases are IEnumerable<Type>

EDIT 1:

My code was:

var controllerTypeExclusionList = new List<Type> {
   typeof(StatusController),
   typeof(PruebaSinAuthController) };

container.RegisterInstance<IEnumerable<Type>>(controllerTypeExclusionList);

With this option both classes share the same list because I'm calling to container.GetInstance<IEnumerable<Type>>()

The main problem is that I need different lists for each Middleware. I hope this helps clarify my problem

EDIT 2

LogContextMiddleware class

public class LogContextMiddleware : IMiddleware
{
    private readonly IEnumerable<Type> controllerTypeExclusionList;
    private readonly ILogger logger;

    public LogContextMiddleware(
        IEnumerable<Type> controllerTypeExclusionList,
        ILogger logger)
    {
        this.controllerTypeExclusionList = controllerTypeExclusionList;
        this.logger = logger;
    }

Thanks @Steven

0

There are 0 answers