0

In Starup file

services.AddScoped<IUserResponsitory , UserResponsitory>();
services.AddSingleton<IAuthService>(service=>new AuthServiceImpl(
            service.GetService<IUserResponsitory>(),service.GetService<IConfiguration>()));

In AuthServiceImpl file:

    private IUserResponsitory m_userResponsitory;
    private IConfiguration m_config;
    public  AuthServiceImpl(IUserResponsitory userResponsitory, IConfiguration config) 
    {
        m_config = config;
        m_userResponsitory = userResponsitory;
    }

In the UserResponsitory file.

public class UserResponsitory : Responsitory<Users>,IUserResponsitory
    {
        private DbSet<Users> userEntity;

        public UserResponsitory(MyDBContext context) : base(context)
        {
            userEntity = context.Set<Users>();
        }
    }

The error below enter image description here

Some of the help: help1

help2

Can you help me.Please!

vankhanhpr
  • 203
  • 2
  • 9

2 Answers2

0

Since scope validation is applied by .NET Core, you can not resolve services with scoped lifetime (IUserResponsitory in your case) in the constructor of a service registered with singleton lifetime (IAuthService).

So you need to change your IAuthService lifetime to scoped.

services.AddScoped<IAuthService, AuthService>();

Useful Resources:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2#scope-validation

https://bartwullems.blogspot.com/2019/02/aspnet-core-scope-validation.html

Mohsin Mehmood
  • 4,156
  • 2
  • 12
  • 18
  • Thanks for your answer, so the Scoped lifetime services are just created once per request.I want to create the Singleton for my service where save my token when using login. If I used the Scoped for that service , I wouldn't found the token created before. – vankhanhpr Jun 30 '19 at 08:44
  • So making your IAuth service as singleton means that data will be shared across among users logging in which you will never desire. – Mohsin Mehmood Jun 30 '19 at 13:40
0

As the error indicates, you could not resolve a scoped service from root provider.

If you prefer use IAuthService as Singleton, you could reolve the service from IServiceProvider like

  1. Startup.cs

    services.AddScoped<IUserResponsitory , UserResponsitory>();
    services.AddSingleton<IAuthService, AuthServiceImpl>();
    
  2. AuthServiceImpl

    public class AuthService : IAuthService
    {
        private readonly IServiceProvider _serviceProvider;
        private readonly IConfiguration m_config;
        public AuthService(IServiceProvider serviceProvider
            , IConfiguration configuration)
        {
            _serviceProvider = serviceProvider;
            m_config = configuration;
        }
    
        public void Auth()
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var db = scope.ServiceProvider.GetRequiredService<IUserResponsitory>();
                var result = db.Users.ToList();
            }
        }
    }
    
Edward
  • 28,296
  • 11
  • 76
  • 121