← Back to list

What will happen if you inject a Scoped service into a Singleton service in .NET Core?

.NET Core is a powerful framework developed by Microsoft that provides Dependency Injection (DI) as a built-in feature. The DI design…

Code Crack in .Net Programming · 2025-03-29 12:58 · 4 claps · 2.7 min read paywalled
#dependency-injection #scoped #singleton #inject #dotnet-core
Open on Medium ↗

What will happen if you inject a Scoped service into a Singleton service in .NET Core?

Photo by Ilya Pavlov on Unsplash

Photo by Ilya Pavlov on Unsplash

.NET Core is a powerful framework developed by Microsoft that provides Dependency Injection (DI) as a built-in feature. The DI design pattern helps developers keep code modular and reusable. The core concept of DI is the different lifetime configurations — Singleton, Scoped, and Transient. But what happens when a Scoped Service is injected into a Singleton Service?

In this article, we will discuss in detail the difference between Scoped and Singleton Services, how injecting Scoped Services into Singleton Services can cause problems in .NET Core, and methods to resolve it.

Concept of Singleton and Scoped Service

DI Container has three types of lifetime —

  1. Singleton: Only one instance is created during the application’s runtime, and it is shared throughout the application’s entire lifetime.
  2. Scoped: An instance is created for the duration of an HTTP request. Each new request gets a new instance.
  3. Transient: A new instance is created every time a service is instantiated.

The instance of Scoped Service exists for the duration of the HTTP request. But the instance of Singleton Service is created at the beginning of the application and exists until the end of the application’s lifetime.

[embed]Can we override a Constructor? One of the important concepts in object-oriented programming (OOP) is the constructor. A constructor is a special type…medium.com

Case of Injecting Scoped Service into Singleton Service

When you inject Scoped Service into Singleton Service, a problem can arise. The main reason for this is that the instance of Singleton Service always remains the same, but the instance of Scoped Service changes with each HTTP request. This creates a conflict between lifecycles.

Let’s look at a code example —

public class ScopedService
{
    public Guid Id { get; set; } = Guid.NewGuid();
}

public class SingletonService
{
    private readonly ScopedService _scopedService;

    public SingletonService(ScopedService scopedService)
    {
        _scopedService = scopedService;
    }

    public Guid GetScopedServiceId()
    {
        return _scopedService.Id;
    }
}

In Startup.cs

services.AddScoped<ScopedService>();
services.AddSingleton<SingletonService>();

Here ScopedService is injected into SingletonService. ScopedService will create a new instance on every HTTP request, but SingletonService will keep the same instance. Therefore, changing the instance of ScopedService can affect any HTTP request.

This process can cause the following problems —

  1. Inconsistent Behavior: A new instance of ScopedService cannot make changes to the SingletonService. This results in the SingletonService retaining the previous instance, which can create data inconsistencies.
  2. Thread Safety Issues: Multiple HTTP requests using the same Singleton instance can cause thread safety issues.
  3. Memory Leaks: Incorrect dependency lifetimes can cause memory leaks.

Solution

If it is necessary to inject Scoped Service into Singleton Service, then the following methods should be considered —

  1. Use Factory Method: You can use factory method to create new instance of Scoped Service.
services.AddSingleton<SingletonService>(sp =>
{
    var scopedService = sp.GetRequiredService<ScopedService>();
    return new SingletonService(scopedService);
});
  1. Use IServiceProvider: You can use IServiceProvider in a Singleton service to access an instance of a scoped service.
public class SingletonService
{
    private readonly IServiceProvider _serviceProvider;

    public SingletonService(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public Guid GetScopedServiceId()
    {
        var scopedService = _serviceProvider.GetRequiredService<ScopedService>();
        return scopedService.Id;
    }
}
  1. Dependency Redesign: Reconsider your dependency design and make necessary changes to avoid Scoped and Singleton conflicts.

[embed]Don’t miss this AI feature in Visual Studio Visual Studio is one of Microsoft’s leading platforms for developers. It is not just about writing code, but also about…medium.com

Lifetime Management is very important during Dependency Injection. Injecting Scoped service into Singleton service can cause some unwanted issues, which can affect the performance and maintainability of the application. These issues can be avoided by using solutions like Factory method or IServiceProvider.

By creating dependencies with proper lifetime understanding, we can make .NET Core application more efficient and effective.

Enjoyed this article? Support my work by buying me a coffee! It helps me create more content like this. 💙 👉 **Buy Me a Coffee**


메타데이터
post_id
c52bc3ec5656
slug
what-will-happen-if-you-inject-a-scoped-service-into-a-singleton-service-in-net-core-c52bc3ec5656
url
https://medium.com/c-sharp-programming/what-will-happen-if-you-inject-a-scoped-service-into-a-singleton-service-in-net-core-c52bc3ec5656
canonical_url
https://medium.com/c-sharp-programming/what-will-happen-if-you-inject-a-scoped-service-into-a-singleton-service-in-net-core-c52bc3ec5656
author_url
https://medium.com/@CodeCrack
status
ok
fetched_at
2026-07-20 12:13:53