我在基础设施层从IJwtService实现JwtService IJwtService在应用程序层声明 并
我在基础设施层从IIdentityService实现IdentityService
我在基础设施依赖注入时都注册了,比如
services.AddTransient< IJwtService,JwtService>();
services.AddTransient<IIdentityService,IdentityService>();
然后我在 LoginQueryHandler() 中实现 LoginQueryHandler 实现: IRequestHandler<LoginViewModel, LoginDto> 我注入 IIdentityService 和 IJwtService
我在应用层注册了调解器
services.AddMediatR(Assembly.GetExecutingAssembly());
使用中介我向 LoginQuery Handler 发送请求
public async Task Login([FromBody] LoginViewModel model)
{
return await Mediator.Send(model);
}
这是 LoginQueryHandler 类
public class LoginViewModel: IRequest<LoginDto>
{
public string Email { get; set; }
public string Password { get; set; }
}
public class LoginQueryHandler : IRequestHandler<LoginViewModel, LoginDto>
{
private readonly IIdentityService _identityService;
private readonly IJwtService _jwtService;
public LoginQueryHandler(IIdentityService identityService,IJwtService jwtService)
{
_identityService=identityService;
_jwtService=jwtService;
}
public async Task<LoginDto> Handle(LoginViewModel request, CancellationToken cancellationToken)
{
try
{
var user = await _identityService.FindByEmailAsync(request.Email);
// codes....
return new LoginDto();
}
catch (Exception ex)
{
throw ex;
}
}
}
但它会引发以下错误
System.InvalidOperationException:验证服务描述符时出错'ServiceType:MediatR.IRequestHandler`2 [Application.Login.Queries.LoginViewModel,Application.Login.Queries.LoginDto] Lifetime:Transient ImplementationType:Application.Login.Queries.LoginQueryHandler':无法在尝试激活“Newproject.Infrastructure.Identity.IdentityService”时解析“TechneTravel.Infrastructure.Services.JwtService”类型的服务。---> System.InvalidOperationException:尝试激活“Newproject.Infrastructure.Identity.IdentityService”时无法解析“Newproject.Infrastructure.Services.JwtService”类型的服务
然后我尝试了三种在Application层注册Request Handler的方法,如下所示
services.AddTransient(typeof(IRequestHandler<LoginViewModel, LoginDto>), typeof(LoginQueryHandler));
services.AddTransient<IRequestHandler<LoginViewModel, LoginDto>, LoginQueryHandler>();
services.AddTransient(typeof(LoginQueryHandler));
但没有解决