In Autofac how do I change the instance that is registered after Build has been called?

Simon picture Simon · Oct 18, 2010 · Viewed 13.7k times · Source

So lets say i have this code

var builder = new ContainerBuilder();
builder.RegisterInstance(new MyType());
var container = builder.Build();

Then some time later I want to change the instance of MyType for all future resolves that are called on container.

Answer

Jeff Ogata picture Jeff Ogata · Oct 18, 2010

At the time you want to change the registration, create a new ContainerBuilder, register the new instance, and call Update passing in the container:

// at some later point...
builder = new ContainerBuilder();
builder.RegisterInstance(myType2);
builder.Update(container);