5

Is it possible to unregister all implementations of an interface in Autofac?

My scenario:

I register two modules, one DefaultModule and later a SpecificModule if some conditions are fulfilled.

builder.RegisterModule(new DefaultModule());

if (someCondition)
{
   builder.RegisterModule(new SpecificModule());
}

The two modules register multiple named instances of an interface, let's call it ISomething.

Inside the Load function in DefaultModule:

builder.RegisterType<DefaultSomething1>().Named<ISomething>("DefaultSomething1").SingleInstance();
builder.RegisterType<DefaultSomething2>().Named<ISomething>("DefaultSomething2").SingleInstance();

Inside the Load function in SpecificModule:

builder.RegisterType<SpecificSomething1>().Named<ISomething>("SpecificSomething1").SingleInstance();
builder.RegisterType<SpecificSomething2>().Named<ISomething>("SpecificSomething2").SingleInstance();

When I register the SpecificModule I want to unregister all previous registrations of ISomething since they are injected as a collection into another constructor.

public SomeClass(IEnumerable<ISomething> somethingCollection)
{
   _somethingCollection = somethingCollection;
}

Is this possible? Or is it better to do it in another way?

1 Answer 1

0

It should atleast be possible to create a new container with the unwanted components filtered out, see Is it possible to remove an existing registration from Autofac container builder?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.