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?

adamfinstorp
  • 1,627
  • 3
  • 17
  • 26

1 Answers1

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?

Community
  • 1
  • 1
aL3891
  • 6,205
  • 3
  • 33
  • 37