RegisterAssemblyTypes() accepts a parameter array of one or more assemblies. By default, all concrete classes in the assembly will be registered. This includes internal and nested private classes. You can filter the set of types to register using some provided LINQ-style predicates.

In 4.8.0 a PublicOnly() extension was added to make data encapsulation easier. If you only want your public classes registered, use PublicOnly():

builder.RegisterAssemblyTypes(asm).PublicOnly();

To apply custom filtering to the types that are registered, use the Where() predicate:

Module scanning is performed with the RegisterAssemblyModules() registration method, which does exactly what its name suggests. It scans through the provided assemblies for Autofac modules, creates instances of the modules, and then registers them with the current container builder.

For example, say the two simple module classes below live in the same assembly and each register a single component:

The overload of RegisterAssemblyModules() that does not accept a type parameter will register all classes implementing IModule found in the provided list of assemblies. In the example below both modules get registered:

The overload of RegisterAssemblyModules() with the generic type parameter allows you to specify a base type that the modules must derive from. In the example below only one module is registered because the scanning is restricted:

varassembly=typeof(AComponent).Assembly;varbuilder=newContainerBuilder();// Registers AModule but not BModulebuilder.RegisterAssemblyModules<AModule>(assembly);

The overload of RegisterAssemblyModules() with a Type object parameter works like the generic type parameter overload but allows you to specify a type that might be determined at runtime. In the example below only one module is registered because the scanning is restricted:

varassembly=typeof(AComponent).Assembly;varbuilder=newContainerBuilder();// Registers AModule but not BModulebuilder.RegisterAssemblyModules(typeof(AModule),assembly);

When using assembly scanning with IIS applications, you can run into a little trouble depending on how you do the assembly location. (This is one of our FAQs)

When hosting applications in IIS all assemblies are loaded into the AppDomain when the application first starts, but when the AppDomain is recycled by IIS the assemblies are then only loaded on demand.