👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Dependency Injection Lifetimes in .NET

Dependency Injection Lifetimes in .NET

Dependency Injection

2 Articles

Improve

In this article, let's learn about Lifetimes in Dependency Injection in .NET.

Note: If you have not done so already, I recommend you read the article on Introducing Dependency Injection in .NET.

Table of Contents

  1. Introduction
  2. Transient
  3. Scoped
  4. Singleton
  5. Captive Dependency
  6. Scope Validation
  7. Summary

Introduction

Lifetimes in dependency injection describe the way object instance is created, reused and disposed by the framework. Services can be registered with one of the following lifetimes:

  • Transient
  • Scoped
  • Singleton

The following sections describe each of the preceding lifetimes. Choose an appropriate lifetime for each registered service.

Transient

Transient lifetime services are created each time they're requested from the service container. This lifetime works best for lightweight, stateless services. Register transient services with AddTransient.

Code Sample - Transient Dependency

Demo Space

Scoped

For web applications, a scoped lifetime indicates that services are created once per client request (connection). Register scoped services with AddScoped.

In apps that process requests, scoped services are disposed at the end of the request.

When using Entity Framework Core, the AddDbContext extension method registers DbContext types with a scoped lifetime by default.

Code Sample - Scoped Dependency

Demo Space

Singleton

Singleton lifetime services are created either:

  • The first time they're requested.
  • By the developer, when providing an implementation instance directly to the container. This approach is rarely needed.

Every subsequent request of the service implementation from the dependency injection container uses the same instance. If the app requires singleton behavior, allow the service container to manage the service's lifetime. Don't implement the singleton design pattern and provide code to dispose of the singleton. Services should never be disposed by code that resolved the service from the container. If a type or factory is registered as a singleton, the container disposes the singleton automatically.

Register singleton services with AddSingleton. Singleton services must be thread safe and are often used in stateless services.

In apps that process requests, singleton services are disposed when the ServiceProvider is disposed on application shutdown. Because memory is not released until the app is shut down, consider memory use with a singleton service.

Code Sample - Singleton Dependency

Demo Space
Lifetimes in Dependency Injection in DotNet

Captive Dependency

A service should not depend on a service with lifetime shorter than its own. This is called Dependency Captivity. For example, a service registered with singleton lifetime should not depend on transient service. Captive Dependency will make service to live longer than expected causing runtime bugs which are hard to track down.

A service is said to have safe dependency when it has a dependency of lifetime same or above its own lifetime. Here is a table describing the safe dependency.

Safe Dependencies Transient Scoped Singleton
Transient
Scoped
Singleton

By default, in the development environment, resolving a service from another service with a longer lifetime throws an exception. For more information, see Scope Validation.

Dependency Captivity in Dependency Injection in DotNet

Scope Validation

When the app runs in the Development environment and calls CreateDefaultBuilder to build the host, the default service provider performs checks to verify that:

  • Scoped services aren't resolved from the root service provider.
  • Scoped services aren't injected into singletons.

The root service provider is created when BuildServiceProvider is called. The root service provider's lifetime corresponds to the app's lifetime when the provider starts with the app and is disposed when the app shuts down.

Scoped services are disposed by the container that created them. If a scoped service is created in the root container, the service's lifetime is effectively promoted to singleton because it's only disposed by the root container when the app shuts down. Validating service scopes catches these situations when BuildServiceProvider is called.

Scope Validation can be enabled explicitly on build using ValidateOnBuild or in all environment using ValidateScopes options inside UseDefaultServiceProvider.

Code Sample - Dependency Injection Scope Validation

Summary

In this article, we learn't about different lifetimes in Dependency Injection in .NET and when to use what lifetime for registering services. We also understood what is captive dependency and how to avoid it by validating the scopes.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • Dependency Injection
  • Lifetimes
  • Transient
  • Singleton
  • Scoped
  • Dependency Captivity
  • Captive Dependencies