Search code examples
c#.net-core

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: https://stackoverflow.com/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


Solution

  • 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>();
        }
    }