.NET Core: Detail of Lifetime Management

.NET Core

Scopes and life management

The below image will explain the flow of getting services from .net core service registry,

.net core service registry

Once a request comes to the endpoint,

  1. The process begins by fetching the required service details from the service registry, which involves specifying the service type and service name.For example, the service type is "Singleton," and the service name is "IConfigurationSettings" from the Dependency Injection (DI) of the Controller.
  2. The system then checks the service type:
    • If it's "Singleton," it does not create a new object. Instead, it retrieves it from the already created object and returns it. This object is shared memory throughout the application.
    • If it's "Scoped," it checks whether the request already has shared memory within the request. If so, it returns the existing object; otherwise, it creates a new object and returns it. This shared memory is specific to the single request of the endpoint.
    • If it's "Transient," it does not maintain shared memory, always creating a new object and returning it.
  3. In our example, since it's Singleton, the system retrieves the already created object of settings from the application's startup.

Previous Part: Click Here


Similar Articles