6

Can anybody help me to understand following in context to Dependency Injection in Asp.Net 5 and object dispose.

I need to understand if my Service implements IDispose interface ,who will call dispose method.

public void ConfigureServices(IServiceCollection services)
{
 services.AddTransient<IService, Service>();
 services.AddScoped<IService, Service>();
 services.AddSingleton<IService, Service>();
 services.AddInstance<IService, Service>();
} 
Nkosi
249k38 gold badges472 silver badges506 bronze badges
asked Feb 19, 2016 at 6:37

1 Answer 1

4

IServiceCollection contains the set of services available in your application. You defines the services you want to use and their lifetime, and the application will instantiate and dispose them for you.

There are 4 different lifetimes :

Transient

Transient lifetime services are created each time they are requested. This lifetime works best for lightweight, stateless service.

Scoped

Scoped lifetime services are created once per request.

Singleton

Singleton lifetime services are created the first time they are requested, and then every subsequent request will use the same instance. If your application requires singleton behavior, allowing the services container to manage the service’s lifetime is recommended instead of implementing the singleton design pattern and managing your object’s lifetime in the class yourself.

Instance

You can choose to add an instance directly to the services container. If you do so, this instance will be used for all subsequent requests (this technique will create a Singleton-scoped instance). One key difference between Instance services and Singleton services is that the Instance service is created in ConfigureServices, while the Singleton service is lazy-loaded the first time it is requested.

The asp.net 5 official documentation is great, take time to read it : http://docs.asp.net/en/latest/fundamentals/dependency-injection.html


The documentation doesn't mention how exactly the dependencies lifetimes are handled by the dependency injection service but if you search in the code, you will find the ServiceProvider class, who manages the lifetimes : ServiceManager class

To be a little more specific, when a scope is created, the service scope factory returns a new service scope, who is instanciated with a service provider. When the dependency injection service have to dispose a service, he calls the service scope's dispose method, who calls the service provider's dispose method.

How the service provider works ? He has all the service scopes in a property, named _resolvedServices, and all the transiant disposables in a property named _transientDisposables. When the dispose() method of the service provider is called, he loops on all the items he has in this two properties, and for each object calls his dispose method.

You have all the source code here : Dependency Injection source code

answered Feb 19, 2016 at 8:18
Sign up to request clarification or add additional context in comments.

7 Comments

Do the IService resource will be cleaned just like what c# using statement does
Using statement defines a little scope of lifetime for object he uses, himself. Dependency Injection is a little different, and more complex because application can resolves a lot of things for you. For example, if you have a service who has a Repository in private property, repository who has your EF db context in private property, you have just to impements constructors with their needs, add each services in your startup class and the application will manage their dependencies for you. Anyway, the services will be cleaned when the application decide to, depends of the lifetime you gave them.
This is why the choice of the lifetime you give to each service is very important and needs to be considered.
Ok, If I have IDbClient as service injected which closes and dispose IConnection by implementing IDisposable interface. Now If I have injected as transient then after fulfilling Http Request and database work, my dbconnection will be closed and move to pool.
This answer describes when services are created. This question is when are they disposed. Who is responsible for the disposal? The DI framework or the code into which the services are injected? If the DI framework, how does it know when the service is no longer used?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.