AddScoped DI And Thread Safety

I am trying to understand exactly what happens with AddScoped after a conversation with a colleague. I have always interpreted that AddScoped create a new instance of the manager class in a service for each request. My colleague is telling me that even if there is a new instance of the class it is still not thread safe because those instances could be in the same thread.

Interface

public interface IManager
{
    string AuthValue { get; set; }
    Task<bool> DoSomethingAsync();
    Task<bool> DoSomethingElseAsync();
}

Manager

public class Manager : IManager
{
    protected string _AuthValue;

    string IManager.AuthValue { get => _AuthValue; set => _AuthValue = value; }

    public async Task<bool> DoSomethingAsync()
    {
        bool result = false;
        ///somecode that could take a few seconds to run
        if (_AuthValue == "123")
        {
            result = true;
        }

        return result;
    }

    public async Task<bool> DoSomethingElseAsync()
    {
        bool result = false;
        ///somecode that could take a few seconds to run
        if (_AuthValue == "456")
        {
            result = true;
        }

        return result;
    }
}

program.cs

builder.Services.AddScoped<IManager, Manager>();
var app = builder.Build();

Service

private readonly ILogger<MyService> _logger;
private IManager _manager;
public MyService(ILogger<MyService> logger, IManager manager)
{
    _logger = logger;
    _manager = manager;
}

public override Task<bool> SayHello(HelloRequest request, ServerCallContext context)
{
    _manager.AuthValue = context.RequestHeaders.GetValue("Auth");
    _manager.DoSomethingAsync();
}

public override Task<bool> SayHelloAgain(HelloRequest request, ServerCallContext context)
{
    _manager.AuthValue = context.RequestHeaders.GetValue("Auth");
    _manager.DoSomethingElseAsync();
}

I am not advocating for this way of doing things, but I just want to understand better.
My college is saying that when multiple concurrent requests come in because the scoped could be using the same thread there is a chance that when a request comes in for “SayHello” and begins executing “DoSomethingAsync” if a new request comes in for “SayHelloAgain” with a different “AuthValue”, while “SayHello” in first request is still running, that it will potentially update the AuthValue for the first request because the service is running on the same thread.
I have always understood that using DI as scoped meant that each request spun up a new instance of that manager class to be used so things like this could not happen. If I am wrong, can it please be explained how adding a class as scoped is meant to be used.

I have tried searching multiple times even making past page 1 of results. Everything seems to read as my understanding is right but there has not been anything specific enough to my scenario to confirm.

  • What Happens When the Same Thread Serves a Different Request?

Thread Reuse: ASP.NET Core uses a thread pool to handle incoming requests. Threads are reused to improve performance. This means that the same thread might handle multiple requests over its lifetime.

Scoped Service Lifetime: The lifetime of a scoped service is tied to the DI scope, not the thread. Each HTTP request creates a new DI scope, and all scoped services are resolved within this scope.

Isolation Between Requests: Even if the same thread handles multiple requests, the scoped services from one request are not carried over to the next. Each request has its own set of scoped service instances.

  • Implications:

No Cross-Request Leakage: You don’t have to worry about scoped services leaking data or state between requests, even on the same thread.

Thread Safety: Since each request gets its own instance of scoped services, you avoid concurrency issues related to shared state.


Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật