Instance management
.NET Core C# Dependency Injection Patterns Visual Studio

How to use AddSingleton, AddScope and AddTransient Services in a .NET Core Application

Welcome to my post!

Today I will discuss how you use the three important service lifetimes with dependency injection.

The lifetime scopes are Singleton, Scoped and Transient.

Within a .NET Core application services are created with the three service collection extension methods AddSingleton(), AddScoped() and AddTransient().

When you first encounter these scopes, it can be confusing as to which lifetime to use within a .NET Core application, especially an application that is running as a service, such as a Web API. Using the wrong lifetime will lead your application to fail under certain scenarios, such as multi-user access to a service.

I will provide recommendations on what you can use in different application requirement contexts.

Singleton

With Singleton lifetime, the interface you want to instantiate will span the lifetime of the application.

The diagram below illustrates this:

Object instancing lifetime

When to use Singleton lifetime

When you need to define a set of configurations that are visible to your application components (start up, controllers and classes), then use the Singleton lifetime.

Scoped

With Scoped lifetime, the interface you want to instantiate will span the lifetime of the request or controller.

The diagram below illustrates this:

When to use Scoped lifetime

Use this for web session user contexts and data access contexts will vary and require isolation for security and concurrency reasons.

Transient

Recommended usage scenarios

Transient lifetime

Use this lifetime for instantiated user classes that will vary whenever they are created on demand within a class.  This is clearly the most frequently used lifetime scope in an application.

Examples would be data repository classes, service classes and so on.

Scoped lifetime

Use this lifetime for web session user contexts and data access contexts will vary and require isolation for security and concurrency reasons.

Examples of usage would be in the following contexts:

  • Data context

Persistence of data context connections during access to repository classes during a request.

  • HTTP context

Context of the instantiated objects persist during a Web HTTP request (such as an API call).

Singleton lifetime

Use this lifetime when you need to define a set of configurations that are visible to your application components (start up, controllers and classes), and expect the lifetime of the instantiated objects to persist during the lifetime of the application, then use the Singleton lifetime.

An example would be an application configuration class, whose properties are accessible to all classes instantiated within the application. We will discuss how you can do this is a future blog.

A detailed treatment of service lifetimes is here (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0#service-lifetimes).

That’s all for this discussion on using the service lifetime patterns in .NET Core Dependency Injection.

Social media & sharing icons powered by UltimatelySocial