Search code examples
c#.netdependency-injectionsimple-injector

SimpleInjector Unbind/Rebind


I have a core set of libraries that are delivered and work "out of the box". Meaning that the services are all wired up inside. I would like to have the ability to modify the core library (without modifying the library itself).

With that said, is there an Unbind/Rebind support in SimpleInjector? I didn't see any publicly visible methods on the container. I did find a private dictionary of registrations that I can get to with reflection.

Does anyone see a reason why I can't remove items from this private dictionary (to then re-add) at runtime with reflection? Is there a method I am missing?


Solution

  • Removal of registrations is not possible. Overriding registrations however is. You will have to flag the container to allow this:

    var container = new Container();
    
    container.Register<IService, FirstService>();
    
    container.Options.AllowOverridingRegistrations = true;
    
    // Replaces the former registration
    container.Register<IService, AnotherService>();