Skip to content

Service Lifecycles in C#

service lifecycles in c#

In this article, we’ll learn about Service Lifecycles in C# by examples. In C#, Dependency Injection (DI) containers or Inversion of Control (IoC) containers are responsible for creating and managing the lifecycle of objects, including how they are created, how they are disposed, and how they are shared between components.

Imagine that we have the following service:

FLASH SALES
$29.95
NEW
$249.00
Become a job-ready C# developer!

And the following component needs it.

And we want to let an Inversion of Control Container instantiate the service and the component. 

In this example let’s use the dotnet’s IoC library. So install the Microsoft.Extensions.DependencyInjection Nuget package.

And create a new instance of the service collection.

There are three method of adding services to the collection. To see an example of each, let’s register MyService as a singleton, and MyComponent as a scoped for now.

 

For now, we use the AddSingletonand AddScoped extension methods to register the services with the DI container. These methods determine the lifecycle of the service, so to speak.

  • The AddSingleton method creates a single instance of the service for the entire application.
  • AddScoped creates a single instance of the service per scope (I’ll explain it later in this article).
  • AddTransient creates a new service instance each time it is requested (I’ll illustrate this later in this article).

You’ll see the difference better once we instantiate the services. But we need a service provider to instantiate the services. So let’s create one that can provide the dependency instances based on the configuration above.

 

Singleton

We registered MyService as a singleton. Now, if I use the providerto create two instances of MyService:

The instances are identical. I can prove it. 😉

This is the console’s output:

 

Scoped

If I create two instances of a scoped dependency:

Both variables refer to the same object within the same scope. And that’s how you can create scopes.

So the output is: 

 

To clarify what that means, let’s create each component instance in a different scope.

And test it:

So the output is:

So, scopes help us separate environments in which the instances are shared. You can resolve the service using the GetService method and control the scope of the service by creating a scope using the CreateScope method and resolving the service in that scope.

Transient

Transient is the simplest one. Each time it creates a new instance regardless of the scope. This is how I can register a transient dependency:

 

Now, if I ask the provider for the service twice:

 

Guess the output of the following test 😃

Right! component3 and component4 should refer to different instances. So the output is:

By the way, did you know that we offer a unique online course that boosts your C# career? Check it out here!

Conclusion

There are three methods for service dependency resolution when using Dotnet’s built-in IoC library.

  • The AddSingleton method creates a single instance of the service for the entire application.
  • AddScoped creates a single instance of the service per scope.
  • AddTransient creates a new service instance each time it is requested.

ASP.NET creates a scope per request. If you register a dependency as scoped, ASP.NET shares the instances of that dependency with all clients (clients are the objects that need the dependency in the Object-Oriented programming context). 

Leave a Reply

Your email address will not be published. Required fields are marked *