AutoFac未注册FluentValidation命令验证程序

时间:2019-01-20 01:58:31

标签: c# asp.net-core domain-driven-design autofac fluentvalidation

我一直在努力解决一个问题。我正在基于eShopOnContainers GitHub项目See Here构建一个项目。我的项目在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命令,并且通过许多文章以及在线eShopOnContainers示例,我实现了一个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;
    }
}

我还包括一个MediatorModule,与示例项目中实现的一样。

public class MediatorModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterAssemblyTypes(typeof(IMediator).GetType().Assembly)
            .AsImplementedInterfaces();

        // Get the assembly name
        var assembly = typeof(Startup).GetType().Assembly;

        // Register all the Command classes (they implement IRequestHandler) in assembly holding the Commands
        builder.RegisterAssemblyTypes(assembly)
            .AsClosedTypesOf(typeof(IRequestHandler<,>));

        // Register the DomainEventHandler classes (they implement INotificationHandler<>) 
        // in assembly holding the Domain Events
        builder.RegisterAssemblyTypes(assembly)
            .AsClosedTypesOf(typeof(INotificationHandler<>));

        // Register the Command's Validators (Validators based on FluentValidation library)
        builder.RegisterAssemblyTypes(assembly)
            .Where(t => t.IsClosedTypeOf(typeof(IValidator<>)))
            .AsImplementedInterfaces();

        builder.Register<ServiceFactory>(context =>
        {
            var componentContext = context.Resolve<IComponentContext>();
            return t => { object o; return componentContext.TryResolve(t, out o) ? o : null; };
        });

        builder.RegisterGeneric(typeof(LoggingBehavior<,>)).As(typeof(IPipelineBehavior<,>));
        builder.RegisterGeneric(typeof(ValidatorBehavior<,>)).As(typeof(IPipelineBehavior<,>));
        builder.RegisterGeneric(typeof(TransactionBehaviour<,>)).As(typeof(IPipelineBehavior<,>));

    }
}

我的测试控制器是:

[Route("api/[controller]")]
[ApiController]
public class ApplicationsController : ControllerBase
{
    private readonly IMediator _mediator;

    public ApplicationsController(IMediator mediator)
    {
        _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
    }

    [HttpPost]
    [ProducesResponseType((int)HttpStatusCode.OK)]
    [ProducesResponseType((int)HttpStatusCode.BadRequest)]
    public async Task<IActionResult> Put([FromBody]ApplicationCreateCommand command, [FromHeader(Name = "x-requestid")] string requestId)
    {
        var c = await _mediator.Send(command);
        return c ? Ok() : (IActionResult)BadRequest();
    }
}

我遇到以下问题:

  1. 每当我尝试调用此API时,都会出现以下错误:

    无法解析构造函数'Void .ctor(MediatR.IMediator)'的参数'MediatR.IMediator中介器。”

我正在通过使用.AddMediatR()将中介程序作为服务添加来解决此问题,即使在示例项目中也从未那样添加。

  1. 添加中介后,API会正确运行,并且正在发送命令,并且处理的命令正在正确处理它。同时,ValidatorBehavior 被正确调用,但CommandValidator不存在。 _validators列表实际上是空的,因此没有进行验证。

我还在命令验证器中设置了断点,但是没有命中任何点。

这是我的命令验证器:

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;
    }
}

一切正常!我不明白为什么不会。也许我没有在autofac中正确加载命令验证器,但是,我在网上找到的每个示例代码都指向相同的注册方法:

builder.RegisterAssemblyTypes(assembly)
            .Where(t => t.IsClosedTypeOf(typeof(IValidator<>)))
            .AsImplementedInterfaces();

如果您想仔细看看,我在git hub帐户中拥有该项目的全部源代码。 This is the API

有人可以帮助我了解我在做什么错吗?这几天让我发疯。

1 个答案:

答案 0 :(得分:1)

我的配置与您相似。我唯一能找到的区别是在start.cs文件中的行下方

public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
                .AddFluentValidation(fv =>
                {
                    fv.RegisterValidatorsFromAssemblyContaining<MediatorModule>();
                    fv.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
                }
            );
}