Service Offerings

Explore tailored services designed to solve business challenges and support growth

Industries

Discover industry-focused expertise built to meet unique business needs

Partners

Meet our service partners who strengthen delivery and support client success

Meet our service partners who enhance our capabilities, strengthen service delivery, and help drive successful outcomes for our clients.

Learn how to modernize data foundations to enable trusted, scalable AI.

Global supply chain leader in apparel embarks on unified analytics strategy with Microsoft Fabric

See how a unified data strategy built faster insights and scaled analytics.

Microsoft solutions partner for Data & AI

Products

Explore our digital products built to streamline work and drive growth every day

Partners

Meet our product partners who enhance our solutions and expand client value

AI-powered automated
 regression testing: Your
kickstart for 2026

Explore a better way to speed up testing and improve release quality.

Resources

Access blogs, case studies, events, and insights that support smarter decisions.

Latest Resources

CASE STUDY

HamiltonJet transforms regression testing on Infor CloudSuite with Fortest

NEWS

Fortude earns Microsoft Azure Infrastructure Solutions designation

Our People

Discover a culture where you can grow and shape what’s next

Everyone can grow at Fortude

We believe in creating a global workplace where everyone can grow. This is amplified by our teams, who say the best thing about Fortude is our culture, one that is brought to life by a diverse team that spans across continents.

Latest

Fortude builds momentum for women in tech with all-female Ignite 2.0 tech internship

Aug 22, 2025

Pioneering innovation and inculcating learning in the age of AI

Aug 01, 2024

About us

Learn who we are, what we do, and the values that drive our growth

News & events

Stay updated with Fortude news, events, stories, and company highlights.

Contact us

Get in touch with our team to ask questions or start a conversation

Your nearest office- Sri Lanka

Fortude (Pvt) Ltd
146 Kynsey Road, Colombo 7, Sri Lanka

Email – talk-to-us@fortude.co
Phone – +94 11 453 1531

What defines us goes beyond what we do

Every day, we bring together diverse perspectives, strong leadership and responsible thinking to build a business that creates lasting value for our clients, people and communities.

Fortude secures major Solutions Partner achievement with Analytics on Microsoft Azure Specialization

Office locations

Data & AI

ASP.NET Core Dependency Injection and Service Lifetimes

3 MIN READ

July 19, 2021

Share

Introduction

ASP.NET Core supports the Dependency Injection (DI) software design pattern that allows registering services and controlling how these services are instantiated and injected in different components. Also, ASP.NET Core has a built-in IoC container, eliminating the need to use a third-party container to inject the service. Some services will be instantiated for a short time and will be available only in a particular component and request. Some will be instantiated just once and will be available throughout the application.

To inject application services automatically, first register them with the IoC container.

There are two key components in the built-in container:

  1. IServiceCollection – All the services will register here.
  2. IServiceProvider – Implementation supports the constructor injection.

Figure 01: Key components in the built-in IoC container

Service lifetimes

The built-in IoC container manages the lifetime of the registered service type. It automatically disposes of the service instance according to the specified lifetime.

ASP.NET Core services support three types of lifetimes:

Singleton

A single instance of the registered service class is created and stored in the memory. It is re-used across the application when multiple requests come into the application. Thus, we can use the singleton lifetime for services that are expensive to instantiate.

When using the singleton type in real use cases, you need to consider the following:

  1. The service should not be dependent on other service types.
  2. A singleton instance lasts the entire lifetime of the application until it is terminated. Therefore, the instance should be disposed properly. If not, it can cause a performance issues in the application.

Figure 02: Singleton instances are created the first time they are requested

How to register the singleton service .NET Core during startup.

public void ConfigureServices(IServiceCollection services)

{
services.AddSingleton();

}

Scoped

Think of a scoped lifetime as a service instance that is created per web request. The request will be considered as scope. This lifetime works best for lightweight, stateless services.

A common usage of this approach is to declare the DbContext class of Entity Framework Core. The lifetime of a DbContext begins when the instance is created and ends when the instance is disposed.

Figure 03: A scoped lifetime denotes that services are created once per client web request.

How to register the scoped service .NET Core during startup.

public void ConfigureServices(IServiceCollection services)
{
services.AddScoped();
}

Transient

Figure 04: Transient lifetime services are created each time they are requested from the service container

Each time a service is requested, a new instance will be created. This is the most common and always the safest option if you are worried about multithreading.

If we inject the same service into a Controller and a View in an MVC application, two different instances will be created in the controller and the view.

How to register the transient service .NET Core during startup.

public void ConfigureServices(IServiceCollection services)

{ services.AddTransient();

}

“In terms of the lifetime of the instance, the Singleton object gets the highest life per instantiation, followed by a Scoped service object and the least by a Transient object.”

Written by

FAQ

CONTENTS

Introduction
Service lifetimes
Singleton
Scoped
Transient

Receive the latest
Fortude Newsletter
updates.

Share

Receive the latest
Fortude Newsletter
updates.

Share