Dynamic Service Registration In ASP.NET Core Dependency Injection Container

Dynamic Service Registration In ASP.NET Core Dependency Injection Container

How to dynamically register all services as a dependency to an 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 ITransientServiceIScoperService, and ISingletonService marker interfaces as follows,

using TanvirArjel.Extensions.Microsoft.DependencyInjection

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

ITransientServiceIScoperService, and ISingletonService are available in _TanvirArjel.Extensions.Microsoft.DependencyInjection _namespace.


Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

jQuery Ajax CRUD in ASP.NET Core MVC with Modal Popup

In this article, we’ll discuss how to use jQuery Ajax for ASP.NET Core MVC CRUD Operations using Bootstrap Modal. With jQuery Ajax, we can make HTTP request to controller action methods without reloading the entire page, like a single page application.

Hire ASP.Net Developers

Looking to outsource your asp dot net development requirement? ASP.Net is a special feature of the DOT Net framework created by Microsoft. At [HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io"), we have a team of experienced...

MVC User Registration & Login with ASP.NET Core Identity

In this article, we will discuss the quickest way to use ASP.NET Core Identity for User Login and Registration in a new or existing MVC application.

Running WordPress on ASP.NET Core with Peachpie

In this article, you will learn how to use or integrate WordPress in ASP.NET and Running WordPress on ASP.NET Core, without PHP, or any source files on the server. The following demonstration will show you how to add WordPress as a frontend to an existing ASP.NET Core application step by step.

Routing in MVC - ASP.NET Core Demystified

ASP.NET Core MVC has introduced quite a few concepts that new (or new-to-ASP.NET) web developers might have some difficulty getting caught up with. My ASP.NET Core Demystified series is designed to help these developers get started building their own custom, full-fledged, working AASP.NET Core applications. In