DEV Community

Cover image for How to use Scoped service from Singleton Service in .Net Core
.Net Labs
.Net Labs

Posted on

How to use Scoped service from Singleton Service in .Net Core

We should be very careful about lifetime of objects in .net core.

Suppose we created Two services one register as Singleton and other as Scoped.

Code

public interface IScopedService
 {  
 }

 public interface ISingletonService
 {

 }

/// Scope service
 public class ScopedService : IScopedService
 {
     public ScopedService()

     {

     }

 }
/// Singelton Service , here we are injecting scope service object
 public class SingletonService : ISingletonService
 {
     public SingletonService(IScopedService svc)

     {

     }

 }
Enter fullscreen mode Exit fullscreen mode

Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


builder.Services.AddSingleton<ISingletonService, SingletonService>();

builder.Services.AddScoped<IScopedService, ScopedService>();
Enter fullscreen mode Exit fullscreen mode

Image description

Understanding of code.

we have created two classes Singelton and Scoped and we have injected scoped inside singleton service.

When we run application, we will get below error

Image description

Understanding Error

We have 3 types of service lifetimes:

  1. Singleton — they’re created first time they’re request, and every time after that the same instance will be reused

  2. Scoped — they’re created once per the request (connection)

  3. Transient are created every time you request them from the DI container

_Here we are trying to inject a object that will be created on each user scope request so that required to dispose after each and every request. Here Singelton object will never be going to dispose but scoped object is required to dispose, so this is conflict.
_

As per above explanation we should almost never consume scoped service or transient service from a singleton. You should also avoid consuming transient service from a scoped service.

What is happening when you consume scoped service from a singleton service is known as a captive dependency. It occurs when a service intended to live for a short(er) amount of time gets held by a service that lives for a more extended amount of time. This almost always suggests that you should change something in your design.

*Solution *

If there is need to use Scoped service inside singleton then we need to create scope manually. A new scope can be created by injecting an IServiceScopeFactory into your singleton service. The IServiceScopeFactory has a CreateScope method, which is used for creating new scope instances. IServiceScopeFactory is singelton in itself that’s why it works here.

Please Click here for complete implementation

Top comments (0)