03 Feb 2024




Advanced

Scoped, transient, and singleton are three different ways to define the lifetime of a service when using dependency injection (DI) in ASP.NET Core. They determine how many instances of the service are created and when they are created/destroyed, impacting performance, memory usage, and state management.

  1. Singleton: Only one instance of the specified service type is created and shared across all requests for the entire application lifetime. This means that a single global instance of the object will be created and reused throughout the application's lifetime. Singleton lifetime is appropriate for stateless or thread-safe services that can be shared across multiple requests.

    💡Detailed Explanation for Singleton Service: Only one instance of the specified service type is created for the entire application lifecycle. This single instance is shared across all components and classes that request it throughout the lifetime of the application. Once created, the same instance is reused for subsequent requests, regardless of the scope or context in which it is requested. Singleton services are typically used for stateless or thread-safe operations that can be safely shared across multiple components and requests. They are suitable for services that maintain global state, perform expensive initialization, or need to be accessed consistently throughout the application. However, caution should be exercised when using Singleton services to ensure thread safety and prevent unintended state mutations.

  2. Scoped: A single instance of the specified service type is created per request (typically an HTTP request in web applications). The same object instance will be shared within the scope of a single request and response cycle. Scoped lifetime is useful for services that need to maintain state within the scope of a single request.

    💡Detailed Explanation for Scoped Service: A single instance of the specified service type is created per scope. In the context of web applications, such as ASP.NET Core, the scope typically corresponds to an HTTP request. This means that within the scope of a single HTTP request and response cycle, the same instance of the service is shared across all components and classes that request it. Once the request is completed, and the response is sent, the scoped service instance is disposed of, and a new instance will be created for the next HTTP request. Scoped services are useful for maintaining state or data specific to individual HTTP requests without sharing across different requests.

  3. Transient: A new instance of the specified service type is created every time it is requested by any component or class within the application. This means that a new object will be injected into every single request and response cycle. Transient lifetime is suitable for lightweight, stateless services where a new instance is needed every time.

    💡Detailed Explanation for Transient Service: A new instance of the specified service type is created each time it is requested by any component or class within the application. Unlike Scoped services, Transient services are not bound to a specific scope such as an HTTP request. Instead, a new instance is created every time the service is requested, regardless of the scope or context. This makes Transient services suitable for lightweight, stateless operations where a fresh instance is needed for each usage. They are ideal for scenarios where the service does not maintain state or requires independence from the lifecycle of other components or requests.

These lifetimes allow you to control how instances of your services are managed and shared within your ASP.NET Core application, depending on the specific requirements and behavior of your components.

AspectSingletonScopedTransient
CreationOnly one instance of the specified service type is created and shared across all requests for the entire application lifetime.A single instance of the specified service type is created per request (typically an HTTP request in web applications).A new instance of the service is created each time it is requested.
ScopeShared across the entire application.Shared within the same HTTP request.Not shared, specific to each request or usage.
LifetimeLong-lived, persists throughout the application's lifetime.Tied to the scope of an HTTP request, disposed when the request ends.Short-lived, disposed after use.
UsageSuitable for stateless or thread-safe services that can be reused across multiple requests.Suitable for components requiring shared state within an HTTP request.Suitable for lightweight, stateless services.
ExampleCaching services, configuration settings.Entity framework database context and unit of work patterns.Repository classes for short-lived operations.

👉Examples for scoped , singleton and transient service lifetime in Asp.Net Core

asp.net-core
scoped
transient
singleton
difference