AUTOFAC can use conventions to find and register components in an assembly. You can scan and register various types, or you can specialize in scanning AUTOFAC modules. Scan Type
Otherwise known as custom-driven registrations or scans, AUTOFAC can register a set of types from an assembly based on user-specified rules:
var dataaccess = assembly.getexecutingassembly ();
Builder. Registerassemblytypes (dataaccess)
. Where (t => t.name.endswith ("Repository"))
. Asimplementedinterfaces ();
Each registerassemblytypes () call applies only a set of rules-if you want to register components for multiple different groups, you will need to call Registerassemblytypes () multiple times. Filter Type
Registerassemblytypes () accepts an array of parameters for one or more assemblies. By default, all public-specific classes in the assembly are registered. You can use some of the provided LINQ style verbs to filter the set of types to be registered.
To filter registered types, use the WHERE () verb:
Builder. Registerassemblytypes (ASM)
. Where (t => t.name.endswith ("Repository"));
To exclude types from the scan, use the except () verb:
Builder. Registerassemblytypes (ASM)
. Except<myunwantedtype> ();
The Except () predicate also allows you to customize the registration for a specific exclusion type:
Builder. Registerassemblytypes (ASM)
. except<mycustomisedtype> (Ct =>
Ct. As<ispecial> (). SingleInstance ());
Multiple filters can be used, in which case they will be applied to the logical and. Specify service
The registration Syntax for Registerassemblytypes () is a superset of a single type of registration syntax, so a method like as () can also use an assembly:
Builder. Registerassemblytypes (ASM)
. Where (t => t.name.endswith ("Repository"))
. As<irepository> ();
Optional overloads of AS () and named () accept lambda expressions that determine a type and what services it will provide:
Builder. Registerassemblytypes (ASM)
. As (T => t.getinterfaces () [0]);
As with normal component registrations, multiple calls to as () are added together.
Added some additional registration methods to make it easier to establish common conventions:
| method
Description |
Example |
| Asimplementedinterfaces () |
Registers a type to provide all its public interfaces as a service (excluding IDisposable) |
Builder. Registerassemblytypes (ASM). Where (t => t.name.endswith ("Repository")). Asimplementedinterfaces (); |
| Asclosedtypesof (Open) |
Registers the types of closed instances that can be assigned to open generic types ... |
Builder. Registerassemblytypes (ASM). Asclosedtypesof (typeof (Irepository<>)); |
| Asself () |
Default value: Registering the type for yourself-is also useful when overriding the default value with another service specification. |
Builder. Registerassemblytypes (ASM). Asimplementedinterfaces (). Asself (); |
Scan Module
The module scan is done through the Registerassemblymodules () registration method, which is implied by its name. It scans the supplied AUTOFAC module's assembly, creates an instance of the module, and registers it with the current container builder.
For example, suppose that the following two simple module classes exist in the same assembly, and each registers a component:
public class Amodule:module
{
protected override void Load (Containerbuilder builder)
{
Builder. Register (c => new Acomponent ()). As<acomponent> ();
}
public class Bmodule:module
{
protected override void Load (Containerbuilder builder)
{
Builder. Register (c => New Bcomponent ()). As<bcomponent> ();
}
Overloads that do not accept the registerassemblymodules () of the type parameter will register all classes found in the provided assembly list that implement IModule. In the following example, two modules are registered:
var assembly = typeof (Acomponent). Assembly;
var builder = new Containerbuilder ();
Register these two modules
builder. Registerassemblymodules (assembly);
Overloads of Registerassemblymodules () and generic type parameters allow you to specify the base type (from which the module must derive). In the following example, only one module is registered because the scan is restricted:
var assembly = typeof (Acomponent). Assembly;
var builder = new Containerbuilder ();
Registers a module but does not contain Bmodule
builder. Registerassemblymodules<amodule> (assembly);
The overload of the Registerassemblymodules () that uses the type object parameter is similar to the common type parameter overload, but allows you to specify a type that may be determined at run time. In the following example, only one module is registered because the scan is restricted:
var assembly = typeof (Acomponent). Assembly;
var builder = new Containerbuilder ();
Registers a module but does not contain Bmodule
builder. Registerassemblymodules (typeof (Amodule), assembly);
IIS-hosted Web applications
When you use an assembly scan in an IIS application, you may experience some trouble depending on the location of the assembly. (This is one of our common problems)
When an application is hosted in IIS, all assemblies are loaded into AppDomain when the application first starts, but when AppDomain is reclaimed by IIS, the assembly is loaded only when it is needed.
To avoid this problem, use the Getreferencedassemblies () method on System.Web.Compilation.BuildManager to get a list of reference assemblies:
var assemblies = Buildmanager.getreferencedassemblies (). Cast<assembly> ();
This forces the referenced assembly to be loaded immediately into the AppDomain so that it can be used for module scans.