I have read various articles on the problem and looks like a lot of us have faced this issue. I understood the concept that because the ChildService is scoped, but the FatherService is a singleton, it’s not going to allow us to run.
But the fact is I am adding both my services IAuthRepository and AuthUserContext as Scoped, hence I am not sure why it is complaining about this.
The code in my startup class is
var userDbConnectionString = Configuration["connectionStrings:userDBConnectionString"];
services.AddDbContext<AuthUserContext>(o => o.UseSqlServer(userDbConnectionString));
services.AddScoped<IAuthUserRepository, AuthUserRepository>();
In the above code, you can see My DbContext i.e. AuthUserContext is added with Lifetime as Scoped. Similarly, IAuthRepository is added as scoped as well.
Can someone please help in why it is complaining of below runtime error? even though I have not done like consuming Scoped service from Singleton.
An unhandled exception occurred while processing the request. InvalidOperationException: Cannot consume scoped service 'IDP.Entities.AuthUserContext' from singleton 'IDP.Services.IAuthUserRepository'
My Repository class (For simplicity I have removed rest of the code)
public class AuthUserRepository : IAuthUserRepository
{
AuthUserContext _context;
public AuthUserRepository()
{
}
public AuthUserRepository(AuthUserContext context)
{
_context = context;
}
}
My AuthUserContext Class
public class AuthUserContext : DbContext
{
public AuthUserContext(DbContextOptions<AuthUserContext> options)
: base(options)
{
}
public DbSet<User> Users { get; set; }
public DbSet<UserClaim> Claims { get; set; }
public DbSet<OtpDetails> OtpDetails { get; set; }
}
I am using Microsoft.AspNetCore.App (2.1.1) and IdentityServer4(2.3.2) Nugets
Any help will be appriciated.
This is my configure services method
public void ConfigureServices(IServiceCollection services)
{
var userDbConnectionString = Configuration["connectionStrings:userDBConnectionString"];
services.AddDbContext<AuthUserContext>(o => o.UseSqlServer(userDbConnectionString));
services.AddScoped<IAuthUserRepository, AuthUserRepository>();
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.AddMvc();
services.AddSingleton<IConfiguration>(Configuration);
var identityServerDataDBConnectionString = Configuration["connectionStrings:identityServerDataDBConnectionString"];
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddUserStore()
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder => builder.UseSqlServer(identityServerDataDBConnectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder => builder.UseSqlServer(identityServerDataDBConnectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
});
}
The exact exception I am getting is as below:
An unhandled exception occurred while processing the request. InvalidOperationException: Cannot consume scoped service 'CCIDP.Entities.AuthUserContext' from singleton 'CCIDP.Services.IAuthUserRepository'