Click here to Skip to main content
15,891,704 members
Articles / Web Development / ASP.NET / ASP.NET Core
Tip/Trick

Dynamic Service Registration in ASP.NET Core Dependency Injection Container

Rate me:
Please Sign up or sign in to vote.
4.75/5 (5 votes)
15 Apr 2020CPOL1 min read 14.5K   7   1
How to dynamically register all services as a dependency to an ASP.NET Core Dependency Injection container
Registering services one by one when injecting a service as a dependency to an ASP.NET Core Dependency Injection container is not only tedious and time consuming, but also error prone. In this post, we will discuss how to register all the services at once dynamically.

Introduction

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

Let's Get Started!

To register all 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.

Now first install the latest version of AspNetCore.ServiceRegistration.Dynamic nuget package into your project as follows:

C#
Install-Package AspNetCore.ServiceRegistration.Dynamic

Using Marker Interface:

Now let your services inherit any of the ITransientService, IScoperService and ISingletonService marker interfaces as follows:

C#
// Inherit `IScopedService` interface if you want to register `IEmployeeService` 
// as scoped service.
public class IEmployeeService : IScopedService 
{
    Task CreateEmployeeAsync(Employee employee);
}

internal class EmployeeService : IEmployeeService
{
   public async Task CreateEmployeeAsync(Employee employee)
   {
       // Implementation here
   };
}

Using Attribute:

Now mark your services with any of the ScopedServiceAttributeTransientServiceAttribute and SingletonServiceAttribute attributes as follows:

C#
// Mark with ScopedServiceAttribute if you want to register `IEmployeeService` as scoped
// service.
[ScopedService]
public class IEmployeeService
{
    Task CreateEmployeeAsync(Employee employee);
}

internal class EmployeeService : IEmployeeService
{
    public async Task CreateEmployeeAsync(Employee employee)
    {
       // Implementation here
    };
}

Now in your ConfigureServices method of the Startup class:

C#
public static void ConfigureServices(IServiceCollection services)
{
    services.AddServicesOfType<IScopedService>();
    services.AddServicesWithAttributeOfType<ScopedServiceAttribute>();
}

AddServicesOfType<T>(); and AddServicesWithAttributeOfType<T>(); are available in AspNetCore.ServiceRegistration.Dynamic.Extensions namespace.

Conclusion

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!

History

  • 9th April, 2020: Initial version.
  • 16th April, 2020 : Updated for attribute-based registration.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Bangladesh Bangladesh
A Software Engineer, Passionate Programmer and Technical Writer!!

Comments and Discussions

 
Praisevery clever Pin
cphv17-Apr-20 17:07
cphv17-Apr-20 17:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.