Add Transient vs Add Scoped vs Add Singleton Object lifetime in .Net Core
In the context of dependency injection in .NET Core (or ASP.NET Core), AddScoped and AddTransient are methods used to register services with the built-in dependency injection container. These methods are part of the IServiceCollection interface and are typically used in the ConfigureServices method of the Startup class.
Lifetime:
AddScoped:
AddScoped: Suitable for services that should be shared within the scope of a single request or operation. The same instance will be used throughout the entire scope. AddTransient: Suitable for lightweight, stateless services where a new instance can be created every time the service is requested. Now, let's demonstrate these differences with a simple example. Consider a basic service interface and its implementation:
Lifetime:
AddScoped:
This method registers a service with a scoped lifetime. A new instance of the service is created for each scope, and it is reused within that scope.
AddTransient: This method registers a service with a transient lifetime. A new instance of the service is created each time it is requested.
Usage:AddScoped: Suitable for services that should be shared within the scope of a single request or operation. The same instance will be used throughout the entire scope. AddTransient: Suitable for lightweight, stateless services where a new instance can be created every time the service is requested. Now, let's demonstrate these differences with a simple example. Consider a basic service interface and its implementation:
public interface IMyService
{
void DisplayMessage();
}
public class MyService : IMyService
{
private readonly Guid _instanceId;
public MyService()
{
_instanceId = Guid.NewGuid();
}
public void DisplayMessage()
{
Console.WriteLine($"Instance {_instanceId}: Hello from MyService!");
}
}
Now, in the Startup class, let's register this service using both AddScoped and AddTransient:
public void ConfigureServices(IServiceCollection services)
{
// Using AddScoped
services.AddScoped();
// Using AddTransient
services.AddTransient();
}
In your application, you can inject IMyService into controllers or other components. The difference in behavior will be evident when the service is used within different scopes. For example, in the case of AddScoped, the same instance will be reused within a scope (e.g., a single HTTP request), while with AddTransient, a new instance will be created each time the service is requested.
public class MyController : Controller
{
private readonly IMyService _myServiceScoped;
private readonly IMyService _myServiceTransient;
public MyController(IMyService myServiceScoped, IMyService myServiceTransient)
{
_myServiceScoped = myServiceScoped;
_myServiceTransient = myServiceTransient;
}
public IActionResult Index()
{
_myServiceScoped.DisplayMessage();
_myServiceTransient.DisplayMessage();
return View();
}
}
By observing the output and behavior in different scenarios, you can see how the choice between AddScoped and AddTransient affects the lifecycle and usage of the registered service instances.
Comments
Post a Comment