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 TanvirArjel.Extensions.Microsoft.DependencyInjection 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 TanvirArjel.Extensions.Microsoft.DependencyInjection NuGet package into your project as follows,
  1. Install-Package TanvirArjel.Extensions.Microsoft.DependencyInjection  
Using Marker Interface
Now let your services inherit any of the ITransientService, IScoperService, and ISingletonService marker interfaces as follows,
  1. using TanvirArjel.Extensions.Microsoft.DependencyInjection

  2. // Inherit `IScopedService` interface if you want to register `IEmployeeService` as scoped service.    
  3. public class IEmployeeService : IScopedService     
  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. }    
ITransientService, IScoperService, and ISingletonService are available in TanvirArjel.Extensions.Microsoft.DependencyInjection namespace.
Using Attribute
Now mark your services with any of the ScopedServiceAttribute, TransientServiceAttribute, and SingletonServiceAttribute attributes as follows,
  1. using TanvirArjel.Extensions.Microsoft.DependencyInjection

  2. // Mark with ScopedServiceAttribute if you want to register `IEmployeeService` as scoped service.  
  3. [ScopedService]  
  4. public class IEmployeeService  
  5. {  
  6.         Task CreateEmployeeAsync(Employee employee);  
  7. }  
  9. internal class EmployeeService : IEmployeeService   
  10. {  
  11.     public async Task CreateEmployeeAsync(Employee employee)  
  12.     {  
  13.        // Implementation here  
  14.     };  
  15. }  
ScopedServiceAttribute, TransientServiceAttribute, and SingletonServiceAttribute are available in TanvirArjel.Extensions.Microsoft.DependencyInjection 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 TanvirArjel.Extensions.Microsoft.DependencyInjection namespace.
Moreover, if you want only specific assemblies to be scanned during type scanning,
  1. public static void ConfigureServices(IServiceCollection services)  
  2. {  
  3.     // Assemblies start with "TanvirArjel.Web", "TanvirArjel.Application" will only be scanned.  
  4.     string[] assembliesToBeScanned = new string[] { "TanvirArjel.Web""TanvirArjel.Application" };  
  5.     services.AddServicesOfType<IScopedService>(assembliesToBeScanned);  
  6.     services.AddServicesWithAttributeOfType<ScopedServiceAttribute>(assembliesToBeScanned);  


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!