NorthwindTraders icon indicating copy to clipboard operation
NorthwindTraders copied to clipboard

IHttpContextAccessor issue

Open shareonline opened this issue 4 years ago • 0 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.

shareonline avatar May 10 '20 14:05 shareonline