Exploring Transient, Scoped, and Singleton using .NET and C#

A Quick Guide to Transient, Scoped, and Singleton in C#

Abdelmajid BACO
3 min readDec 30, 2022

--

Photo by Vlad Zinculescu on Unsplash

In C#, the Transient, Scoped, and Singleton scopes are options for controlling the lifetime of objects which are created by dependency injection.

Transient

Transient objects are created each time they are requested. This means that if a Transient is injected into multiple consumers, each consumer will receive a separate instance of the object.

Scoped

Scoped objects are created once per request. This means that if a Scoped is injected into multiple consumers within the same request, they will all receive the same instance of this object. However, if the object is injected into multiple consumers in different requests, each request will receive its own instance of the object.

Singleton

Singleton objects are created the first time they are requested, and the same instance is used for all subsequent requests. This can be useful if you want to ensure that all consumers of a service are using the same instance of the object.

It is important to note that the Singleton should be used with caution. If for example the Singleton instance is modified simultaneously by multiple consumers. This can cause concurrency issues.

Code Example

Here is an example of using the scopes in a .NET project. This example shows how to register a service with .NET dependency injection system, and how to inject and use the service in a consumer class.

First, let’s define the service interface and its implementation:

public interface IMyService
{
void DoWork();
}

public class MyService : IMyService
{
public void DoWork()
{
Console.WriteLine("Doing work in MyService");
}
}

Next, we can inject and use the IMyService service in a consumer class:

public class Consumer
{
private readonly IMyService _myService;

public Consumer(IMyService myService)
{
_myService = myService;
}

public void DoWork()
{
_myService.DoWork();
}
}

--

--

Abdelmajid BACO

Senior Full Stack .Net / Angular Developer, Cloud & Azure DevOps, Carrier Manager, Husband, and Father.