Service Life Time in ASP.NET Core

In ASP.NET Core, the service lifetime is a concept that determines how long an instance of a service will be kept in memory and reused by the application. The service lifetime is specified when a service is registered with the service collection, and it can be one of three options:

Transient: A new instance of the service is created each time it is requested. This is useful when a service has no state or its state can be discarded after each use.

Scoped: A new instance of the service is created for each request, but the same instance is reused for all services requested within the same request. This is useful when a service has a state that should be shared within the scope of a single request.

Singleton: A single instance of the service is created and reused for all requests. This is useful when a service has a state that should be shared across all requests.

2 Comments

Leave a Reply

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