Auto Registration in Microsoft Unity
Unlike many other .NET IoC containers, Microsoft Unity does not support auto registrations. Auto registration is a very useful feature for any IoC container and it would be very helpful in some specific contexts. The Unity Auto Registration project in Codeplex will provide the support for automatic registration using a fluent API. You can add Unity Auto Registration package using NuGet. The following command will add reference to Unity Auto Registration.
PM> Install-Package UnityAutoRegistration
The following registration syntax will scan the all assemblies except system assemblies and will register all types that implemented the open generic type ICommandHandler<>
- IUnityContainer container = new UnityContainer();
- container.ConfigureAutoRegistration()
- .ExcludeSystemAssemblies()
- .Include(type => type.ImplementsOpenGeneric(typeof(ICommandHandler<>)),
- Then.Register().AsFirstInterfaceOfType().WithTypeName())
- .ApplyAutoRegistration();