Service lifecycle in asp.net core

 Service lifecycle is the term that describes how long a service instance lives and when it is created and disposed by the dependency injection container. ASP.NET Core supports three types of service lifecycles: scoped, transient, and singleton. The difference between them is:

  • Scoped: A scoped service is created once per client request (connection). It means that the same service instance is used throughout the request processing pipeline, but a new instance is created for each new request. Scoped services are suitable for scenarios where you need to maintain some state or context within a request, such as database connections, user sessions, etc.
  • Transient: A transient service is created each time it is requested from the service container. It means that different service instances are used in different places within the same request processing pipeline, and also across different requests. Transient services are suitable for scenarios where you need lightweight and stateless services, such as validators, helpers, etc.
  • Singleton: A singleton service is created only once when the application starts and reused throughout the application lifetime. It means that the same service instance is used in all requests processing pipelines and across all requests. Singleton services are suitable for scenarios where you need to share some global or persistent state or functionality across the application, such as configuration, logging, caching, etc.


Scoped Scope Lifecycle

Scoped services are created once per request. This is useful when you need a service to maintain state across a single request but do not want it to persist beyond that request.

Example: Database Context

Consider an application that uses Entity Framework Core. The DbContext is typically registered with a Scoped lifetime because you want a new instance for each HTTP request, and you want to share the same instance among different components during the processing of that request.

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    // Register other services
    services.AddScoped<IMyScopedService, MyScopedService>();
}

// IMyScopedService.cs
public interface IMyScopedService
{
    void DoWork();
}

// MyScopedService.cs
public class MyScopedService : IMyScopedService
{
    private readonly ApplicationDbContext _context;

    public MyScopedService(ApplicationDbContext context)
    {
        _context = context;
    }

    public void DoWork()
    {
        // Use _context to interact with the database
    }
}

Transient Scope Lifecycle

Transient services are created each time they are requested. This is useful for lightweight, stateless services where you want a new instance each time to ensure there is no shared state.

Example: Logging Service

Consider a service that generates unique identifiers or performs stateless operations. You might want this service to be transient to ensure a new instance with each request.

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IMyTransientService, MyTransientService>();
}

// IMyTransientService.cs
public interface IMyTransientService
{
    string GenerateUniqueIdentifier();
}

// MyTransientService.cs
public class MyTransientService : IMyTransientService
{
    public string GenerateUniqueIdentifier()
    {
        return Guid.NewGuid().ToString();
    }
}


Singleton Scope Lifecycle

In ASP.NET Core, the ILogger service is typically registered as a Singleton. This is because logging is generally a cross-cutting concern that doesn't hold any state specific to a request or operation, and having a single instance shared across the application is both efficient and logical.

Here's why ILogger is registered as a Singleton:

  1. Performance: Creating a new instance of the logger for each request or operation would be inefficient. Using a singleton instance minimizes overhead.
  2. Consistency: Logging often needs to be consistent across the application, and a singleton ensures that all components use the same logger configuration and behavior.
  3. State Management: The logger itself doesn't maintain any per-request state. Instead, it logs events and messages that can be handled in a consistent manner regardless of the request.

Here's an example of how the ILogger is typically used:

public class MyService
{
    private readonly ILogger<MyService> _logger;

    public MyService(ILogger<MyService> logger)
    {
        _logger = logger;
    }

    public void DoWork()
    {
        _logger.LogInformation("Doing work");
        // Do some work
    }
}


Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form