jasontaylordev / NorthwindTraders

Northwind Traders is a sample application built using ASP.NET Core and Entity Framework Core.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IHttpContextAccessor issue

shareonline opened this issue · comments

I have used this sample as a reference. I really like the de-coupling through the project.
I have run into an issue i can't seem to fix.

I'm trying to use Azure B2C authentication, together with a claimstransformer which works nicely.
However the IHttpContextAccessor is always null unless i inject it in my web project directly.
Whenever i use IHttpContextAccesor or CurrentUserService which depends on it, in a class library (application/Infrastructure/persistence layer), it is null.

My startup.cs in web:
services.AddInfrastructure(Configuration, Environment);
services.AddPersistence(Configuration);
services.AddApplication();

        services.AddScoped<ICurrentUserService, CurrentUserService>();
        services.AddHttpContextAccessor();

appdbcontext:
private readonly ICurrentUserService currentUserService;
private readonly IDateTime dateTime;

    public AZManagerDbContext(DbContextOptions<AZManagerDbContext> options)
        : base(options)
    {
    }

    public AZManagerDbContext(
        DbContextOptions<AZManagerDbContext> options,
        ICurrentUserService currentUserService, IDateTime dateTime)
        : base(options)
    {
        this.currentUserService = currentUserService ?? throw new ArgumentNullException(nameof(currentUserService));
        this.dateTime = dateTime ?? throw new ArgumentNullException(nameof(dateTime));
    }

the IDateTime interface is working fine in the dbcontext, so injection works.

Do you have any suggestions to what i'm facing?
The only thing different in my sample is the identity platform used.

Thank you for your interest in this project. This repository has been archived and is no longer actively maintained or supported. We appreciate your understanding. Feel free to explore the codebase and adapt it to your own needs if it serves as a useful reference. If you have any further questions or concerns, please refer to the README for more information.