Explore tailored services designed to solve business challenges and support growth
Discover industry-focused expertise built to meet unique business needs
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.
See how a unified data strategy built faster insights and scaled analytics.
Explore our digital products built to streamline work and drive growth every day
Meet our product partners who enhance our solutions and expand client value
Explore a better way to speed up testing and improve release quality.
Access blogs, case studies, events, and insights that support smarter decisions.
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.
Learn who we are, what we do, and the values that drive our growth
Stay updated with Fortude news, events, stories, and company highlights.
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
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.
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
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:
Figure 01: Key components in the built-in IoC container
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:
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:
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();
}
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();
}
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
Automated page speed optimizations for fast site performance