IoC Container Module

Before an application will be run you need to register all components in the IoC container. Modules can add a set of related components to a container. Each module implements the IContainerModule interface and contains only the Load() method for registering components.

Loading of IoC Container Module

Method Load() designed to register app components and must not contain any other logic due to the fact it is posed in inconsistent state. To register components into Load() an instance of the IContainerBuilder interface is passed.

Note

If there is necessity to execute some logic immediately after the app is run one should use methods described in the article Application Events.

Common structure of IoC-container module may look like this:

public class MyAppContainerModule : IContainerModule
{
    public void Load(IContainerBuilder builder)
    {
        // Registering components...
    }
}

Configuration IoC Container on Startup

To configure the IoC container in an ASP.NET Core application you need to create an instance of the IServiceProvider interface and return one from the ConfigureServices() method. For features that require substantial setup there are Add[Component] extension methods on IServiceCollection. User defined modules are added by the AddContainerModule() extension method. The BuildProvider() extension method builds and returns an instance of the IServiceProvider interface.

 public class Startup
 {
     public IServiceProvider ConfigureServices(IServiceCollection services)
     {
         services.AddContainerModule(new MyAppContainerModule());

         return services.BuildProvider();
     }

     // ...
 }