C# FluentValidation命令验证程序未由AutoFac注册
我已经为一个问题挣扎了一段时间了。我正在基于eShopOnContainers GitHub项目构建一个项目。我的项目运行在asp.net core 2.2上,我正在使用 MediatR 6.0C# FluentValidation命令验证程序未由AutoFac注册,c#,asp.net-core,domain-driven-design,autofac,fluentvalidation,C#,Asp.net Core,Domain Driven Design,Autofac,Fluentvalidation,我已经为一个问题挣扎了一段时间了。我正在基于eShopOnContainers GitHub项目构建一个项目。我的项目运行在asp.net core 2.2上,我正在使用 MediatR 6.0 MediatR 6.0 MediatR.Extensions.Microsoft.DependencyInjection 6.0.1 FluentValidation.AspNetCore 8.1.2 Autofac.Extensions.DependencyInjection 4.3.1 我正在使
- MediatR 6.0
- MediatR.Extensions.Microsoft.DependencyInjection 6.0.1
- FluentValidation.AspNetCore 8.1.2
- Autofac.Extensions.DependencyInjection 4.3.1
ValidatorBehavior
类,该类实现了IPipelineBehavior
public class ValidatorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator<TRequest>> _validators;
public ValidatorBehavior(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
var context = new ValidationContext(request);
var failures = _validators
.Select(v => v.Validate(context))
.SelectMany(result => result.Errors)
.Where(error => error != null)
.ToList();
if (failures.Any())
{
throw new PlanningDomainException(
$"Command Validation Errors for type {typeof(TRequest).Name}", new ValidationException("Validation exception", failures));
}
var response = await next();
return response;
}
}
一切都会好起来的!我不明白为什么它不会。也许我没有在autofac中正确加载命令验证程序,但是,我在网上找到的每个示例代码都指向相同的注册方法:
builder.RegisterAssemblyTypes(assembly)
.Where(t => t.IsClosedTypeOf(typeof(IValidator<>)))
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(程序集)
.式中(t=>t.IsClosedTypeOf(typeof(IValidator)))
.a实现接口();
我的git hub帐户上有这个项目的全部源代码,如果你想仔细看看的话
有人能帮我理解我做错了什么吗?这几天我都快发疯了。我的配置和你的相似。我能找到的唯一区别是start.cs文件中的以下几行
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddFluentValidation(fv =>
{
fv.RegisterValidatorsFromAssemblyContaining<MediatorModule>();
fv.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
}
);
}
公共IServiceProvider配置服务(IServiceCollection服务)
{
services.AddMvc()
.AddFluentValidation(fv=>
{
fv.RegisterValidatorsFromAssemblyContaining();
fv.RunDefaultMvcValidationAfterFluentValidationExecutes=false;
}
);
}
此问题是否得到解决?我面临着完全相同的问题,也在使用相同的eshoponcontainer项目。你能告诉我这件事是否解决了吗?嘿,伙计。没有人回答这个问题,所以我所做的是将每个验证单独添加为修补程序。@BillyVlachos,根据您的详细问题,我能够解决我自己的问题,谢谢您!对经过这么长时间,你是第一个给我答案的人!非常感谢你。我丢失了这段代码!我很高兴能帮上忙。
public class ApplicationCreateCommandValidator : AbstractValidator<ApplicationCreateCommand>
{
public ApplicationCreateCommandValidator()
{
RuleFor(cmd => cmd.CategoryType).NotEmpty().Must(BeValidCategoryType).WithMessage("The category type is not valid.");
RuleFor(cmd => cmd.CompetitionId).NotEmpty().WithMessage("The competition id must be specified.");
RuleFor(cmd => cmd.ParticipantId).NotEmpty().WithMessage("The participant id must be specified.");
}
private bool BeValidCategoryType(int categoryType)
{
return categoryType != 0;
}
}
builder.RegisterAssemblyTypes(assembly)
.Where(t => t.IsClosedTypeOf(typeof(IValidator<>)))
.AsImplementedInterfaces();
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddFluentValidation(fv =>
{
fv.RegisterValidatorsFromAssemblyContaining<MediatorModule>();
fv.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
}
);
}