引发异常:ASP Core 3.0中的Microsoft.Extensions.DependencyInjection.dll中的'System.InvalidOperationException'

时间:2019-12-20 07:07:04

标签: c# asp.net asp.net-core

我发送了一个LoginController的请求,但它向我显示了输出中的此错误:

  

抛出异常:Microsoft.Extensions.DependencyInjection.dll中的'System.InvalidOperationException'

,这是输出: OutPutImage

在启动时,我有此代码用于depencyinjection:

services.Injection();

这是该代码:

  public static void Injection(this IServiceCollection services)
    {
        services.AddScoped(typeof(IEFRepository<>), typeof(EFRepository<>));
        services.AddScoped(typeof(IEFDapperRepository<>), typeof(EFDapperRepository<>));
        services.AddScoped<IRoleService, RoleService>();
        services.AddScoped<IUserService, UserService>();
        services.AddScoped<IEmailService, EmailService>();
        services.AddScoped<IUserRoleService, UserRoleService>();
        services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddScoped<ITokenService, TokenService>();
        services.AddScoped<IPermissionService, PermissionService>();
        services.AddScoped<IUseServiceDapper, UseServiceDapper>();
        services.AddScoped<IActivetionAccountService, ActivetionAccountService>();
        services.AddScoped<IPermissionServiceDapper, PermissionServiceDapper>();
    }

这是Login的构造函数:

private readonly IUseServiceDapper repository;

        public LoginCommandHandler(IUseServiceDapper repository)
        {
            this.repository = repository;
        }

这是IUseServiceDapper的构造函数:

  private readonly IConfiguration configuration;
    private readonly string ConnectionString;
    private readonly IResponseCachedService cachedService;

    public UseServiceDapper(IResponseCachedService cachedService, IConfiguration configuration)
    {
        this.cachedService = cachedService;
        this.configuration = configuration;
        ConnectionString = configuration.GetConnectionString("SqlServer");
    }

现在有什么问题?我该如何解决这个问题?

登录控制器:

public class LoginController : BaseController
{
    private readonly ITokenService token;
    private readonly IUserService user;
    private readonly IMediator mediator;

    public LoginController(ITokenService token, IUserService user, IMediator mediator) : base(mediator)
    {
        this.token = token;
        this.user = user;
        this.mediator = mediator;
    }


    [HttpPost]
    public async Task<ReturnResult<RefreshTokenDto>> Login(LoginDto dto)
    {
        var result =await mediator.Send(new LoginCommand { Password = dto.Password, Username = dto.Username });
        if(result.Success)
        {
            var tokenResult = await token.GenerateNewToken(result.Result);
            return Ok(tokenResult);
        }
        return BadRequest(result.ErrorMessage);
    }
}

}

0 个答案:

没有答案