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
.
At the time you want to change the registration, create a new
ContainerBuilder
, register the new instance, and callUpdate
passing in the container:You can also make use of the Autofac Lifetime event "OnActivating" and have your own controller object in memory which replaces the resolved instance like so
https://autofaccn.readthedocs.io/en/latest/lifetime/events.html#onactivating
An alternative could be to register a delegate that is able to change the underlying instance provided by the container. Consider the following code:
You can now resolve the action to get a delegate that can change the registration:
Note: if you could elaborate on when and why you need to change the instance, perhaps we could even find a better solution.