Difference between AddTransient, AddScoped and AddSingleton in .NET Core
.NET Core provides built in Dependency Injection, In most of the cases, we create Interfaces and inherit those interfaces into our actual class implementation, to declare the Dependency in .NET Framework we used to add third party tool like UnityContainer, but in .NET Core, we have inbuilt Services such as AddSingleton(), AddScoped() and AddTransient() to declare the dependency between classes, following article helps you to understand the difference between AddSingleton, AddScoped and AddTrasient:
AddSingleton vs AddScoped vs AddTransient in .NET Core:
AddSingleton:
With Singleton, an new instance is created when first time the service is requested and the same instance is used for all the request, even for every new http request it uses the same reference. It is like static where you get the same value of the property even if property accessed from different instance. See the below example to understand more on AddSingleton()
AddScoped:
With Scoped, a new instance is created for every new http request, but during http request, it uses the same reference for all the instances. See the below example to understand more on AddScoped().
AddTransient:
With transient, new instance is created for every request. See the below example to understand more on AddTransient()
Example of AddSingleton(), AddScoped() & AddTransient()
Declare Interface for Singleton Service
- interface IRandomValueSingletonService
- {
- int GetRandomValue();
- }
Declare Interface for Scoped Service
- interface IRandomValueScopedService
- {
- int GetRandomValue();
- }
Declare interface for Transient Service
- interface IRandomValueTransientService
- {
- int GetRandomValue();
- }
Define class which will implement all above interface
- public class RandomValueService : IRandomValueSingletonService, IRandomValueScopedService, IRandomValueTransientService
- {
- private int randomValue = new Random().Next();
- public int GetRandomValue() => randomValue;
- }
Startup.cs =>ConfigureServices() method in .NET Core: Add all the Services in the IServiceCollection at the start of the application in Startup.cs class. Here we are adding all the services which will be using Dependency Injection and will be injecting the services. In this example we are using View Page (Index.razor) to inject the Dependency, check the below code to see how to Inject the Services into Blazor View page.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddRazorPages();
- services.AddServerSideBlazor();
- services.AddSingleton<IRandomValueSingletonService, RandomValueService>();
- services.AddScoped<IRandomValueScopedService, RandomValueService>();
- services.AddTransient<IRandomValueTransientService, RandomValueService>();
- }
Index.razor (Blazor View Page): We are injecting the services and will be calling GetRandomValue() method through instances of the services.
- @page "/"
- @inject BlazorServerApp.Data.IRandomValueSingletonService singletonInstance1
- @inject BlazorServerApp.Data.IRandomValueSingletonService singletonInstance2
- @inject BlazorServerApp.Data.IRandomValueScopedService scopedInstance1
- @inject BlazorServerApp.Data.IRandomValueScopedService scopedInstance2
- @inject BlazorServerApp.Data.IRandomValueTransientService transientInstance1
- @inject BlazorServerApp.Data.IRandomValueTransientService transientInstance2
- <div style="float:left;padding:20px;border:1px solid;">
- <h5>AddSingleton(), AddScoped(), AddTransient() Example</h5>
- <table border="1" cellspacing="5" cellpadding="5">
- <tr>
- <td>Instance</td>
- <td>Singleton</td>
- <td>Scoped</td>
- <td>Transient</td>
- </tr>
- <tr>
- <td>Instance 1</td>
- <td>@singletonRandomVal1</td>
- <td>@scopedRandomVal1</td>
- <td>@transientRandomVal1</td>
- </tr>
- <tr>
- <td>Instance 2</td>
- <td>@singletonRandomVal2</td>
- <td>@scopedRandomVal2</td>
- <td>@transientRandomVal2</td>
- </tr>
- </table>
- </div>
- @code{
- int singletonRandomVal1, singletonRandomVal2, scopedRandomVal1, scopedRandomVal2, transientRandomVal1, transientRandomVal2;
- protected override void OnInitialized()
- {
- singletonRandomVal1 = singletonInstance1.GetRandomValue();
- singletonRandomVal2 = singletonInstance1.GetRandomValue();
- scopedRandomVal1 = scopedInstance1.GetRandomValue();
- scopedRandomVal2 = scopedInstance2.GetRandomValue();
- transientRandomVal1 = transientInstance1.GetRandomValue();
- transientRandomVal2 = transientInstance2.GetRandomValue();
- }
- }
Output:
As you can see in the above output, Instance 1 and Instance 2 values of Singleton Instance Random Values are Same.
Also for Scoped Instance, both the instance values are same, but for Transient, both the instance Random Values are different.
When we make new Http request to the same page (ie. Refresh the Page on the browser), then it will show the following output:
As you can see from the above 2 outputs, value of both the Singleton instance are same and even for every new http request values doesn't change.
For Scoped instance new values are assigned for new http request but for all the instance in that http request, values of service are same.
In case of Transient, values are different for each instance and for every http request.