AddTransient, AddScoped and AddSingleton Services Differences

Post author: Adam VanBuskirk
Adam VanBuskirk
7/29/23 in
Tech
C#

In C#, when registering services in the dependency injection container (usually in the ConfigureServices method of the Startup class), you have three options: AddTransient, AddScoped, and AddSingleton. These methods determine the lifetime of the registered services, meaning how long instances of the service will be kept in memory and how they will be shared among different parts of your application.

  1. AddTransient:
    • The AddTransient method registers a new instance of the service every time it is requested by any part of the application.
    • Each time a service is requested, a new instance is created, and it is not reused for subsequent requests.
    • This is suitable for lightweight stateless services that don’t require any shared state among different parts of the application.

Example:

// Assume we have a service called MyTransientService implementing IMyService interface.
// When a service is registered as transient, a new instance is returned every time it is resolved.

services.AddTransient<IMyService, MyTransientService>();

See Using a Transient Scope with repositories or data access services for a more robust example of when to use a transient scope.

  1. AddScoped:
    • The AddScoped method registers a single instance of the service per scope. A scope could be an HTTP request or any other defined scope, depending on the application setup (e.g., web applications with web requests, or using an explicit scope in other scenarios).
    • For the duration of a scope, the same instance is reused for all requests within that scope. When a new scope is created, a new instance will be provided.
    • This is appropriate for services that maintain state within a specific scope, such as per-request services in web applications.

Example:

// Assume we have a service called MyScopedService implementing IMyService interface.
// When a service is registered as scoped, the same instance is returned within the same scope.

services.AddScoped<IMyService, MyScopedService>();

See Using a Scoped service to deal with stateful operations or maintaining data across multiple method calls for a more robust example of when to use a scoped service.

  1. AddSingleton:
    • The AddSingleton method registers a single instance of the service for the entire lifetime of the application.
    • It means that the same instance is reused for all requests, regardless of the scope or the number of times the service is requested.
    • Use this for services that are stateless or immutable and can be safely shared across the application.

Example:

// Assume we have a service called MySingletonService implementing IMyService interface.
// When a service is registered as singleton, the same instance is returned for the entire application lifetime.

services.AddSingleton<IMyService, MySingletonService>();

See Use a singleton service to manage a shared resource or state that needs to be accessed by multiple parts of the application throughout its lifetime for a more robust example of using a singleton service in C#.

Recap

Choosing the appropriate lifetime for your services is essential for managing resource usage and ensuring the correctness of your application. Always consider the statefulness and the desired behavior of your services when selecting between AddTransient, AddScoped, and AddSingleton.

Sign up today for our weekly newsletter about AI, SEO, and Entrepreneurship

Leave a Reply

Your email address will not be published. Required fields are marked *


Read Next




© 2024 Menyu LLC