Back to Devexpress

How to Register DI Services in a Custom Module with Application Builder Extensions

expressappframework-405025-app-shell-and-base-infrastructure-application-solution-components-modules-register-and-configure-services-in-a-module.md

latest3.1 KB
Original Source

How to Register DI Services in a Custom Module with Application Builder Extensions

  • Dec 15, 2025
  • 4 minutes to read

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:

  • In your custom module, you can register and configure all services that the module requires in a centralized manner.
  • In the application’s main module (the MySolution.Module project), you can register and configure all services that must be available across all platforms in the same configuration without the need to duplicate the registration code throughout the Startup.cs files in platform-specific projects (MySolution.Blazor.Server/Startup.cs, MySolution.Win/Startup.cs, and/or MySolution.WebApi/Startup.cs).

Add Module Extensions to a Module

To implement module extensions, follow the steps described below:

  1. 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).

  2. For better abstraction, you can implement additional extension methods (for example, for IServiceCollection) to split the configuration logic based on the task.

Pass Services to a Module Constructor

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:

  1. Add an argument of the required service’s type to your module’s constructor:

  2. Modify the module extensions code so that it passes the required service to the module constructor as shown below:

Register the Module in Platform-Specific Applications

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

csharp
// ...
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

Modules

Register a Built-in XAF Module