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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay