Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

In-built Dependency Injection in .NET Core 3.1


In this article, we will learn about the in-built dependency injection IOC (Inversion of Control) container that comes out of the box with .net core development. This eliminates the use of external IOC Containers like AutoFac, Unity, etc.


  • .NET Core SDK
  • Visual Studio Code
  • Basic knowledge of IOC concept

The built-in container offers Constructor and method injection only. Property injection is not supported as of now, which can be achieved using third party containers like AutoFac, unity etc.

In-built container manages the classes and their implementations in the form of services which can be divided into two parts:

1. Framework provided These are the in-built services that can be injected without registering them in the container. They are by default registered in the container.

2. Application provided These are the user created services that can be injected after registering in the built-in container.

Lifetime of registered services: There are three types of registrations that are provided with IOC container of .net core. These registrations define the lifetime of the dependencies.

1. Transient: This will create a new instance every time it is used and accessed in the code.

2. Scoped: This will create a new instance for a single request and use the same object throughout that particular request. If a new request comes in, a new object will be assigned.

3. Singleton: This will create a new instance per lifecycle.

We will see all of them in action later in this article.

Extension methods: For all the three registration types mentioned above, .net core offers extension methods which makes it easy to register the objects in the container.

1. AddTransient() 2. AddScoped() 3. AddSingleton()

Enough information. Now, let’s see the demo.

Step 1: Install .NET Core 3.1 SDK from Microsoft’s official website.

In-built Dependency Injection

Install the SDK from the downloaded installer.

Step 2: Install Visual Studio Code editor. Please go to https://code.visualstudio.com and download the installer based on your OS Type.

In-built Dependency Injection

Step 3: We need to install an extension in VS Code which will activate C# features in VS code. So, install C# extension mentioned below.

In-built Dependency Injection

Step 4: Create a folder at a location on your hard drive and open it in VS Code.

In-built Dependency Injection

Step 5: Hit the following command in the integrated terminal to create a new .net core console application.

Command: dotnet new webapp

In-built Dependency Injection

Step 6: Create a LogService that uses ILogger in-built service to log messages to the console. Create a Services folder -> LogService.cs. Copy the following code.

using Microsoft.Extensions.Logging; public interface ILogService { void LogMessage(string message); } public class LogService : ILogService { private ILogger<LogService> _log; public LogService(ILogger<LogService> log) { _log = log; } public void LogMessage(string message) { _log.LogInformation(message); } }

As you can see above, we injected ILogger to the LogService class and LogInformation method is used to Log the messages to the console at runtime.

Step 7: Let’s create another service named DemoService.cs file in the same folder and inject LogService using constructor injection.

public class DemoService: IDemoService { private ILogService _logService; public DemoService(ILogService logService) { _logService = logService; } public void Log() { _logService.LogMessage("Logging Message from Demo Service using LogService"); } } public interface IDemoService { void Log(); }

In the above screenshot, we injected LogService in the constructor of this class and calls the LogMessage method with a hardcoded message.

Step 8: Now, we need to invoke DemoService when the application runs. So, let’s inject this service in Index.cshtml.cs file so that we can invoke this service on the Index page.

using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; namespace DotNetCoreDI.Pages { public class IndexModel : PageModel { private readonly ILogger<IndexModel> _logger; private IDemoService _demoService; public IndexModel(ILogger<IndexModel> logger, IDemoService demoService) { _logger = logger; _demoService=demoService; } public void OnGet() { _demoService.Log(); } } }

In the above code, we injected DemoService in the constructor and calling the Log method which should log the hardcoded message of previous step on the console.

Step 9: Now, if you have noticed, we have introduced two services LogService and DemoService which are being used as dependencies. So, we need to register them in the in-built container.

Open Startup.cs file and replace the following code.

public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddSingleton(typeof(ILogService),typeof(LogService)); services.AddTransient(typeof(IDemoService),typeof(DemoService)); }

In the above code, we registered two dependencies in the container services. Now our application container knows about the abstract and the concrete implementation of the newly created services.

So, when the Index page calls DemoService, DemoService will call LogService and finally LogService will call ILogger service to write the log messages to the console.

Step 10: Let’s run the application and see if we are able to see the message on the console.

Command: dotnet run

Now navigate to http://localhost:5000 and once we hit this URL, Index page will get hit and on the left side, you can see the hardcoded message along with Service name from which it is getting logged.

In-built Dependency Injection

If you forget to register services in the container, you might get the below error. I commented DemoService declaration from Startup.cs and received the following error on the console.

In-built Dependency Injection

So, be sure that ASP.NET developers register all those services which are being used as a dependency in any class file.

Software Development Team
Need Software Development Team?

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs


NSS Note
Trusted by Global Clients