无法在没有StructureMap的情况下使用MediatR解决Fluent验证

尝试在具有结构映射的MediatR中向.Net Core 3.1微服务注入Fluent验证。

在Nuget软件包下面添加了:

<PackageReference Include="FluentValidation.AspNetCore" Version="8.6.2" />
<PackageReference Include="MediatR" Version="4.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="4.0.0" />

Startup.cs:

services.AddMvc(options =>
            {
                options.Filters.Add(typeof(HttpGlobalExceptionFilter));
                options.EnableEndpointRouting = false;
            }).AddControllersAsServices()
            .AddNewtonsoftJson()
            .AddViewLocalization(
                       LanguageViewLocationExpanderFormat.Suffix,
                       opts => { opts.ResourcesPath = "Resources"; })
            .AddDataAnnotationsLocalization()
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
            .AddFluentValidation(fv=> fv.RegisterValidatorsFromAssemblyContaining(typeof(Startup)));

注册的IPipeline行为和验证者:

services.AddMediatR();

services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidatorBehaviour<,>));
services.AddTransient<IValidator<ReopenPomIdToWorkingCommand>, ReopenPomIdCommandValidator>();

ValidatorBehaviour.cs:

public class ValidatorBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
    {
        private readonly IValidator<TRequest>[] _validators;
        public ValidatorBehaviour(IValidator<TRequest>[] validators) => _validators = validators;

        public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
        {
            var failures = _validators
                .Select(v => v.Validate(request))
                .SelectMany(result => result.Errors)
                .Where(error => error != null)
                .ToList();

            if (failures.Any())
            {
                var errorFieldsMessages = failures.Select(x => x.ErrorMessage + ", ").ToArray();

                throw new PomDetailsException(
                    $"Command Validation Errors for type {typeof(TRequest).Name}. " +
                    $"Validation failed : {string.Join(string.Empty, errorFieldsMessages)}", new ValidationException("Validation exception", failures));
            }

            var response = await next();
            return response;
        }
    }

ReopenPomIdCommandValidator.cs:

public class ReopenPomIdCommandValidator : AbstractValidator<ReopenPomIdToWorkingCommand>
    {
        private readonly IPomService _pomService;

        public ReopenPomIdCommandValidator(IPomService pomService, IStringLocalizer<SharedResource> localizer)
        {
            _pomService = pomService;
            RuleFor(com => com.PomId).GreaterThan(0).WithMessage(localizer["ValidPomId"]);
            RuleFor(com => com.PomId).MustAsync((x,y) => BeEligibleForReopening(x)).WithMessage(localizer["PomIdIneligibleForReopening"]);

        }

        private async Task<bool> BeEligibleForReopening(long pomId)
        {
            return await _pomService.IsPomIdEligibleForReopening(pomId);
        }
    }

例外:

Unable to resolve service for type 'FluentValidation.IValidator`1[Commands.ReopenPomIdToWorkingCommand][]' while attempting to activate 'PurchaseOrder.API.Application.Behaviours.ValidatorBehaviour`2[Commands.ReopenPomIdToWorkingCommand,System.Boolean]'.

我猜我配置有误,但是我多次更改配置都没有成功。

任何指导将不胜感激