The Docs example about this topic aren't runnable. When I'm setting breakpoint in each of DI classes constructors they will trigger only once when application starts. How to catch the differences in behavior of these DI types (Transient, Singleton, Scoped)?
The rundown of these DI kinds in Docs wasn't clear to me.
Transient
Transient lifetime services are created each time they're requested. This lifetime works best for lightweight, stateless services.
Scoped
Scoped lifetime services are created once per request.
Warning
When using a scoped service in a middleware, inject the service into the Invoke or InvokeAsync method. Don't inject via constructor injection because it forces the service to behave like a singleton. For more information, see ASP.NET Core Middleware.
Singleton
Singleton lifetime services are created the first time they're requested (or when ConfigureServices is run and an instance is specified with the service registration). Every subsequent request uses the same instance. If the app requires singleton behavior, allowing the service container to manage the service's lifetime is recommended. Don't implement the singleton design pattern and provide user code to manage the object's lifetime in the class.
If you do not understand the definitions then you need to explain what you do not understand. It could be that you do not understand the lifetime of an object and scope in web applications?
For code example for Core MVC , this example from
asp.net core docs shows the difference .
To demonstrate the difference between these lifetime and registration options, consider a simple interface that represents one or more tasks as an operation with a unique identifier, OperationId . To make it clear which lifetime is being requested, we will
create one type per lifetime option:
namespace DependencyInjectionSample.Interfaces
{
public interface IOperation
{
Guid OperationId { get; }
}
public interface IOperationTransient : IOperation
{
}
public interface IOperationScoped : IOperation
{
}
public interface IOperationSingleton : IOperation
{
}
public interface IOperationSingletonInstance : IOperation
{
}
}
You could implement these interfaces using a single class, Operation, that accepts a Guid in its constructor, or uses a new Guid if none is provided.
using DependencyInjectionSample.Interfaces;
namespace DependencyInjectionSample.Models
{
public class Operation : IOperationTransient,
IOperationScoped,
IOperationSingleton,
IOperationSingletonInstance
{
public Operation() : this(Guid.NewGuid())
{
}
public Operation(Guid id)
{
OperationId = id;
}
public Guid OperationId { get; private set; }
}
}
Then , in ConfigureServices, add each type to the container according to its named lifetime:
Register an OperationService that depends on each of the other Operation types, so that it will be clear within a request whether this service is getting the same instance as the controller, or a new one, for each operation type
using DependencyInjectionSample.Interfaces;
namespace DependencyInjectionSample.Services
{
public class OperationService
{
public OperationService(IOperationTransient transientOperation,
IOperationScoped scopedOperation,
IOperationSingleton singletonOperation,
IOperationSingletonInstance instanceOperation)
{
TransientOperation = transientOperation;
ScopedOperation = scopedOperation;
SingletonOperation = singletonOperation;
SingletonInstanceOperation = instanceOperation;
}
public IOperationTransient TransientOperation { get; }
public IOperationScoped ScopedOperation { get; }
public IOperationSingleton SingletonOperation { get; }
public IOperationSingletonInstance SingletonInstanceOperation { get; }
}
}
To demonstrate the object lifetimes within and between separate individual requests to the application, the sample includes an OperationsController
Member
317 Points
354 Posts
How to programmatically check difference between kinds of ASP.NET Core DI?
Oct 08, 2018 03:20 PM|Alex9|LINK
Hello all!
The Docs example about this topic aren't runnable. When I'm setting breakpoint in each of DI classes constructors they will trigger only once when application starts. How to catch the differences in behavior of these DI types (Transient, Singleton, Scoped)? The rundown of these DI kinds in Docs wasn't clear to me.
Thank you.
All-Star
37161 Points
15007 Posts
Re: How to programmatically check difference between kinds of ASP.NET Core DI?
Oct 08, 2018 03:49 PM|mgebhard|LINK
Unclear. Is there anyway you can provide a link to the example code that do not run?
Correct and the expected behavior when configuring DI.
The documentation covers the definitions. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1
If you do not understand the definitions then you need to explain what you do not understand. It could be that you do not understand the lifetime of an object and scope in web applications?
Member
317 Points
354 Posts
Re: How to programmatically check difference between kinds of ASP.NET Core DI?
Oct 09, 2018 07:11 AM|Alex9|LINK
There is a code for Razor Pages. Where is the full project code example for Core MVC?
Member
630 Points
209 Posts
Re: How to programmatically check difference between kinds of ASP.NET Core DI?
Oct 09, 2018 10:12 AM|Sherry Chen|LINK
Hi Alex9 ,
For code example for Core MVC , this example from asp.net core docs shows the difference .
To demonstrate the difference between these lifetime and registration options, consider a simple interface that represents one or more tasks as an operation with a unique identifier, OperationId . To make it clear which lifetime is being requested, we will create one type per lifetime option:
You could implement these interfaces using a single class, Operation, that accepts a Guid in its constructor, or uses a new Guid if none is provided.
Then , in ConfigureServices, add each type to the container according to its named lifetime:
Register an OperationService that depends on each of the other Operation types, so that it will be clear within a request whether this service is getting the same instance as the controller, or a new one, for each operation type
To demonstrate the object lifetimes within and between separate individual requests to the application, the sample includes an OperationsController
Best Regards,
Sherry
Member
317 Points
354 Posts
Re: How to programmatically check difference between kinds of ASP.NET Core DI?
Oct 09, 2018 01:54 PM|Alex9|LINK
Hi, Sherry! Thank you. Is it right, that I can inject these DI only in class constructors?
All-Star
37161 Points
15007 Posts
Re: How to programmatically check difference between kinds of ASP.NET Core DI?
Oct 09, 2018 02:56 PM|mgebhard|LINK
Correct, ASP.NET Core uses constructor injection.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1