net-core
  1. net-core-service-registration

Service Registration - ASP.NET Core Dependency Injection

One of the key features of ASP.NET Core is Dependency Injection (DI), which is used to promote loosely coupled architectures and improve testability of applications.

In this page, we will discuss how to register a service in the DI container in ASP.NET Core.

Heading h1

There are different ways to register a service with the DI container.

Syntax

The syntax for service registration in ASP.NET Core looks like below:

services.AddScoped<IService, Service>();

Example

Let us look at an example that demonstrates how to register a service in the DI container.

public interface IMyService
{
    void DoSomething();
}

public class MyService : IMyService
{
    public void DoSomething()
    {
        //implementation
    }
}

public class HomeController : Controller
{
    private readonly IMyService _myService;

    public HomeController(IMyService myService)
    {
        _myService = myService;
    }

    public IActionResult Index()
    {
        return View();
    }
}

In the example, the MyService class implements the IMyService interface. The HomeController constructor takes an instance of the IMyService interface as a parameter.

To register IMyService with the DI container, add the following code to the ConfigureServices method in the Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IMyService, MyService>();

    services.AddControllersWithViews();
}

Output

When we run the application, the DI container creates an instance of MyService based on the registration settings. The created instance of MyService is then passed to the HomeController constructor as a parameter.

Explanation

When we register a service with the DI container, ASP.NET Core automatically creates an instance of the service for us when we request it in our code. ASP.NET Core decides which service to create based on two factors- the service type that is requested and the lifetime of the registered service.

In our example, we have registered the MyService class as a singleton instance using the AddScoped method. This means that whenever an instance of IMyService is requested, the same instance of MyService is returned.

Use

Service registration is used in ASP.NET Core applications to organize and manage the dependencies of an application.

Important Points

  • DI is used to promote loosely coupled architectures and improve testability of applications
  • We can register a service in a DI container using the AddScoped, AddSingleton, or AddTransient methods
  • Services can be registered with a specific lifetime, which determines how often the DI container creates a new instance of the service.
  • DI is used to manage the dependencies of an application.

Summary

In this page, we discussed how to register a service in the DI container in ASP.NET Core. We saw the syntax, example, output, explanation, use, and important points of the service registration. By using DI to manage the dependencies of an application, we can create loosely coupled and testable architectures that can be easily maintained over time.

Published on: