i want to inject the AuthenticationStateProvider into the DatabaseContext. My Code looks like that:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
services.AddDbContextFactory<DatabaseContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("MyCon"));
options.EnableSensitiveDataLogging();
});
services.AddScoped<DatabaseContext>(p =>
p.GetRequiredService<IDbContextFactory<DatabaseContext>>()
.CreateDbContext());
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<DatabaseContext>()
.AddDefaultTokenProviders()
.AddErrorDescriber<MultilanguageIdentityErrorDescriber>();
}
public DatabaseContext : IdentityDbContext
{
private readonly AuthenticationStateProvider _authenticationStateProvider;
public DatabaseContext(DbContextOptions options, AuthenticationStateProvider authenticationStateProvider)
{
_authenticationStateProvider = authenticationStateProvider;
}
}
As soon as i start the App I run into the following error:
InvalidOperationException: Cannot resolve scoped service 'Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider' from root provider. in Startup.cs
{ options.UseSqlServer(Configuration.GetConnectionString("MyCon"));
options.EnableSensitiveDataLogging();
});
services.AddScoped<DatabaseContext>(p =>
p.GetRequiredService<IDbContextFactory<DatabaseContext>>()
.CreateDbContext());
What i am doing wrong?
Thanks for your help!