20

While discussing Autofac with a colleague, the issue of run-time registration of dependencies arose. In Prism, for instance, assemblies are frequently loaded at run time and their dependencies registered with the IoC container (usually Unity).

How can this be accomplished with Autofac?

From Autofac's documentation and what I've found on the web, it seems that registration is performed at application start. Even when "external" assemblies are used, the registrations are located in modules with the assemblies at app start. How do we do this after the container is "built" at app start?

(Note that the assembly may want to add dependencies for the use of other components in the application, and so a nested container may not solve the problem here. Related to this topic: Unity has methods such as RegisterIfExists and the like. Are there Autofac equivalents?)

Thanks!

Daniel Miller
  • 331
  • 1
  • 2
  • 9
  • possible duplicate of [Autofac, adding services after container has been built](http://stackoverflow.com/questions/4998870/autofac-adding-services-after-container-has-been-built) – Jim G. Jun 06 '13 at 15:39

2 Answers2

32

Update an existing Autofac Container: You can update an existing Autofac Container at runtime by using ContainerBuilder.Update(). The following code sample, taken from the blog post Autofac 2.2 Released, demonstrates the usage:

var container = // something already built

var updater = new ContainerBuilder();
updater.RegisterType<A>();
updater.Register(c => new B()).As<IB>();

// Add the registrations to the container
updater.Update(container);

Autofac and Prism Integration: The question Whats the status of Prism integration in Autofac? may also be useful to you.

Update July 2021 - Autofac has removed the Update method (not recently but I've just noticed). See this issue on github for 'better' ways to do what you want without Update. https://github.com/autofac/Autofac/issues/811

Dave
  • 2,829
  • 3
  • 17
  • 44
bentayloruk
  • 4,060
  • 29
  • 31
  • Do you know if it's possible to do this using types from an assembly loaded dynamically? That is, I use `Assembly.LoadFrom()` and then update the container with the newly discovered types? – Mark Richman Feb 05 '15 at 16:26
  • One of the `RegisterType` overloads takes a `Type` instance, so if you have that you should be good to go. – bentayloruk Feb 07 '15 at 16:37
  • `Update()` command can use just once. This is not efficient for runtime registration. I need to Register new types after `.Build()` operation if possible... – Oğuzhan Soykan Oct 03 '16 at 20:50
9

Update for Autofac 4.8.1.0

ContainerBuilder.Update method is marked as Obsolete with a comment:
Containers should generally be considered immutable. Register all of your dependencies before building/resolving. If you need to change the contents of a container, you technically should rebuild the container. This method may be removed in a future major release.

ivamax9
  • 2,601
  • 24
  • 33
  • 1
    Why is swapping implementation at runtime this bad? For example on mobile, the user could reject some permission and instead of an implementation that shows notification, i could swap to one that just ignore them. Why is it so bad? Also maybe i need to register dbcontexts at runtime because I need to have a dynamic connection string. – Taunter exd Dec 23 '22 at 15:28