DEV Community

HOSSIEN014
HOSSIEN014

Posted on

differences of Transient and scoped in ASP NET

In ASP.NET, Transient and Scoped are two different types of dependency lifetimes when working with Dependency Injection (DI). These lifetimes determine how instances of services are created and managed throughout the application's lifecycle. Here's the difference between them:

1. Transient

  • Lifetime: A new instance of the service is created every time it is requested.
  • Use case: Ideal for lightweight, stateless services that don't need to maintain any state between requests.
  • Scope: Each time you request an instance, even within the same HTTP request, you get a new object.
  • Example: A service that performs a small task like formatting a string or logging something specific to a method call.
  services.AddTransient<IService, ServiceImplementation>();
Enter fullscreen mode Exit fullscreen mode

In this case, every time IService is injected, a new instance of ServiceImplementation will be created.

2. Scoped

  • Lifetime: A new instance of the service is created once per request (or per scope). This means that within a single HTTP request or operation, the same instance will be used across different components.
  • Use case: Ideal for services that need to maintain state throughout the duration of a single HTTP request (e.g., a service interacting with a database where you want to use the same instance throughout the request).
  • Scope: The instance is created once per HTTP request (or explicitly defined scope). If multiple components within the same request ask for the service, they get the same instance.
  services.AddScoped<IService, ServiceImplementation>();
Enter fullscreen mode Exit fullscreen mode

In this case, within a single HTTP request, every component requesting IService will get the same instance of ServiceImplementation.


Key Differences:

Aspect Transient Scoped
Lifetime A new instance is created each time the service is requested. A new instance is created once per request or per scope.
Usage For lightweight, stateless services. For services that require state during a single request.
Memory Consumption May increase memory usage if many instances are created. Memory usage is typically lower as the same instance is reused within the scope.
Example Logging services, utility classes. Database context, services that interact with session state.

Choosing Between Them:

  • Use Transient when the service does not need to hold state or depend on other services in a way that requires maintaining a single instance.
  • Use Scoped when the service holds some state for the duration of a request (e.g., database contexts, services that rely on a single request lifetime).

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay