In .NET Core, service lifetimes determine how long a service instance will be alive. There are three types of service lifetimes:
Transient: A transient service is created each time it is requested. This means that a new instance of the service is created for each request.
Scoped: A scoped service is created once per request. This means that the same instance of the service is used for all requests within the same scope.
Singleton: A singleton service is created once for the lifetime of the application. This means that the same instance of the service is used for all requests within the application.
The service lifetime can be specified when the service is registered with the dependency injection container. The following code shows how to register a service with a transient lifetime:
C#
public class MyService
{
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<MyService>();
}
}
In this example, the MyService class is registered with a transient lifetime. This means that a new instance of the MyService class will be created for each request.
The service lifetime can also be specified when the service is injected into a class. The following code shows how to inject a service with a scoped lifetime:
C#
public class MyClass
{
private readonly IMyService _myService;
public MyClass(IMyService myService)
{
_myService = myService;
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IMyService, MyService>();
}
}
In this example, the MyClass class injects the IMyService interface with a scoped lifetime. This means that the same instance of the MyService class will be injected into the MyClass class for all requests within the same scope.
The service lifetime can be a useful way to manage the resources that are used by your application. For example, if you have a service that is expensive to create, you can register the service with a transient lifetime. This will ensure that the service is only created when it is needed.
If you have a service that needs to be shared across multiple requests, you can register the service with a scoped lifetime. This will ensure that the same instance of the service is used for all requests within the same scope.
If you have a service that needs to be shared across the entire application, you can register the service with a singleton lifetime. This will ensure that the same instance of the service is used for all requests within the application.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In .NET Core, service lifetimes determine how long a service instance will be alive. There are three types of service lifetimes:
The service lifetime can be specified when the service is registered with the dependency injection container. The following code shows how to register a service with a transient lifetime:
C#
In this example, the MyService class is registered with a transient lifetime. This means that a new instance of the MyService class will be created for each request.
The service lifetime can also be specified when the service is injected into a class. The following code shows how to inject a service with a scoped lifetime:
C#
In this example, the MyClass class injects the IMyService interface with a scoped lifetime. This means that the same instance of the MyService class will be injected into the MyClass class for all requests within the same scope.
The service lifetime can be a useful way to manage the resources that are used by your application. For example, if you have a service that is expensive to create, you can register the service with a transient lifetime. This will ensure that the service is only created when it is needed.
If you have a service that needs to be shared across multiple requests, you can register the service with a scoped lifetime. This will ensure that the same instance of the service is used for all requests within the same scope.
If you have a service that needs to be shared across the entire application, you can register the service with a singleton lifetime. This will ensure that the same instance of the service is used for all requests within the application.