documentation. But ABP has aconventional dependency registration system which automatically register all services in your assembly. See thedependency Injection documentation for more about the dependency injection system.
You can also configure other services and modules in this way. Example:
public class BlogModule : AbpModule{ public override void ConfigureServices(ServiceConfigurationContext context) { //Configure default connection string for the application Configure<AbpDbConnectionOptions>(options => { options.ConnectionStrings.Default = "......"; }); }}
ConfigureServices method has an asynchronous version too:ConfigureServicesAsync. If you want to make asynchronous calls (use theawait keyword) inside this method, override the asynchronous version instead of the synchronous one. If you override both asynchronous and synchronous versions, only the asynchronous version will be executed.
See theConfiguration document for more about the configuration system.
Pre & Post Configure Services
AbpModule class also definesPreConfigureServices andPostConfigureServices methods to override and write your code just before and just afterConfigureServices. Notice that the code you have written into these methods will be executed before/after theConfigureServices methods of all other modules.
These methods have asynchronous versions too. If you want to make asynchronous calls inside these methods, override the asynchronous versions instead of the synchronous ones.
Application Initialization
Once all the services of all modules are configured, the application starts by initializing all modules. In this phase, you can resolve services fromIServiceProvider since it's ready and available.
OnApplicationInitialization Method
You can overrideOnApplicationInitialization method to execute code while application is being started.
Example:
public class BlogModule : AbpModule{ public override void OnApplicationInitialization( ApplicationInitializationContext context) { var myService = context.ServiceProvider.GetService<MyService>(); myService.DoSomething(); }}
OnApplicationInitialization method has an asynchronous version too. If you want to make asynchronous calls (use theawait keyword) inside this method, override the asynchronous version instead of the synchronous one.
Example:
public class BlogModule : AbpModule{ public override Task OnApplicationInitializationAsync( ApplicationInitializationContext context) { var myService = context.ServiceProvider.GetService<MyService>(); await myService.DoSomethingAsync(); }}
If you override both asynchronous and synchronous versions, only the asynchronous version will be executed.
OnApplicationInitialization is generally used by the startup module to construct the middleware pipeline for ASP.NET Core applications.
Example:
[DependsOn(typeof(AbpAspNetCoreMvcModule))]public class AppModule : AbpModule{ public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var env = context.GetEnvironment(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvcWithDefaultRoute(); }}
You can also perform startup logic if your module requires it
Pre & Post Application Initialization
AbpModule class also definesOnPreApplicationInitialization andOnPostApplicationInitialization methods to override and write your code just before and just afterOnApplicationInitialization. Notice that the code you have written into these methods will be executed before/after theOnApplicationInitialization methods of all other modules.
These methods have asynchronous versions too, and if you want to make asynchronous calls inside these methods, override the asynchronous versions instead of the synchronous ones.
Application Shutdown
Lastly, you can overrideOnApplicationShutdown method if you want to execute some code while application is being shutdown.
This methods has asynchronous version too. If you want to make asynchronous calls inside this method, override the asynchronous version instead of the synchronous one.
Module Dependencies
In a modular application, it's not unusual for one module to depend upon another module(s). An ABP module must declare a[DependsOn] attribute if it does have a dependency upon another module, as shown below:
[DependsOn(typeof(AbpAspNetCoreMvcModule))][DependsOn(typeof(AbpAutofacModule))]public class BlogModule{ //...}
You can use multipleDependsOn attribute or pass multiple module types to a singleDependsOn attribute depending on your preference.
A depended module may depend on another module, but you only need to define your direct dependencies. ABP investigates the dependency graph for the application at startup and initializes/shutdowns modules in the correct order.
Additional Module Assemblies
ABP automatically registers all the services of your module to thedependency injection system. It finds the service types by scanning types in the assembly that defines your module class. That assembly is considered as the main assembly of your module.
Typically, every assembly contains a separate module class definition. Then modules depend on each other using theDependsOn attribute as explained in the previous section. However, in some rare cases, your module may consist of multiple assemblies, and only one of them defines a module class, and you want to make the other assemblies parts of your module. In that case, you can use theAdditionalAssembly attribute as shown below:
[DependsOn(...)] // Your module dependencies as you normally do[AdditionalAssembly(typeof(BlogService))] // A type in the target assemblypublic class BlogModule{ //...}
In this example, we assume that theBlogService class is inside one assembly (csproj) and theBlogModule class is inside another assembly (csproj). With theAdditionalAssembly definition, ABP will load the assembly containing theBlogService class as a part of the blog module.
Notice thatBlogService is only an arbitrary selected type in the target assembly. It is just used to indicate the related assembly. You could use any type in the assembly.
WARNING: If you need to use theAdditionalAssembly, be sure that you don't design your system in a wrong way. With this example above, theBlogService class' assembly should normally have its own module class and theBlogModule should depend on it using theDependsOn attribute. Do not use theAdditionalAssembly attribute when you can already use theDependsOn attribute.
Framework Modules vs Application Modules
There aretwo types of modules. They don't have any structural difference but categorized by functionality and purpose:
Framework modules: These arecore modules of the framework like caching, emailing, theming, security, serialization, validation, EF Core integration, MongoDB integration... etc. They do not have application/business functionalities but makes your daily development easier by providing common infrastructure, integration and abstractions.
Application modules: These modules implementspecific application/business functionalities like blogging, document management, identity management, tenant management... etc. They generally have their own entities, services, APIs and UI components. Seepre-built application modules.