What are the possible solution(s) for requiring access to Scoped service inside a Singleton one?

I’ve got a scenario where I need to access a service that is scoped inside a singleton one. The concrete example is as follows:

public class CustomMapper(IUserRequestData data) : IMapperConfiguration 
{
    public void Map()
    {
        TypeAdapterConfig<User, UserResponse>.NewConfig()
            .Map(
                u => u.GroupName,
                src => src.Groups.FirstOrDefault(g => 
                          g.CnId == data.GetCurrentCnId()
                       ) != null
                    ? src.Groups.FirstOrDefault(
                        g => g.CnId == data.GetCurrentCnId()
                      )!.Name
                    : ""
            );
        TypeAdapterConfig<UserUpdateInput, User>.NewConfig();
    }
}

IUserRequestData is provided as a scoped service, because it holds some information related to the current user’s request (similar to HttpContext.Items). I could make IMapperConfiguration scoped as well, but it doesn’t make much sense to do so, since I’d be calling .Map() for every request.

Given that the mapping function would be executed only when there is a request, it would not be an issue to keep it as is, with the scoped service being injected into the singleton one, but .NET, correctly, won’t allow me to.

Is there a solution I could use to have access to this scoped service inside the singleton one or the correct solution would be to make the IMapperConfiguration scoped as well and have it be called on every single request? :/

I’d like to fix this using DI, if possible. I’ve tried using delegates, maybe incorrectly. I’ve also seen this solution: /a/48368934/578808 but I’m not sure that’d be appropriate for my case, since the scope needs to be started by the request and not manually. Please keep in mind that my understanding of this solution could be incorrect, though.

Please let me know if something’s not clear, I’ll try to elaborate.

On .NET 8

You could use IHttpContextAccessor to access current HTTP context and from there resolve the service, which should have HTTP request scope.

But before you do that, it seems that IHttpContextAccessor is also scoped service, but accordingly to this github thread, we can register it as singleton, so here’s my sample registartion:

builder.Services.AddScoped<Scoped>();
builder.Services.AddSingleton<Singleton>();
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

with having classes implemented like below. Note that Singleton uses accessor to acess current HttpContext, and use it to obtain required service, in our case scoped service Scoped:

public class Scoped { }
public class Singleton 
{
    private readonly IHttpContextAccessor _accessor;

    public Singleton(IHttpContextAccessor accessor) => _accessor = accessor;

    public void MethodUsingScopedService()
    {
        var scoped = _accessor.HttpContext.RequestServices.GetRequiredService<Scoped>();
    }
}

2

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