expressappframework-405025-app-shell-and-base-infrastructure-application-solution-components-modules-register-and-configure-services-in-a-module.md
This topic describes how to implement an extension class that you can use to implement logic required to register and configure services within a module project.
You can use this technique to achieve one of the following:
To implement module extensions, follow the steps described below:
Add a static class (named MySolutionModuleExtensions in this example) to your module. Add a static AddMySolutionModule extension method to the class that extends the IModuleBuilder interface. This method will be used to register your module in the Application Builder code within the Startup.cs files of application projects (see the Register the Module in Platform-Specific Applications section).
For better abstraction, you can implement additional extension methods (for example, for IServiceCollection) to split the configuration logic based on the task.
If you need to access a service from code in a module’s Module.cs file (for example, in your module class’s Setup method), adjust your application code as follows:
Add an argument of the required service’s type to your module’s constructor:
Modify the module extensions code so that it passes the required service to the module constructor as shown below:
To register your module and execute all configuration logic implemented in the module’s MySolutionModuleExtensions class within a platform-specific application’s scope, add the following line to the Application Builder code in the application’s Startup.cs file:
File: MySolution.Blazor.Server/Startup.cs, MySolution.Win/Startup.cs, MySolution.WebApi/Startup.cs
// ...
builder.Modules
// ...
.AddMySolutionModule()
If the Startup.cs file already contains code that registers the module (builder.Modules.Add<MySolutionModule>), be sure to remove this code.
See Also