Dynamic Service Registration In ASP.NET Core Dependency Injection Container


In ASP.NET Core, whenever we inject a service as a dependency, we must register this service to ASP.NET Core Dependency Injection container. However, registering services one by one is not only tedious and time-consuming, but it is also error-prone. So here, we will discuss how we can register all the services at once dynamically.

Let's get started!

To register all of the services dynamically, we will use AspNetCore.ServiceRegistration.Dynamic library. This is a small but extremely useful library that enables you to register all your services into ASP.NET Core Dependency Injection container at once without exposing the service implementation.
First, install the latest version of AspNetCore.ServiceRegistration.Dynamic NuGet package into your project as follows:
  1. Install-Package AspNetCore.ServiceRegistration.Dynamic

Using Marker Interface: 

Now let your services inherit any of the ITransientService, IScoperService, and ISingletonService marker interfaces as follows:
  1. // Inherit `IScopedService` interface if you want to register `IEmployeeService` as scoped service.    
  2. public class IEmployeeService : IScopedService     
  3. {    
  4.     Task CreateEmployeeAsync(Employee employee);    
  5. }    
  7. internal class EmployeeService : IEmployeeService    
  8. {    
  9.    public async Task CreateEmployeeAsync(Employee employee)    
  10.    {    
  11.        // Implementation here    
  12.    };    
  13. }    
ITransientService, IScoperService, and ISingletonService are available in AspNetCore.ServiceRegistration.Dynamic.Interfaces namespace.

Using Attribute: 

Now mark your services with any of the ScopedServiceAttribute, TransientServiceAttribute and SingletonServiceAttribute attributes as follows:
  1. // Mark with ScopedServiceAttribute if you want to register `IEmployeeService` as scoped service.  
  2. [ScopedService]  
  3. public class IEmployeeService  
  4. {  
  5.         Task CreateEmployeeAsync(Employee employee);  
  6. }  
  8. internal class EmployeeService : IEmployeeService   
  9. {  
  10.     public async Task CreateEmployeeAsync(Employee employee)  
  11.     {  
  12.        // Implementation here  
  13.     };  
  14. }  
 ScopedServiceAttribute, TransientServiceAttribute, and SingletonServiceAttribute are available in AspNetCore.ServiceRegistration.Dynamic.Attributes namespace.
Now in your ConfigureServices method of the Startup class:
  1. public void ConfigureServices(IServiceCollection services)    
  2. {    
  3.    services.AddServicesOfType<IScopedService>();   
  4.    services.AddServicesWithAttributeOfType<ScopedServiceAttribute>();    
  5. }    
AddServicesOfType<T> is available in AspNetCore.ServiceRegistration.Dynamic.Extensions namespace.


That's it! The job is done! It is as simple as above to dynamically register all your services into ASP.NET Core Dependency Injection container at once. If you have any issues, you can submit it to the Github Repository of this library. You will be helped as soon as possible.
Thank you for reading!