---
title: "What are service lifetimes in .NET Core?"  
description: "What are service lifetimes in .NET Core?"  
author: "Sanjay Goenka"  
published: 2023-03-06  
updated: 2023-06-18  
canonical: https://www.mindstick.com/forum/157470/what-are-service-lifetimes-in-dot-net-core  
category: "asp.net core"  
tags: ["asp.net", "asp.net core"]  
reading_time: 3 minutes  

---

# What are service lifetimes in .NET Core?

**What are [service](https://www.mindstick.com/articles/105963/online-thesis-writing-service-for-college-kids-with-disabilities) lifetimes in .NET Core?**

## Replies

### Reply by Aryan Kumar

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#

```plaintext
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#

```plaintext
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.


---

Original Source: https://www.mindstick.com/forum/157470/what-are-service-lifetimes-in-dot-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
