0
votes

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'

1
Can you post your complete method with all service registrations? - Isma
Also, post the exact exception you're receiving. - Chris Pratt
@Isma I have edit my original post and updated the code you need. - user1955255
@ChrisPratt Edited my original post and added the information you asked for - user1955255

1 Answers

0
votes

You have two constructors on AuthUserRepositories

public class AuthUserRepository : IAuthUserRepository
    {
        AuthUserContext _context;

        public AuthUserRepository()
        {

        }

        public AuthUserRepository(AuthUserContext context)
        {
            _context = context;
        }
}

And that is problematic and a bad practice. If the first constructor is used, then _context would be null and therefore the instance would be at an invalid state. If your AuthUserRepository has a dependency on AuthUserContext, remove the default constructor never to allow _context to be null.

The dependency injection container thinks that IAuthUserRepository is singleton although in your example you show it as scoped. Not sure if that's all your code, but search for all the references to IAuthUserRepository in your code and make sure it's not being registered anywhere else as singleton.

Also where are you injecting the IAuthUserRepository? It's not shown in your question. Is that service where you are injecting it a Singleton one? If it is you'll have problems because the IAuthUserRepository will not really be scoped