title: CQRS ValidatorHandler not recognizing FluentValidation validators?
body: <p>I'm using Web Api 2, Autofac, and MediatR (CQRS). I have a mediator pipeline in place that has pre/post request handlers. That all works fine. I'm trying to hook up Validation now and decorate the pipeline with it. </p>
  
  <p>Here is my Autofac DI code:</p>
  
  <pre><code>public void Configuration(IAppBuilder app)
  {
      var config = new HttpConfiguration();
      FluentValidationModelValidatorProvider.Configure(config);
      ConfigureDependencyInjection(app, config);
  
      WebApiConfig.Register(config);
      app.UseWebApi(config);
  }
  
  private static void ConfigureDependencyInjection(IAppBuilder app, HttpConfiguration config)
  {
      var builder = new ContainerBuilder();
      builder.RegisterSource(new ContravariantRegistrationSource());
      builder.RegisterAssemblyTypes(typeof(IMediator).Assembly).AsImplementedInterfaces();
  
      builder.Register&lt;SingleInstanceFactory&gt;(ctx =&gt;
      {
          var c = ctx.Resolve&lt;IComponentContext&gt;();
          return t =&gt; c.Resolve(t);
      });
  
      builder.Register&lt;MultiInstanceFactory&gt;(ctx =&gt;
      {
          var c = ctx.Resolve&lt;IComponentContext&gt;();
          return t =&gt; (IEnumerable&lt;object&gt;)c.Resolve(typeof(IEnumerable&lt;&gt;).MakeGenericType(t));
      });
  
      //register all pre handlers
      builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
          .As(type =&gt; type.GetInterfaces()
              .Where(interfacetype =&gt; interfacetype.IsClosedTypeOf(typeof(IAsyncPreRequestHandler&lt;&gt;))))
          .InstancePerLifetimeScope();
  
      //register all post handlers
      builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
          .As(type =&gt; type.GetInterfaces()
              .Where(interfacetype =&gt; interfacetype.IsClosedTypeOf(typeof(IAsyncPostRequestHandler&lt;,&gt;))))
          .InstancePerLifetimeScope();
  
      //register all async handlers
      builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
          .As(type =&gt; type.GetInterfaces()
              .Where(interfaceType =&gt; interfaceType.IsClosedTypeOf(typeof(IAsyncRequestHandler&lt;,&gt;)))
              .Select(interfaceType =&gt; new KeyedService("asyncRequestHandler", interfaceType)))
          .InstancePerLifetimeScope();
  
      //register pipeline decorator
      builder.RegisterGenericDecorator(
          typeof(AsyncMediatorPipeline&lt;,&gt;), 
          typeof(IAsyncRequestHandler&lt;,&gt;), 
          "asyncRequestHandler")
          .Keyed("asyncMediatorPipeline", typeof(IAsyncRequestHandler&lt;,&gt;))
          .InstancePerLifetimeScope();
  
      //register validator decorator
      builder.RegisterGenericDecorator(
          typeof(ValidatorHandler&lt;,&gt;), 
          typeof(IAsyncRequestHandler&lt;,&gt;),
          "asyncMediatorPipeline")
          .InstancePerLifetimeScope();
  
      // Register Web API controller in executing assembly.
      builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).InstancePerRequest();
  
      //register RedStripeDbContext
      builder.RegisterType&lt;RedStripeDbContext&gt;().As&lt;IRedStripeDbContext&gt;().InstancePerRequest();
  
      builder.RegisterType&lt;AutofacServiceLocator&gt;().AsImplementedInterfaces();
      var container = builder.Build();
  
      config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
  
      // This should be the first middleware added to the IAppBuilder.
      app.UseAutofacMiddleware(container);
  
      // Make sure the Autofac lifetime scope is passed to Web API.
      app.UseAutofacWebApi(config);
  }
  </code></pre>
  
  <p>Here is the ValidatorHandler:</p>
  
  <pre><code>public class ValidatorHandler&lt;TRequest, TResponse&gt; : IAsyncRequestHandler&lt;TRequest, TResponse&gt; where TRequest : IAsyncRequest&lt;TResponse&gt;
  {
      private readonly IAsyncRequestHandler&lt;TRequest, TResponse&gt; _inner;
      private readonly IValidator&lt;TRequest&gt;[] _validators;
  
      public ValidatorHandler(
          IAsyncRequestHandler&lt;TRequest, TResponse&gt; inner,
          IValidator&lt;TRequest&gt;[] validators)
      {
          _inner = inner;
          _validators = validators;
      }
  
      public async Task&lt;TResponse&gt; Handle(TRequest request)
      {
          var context = new ValidationContext(request);
  
          var failures = _validators
              .Select(v =&gt; v.Validate(context))
              .SelectMany(result =&gt; result.Errors)
              .Where(f =&gt; f != null)
              .ToList();
  
          if (failures.Any())
              throw new ValidationException(failures);
  
          return await _inner.Handle(request);
      }
  }
  </code></pre>
  
  <p>Here is a sample query:</p>
  
  <pre><code>[Validator(typeof(GetAccountRequestValidationHandler))]
  public class GetAccountRequest : IAsyncRequest&lt;GetAccountResponse&gt;
  {
      public int Id { get; set; }
  }
  </code></pre>
  
  <p>Here is the fluent validation handler:</p>
  
  <pre><code>public class GetAccountRequestValidationHandler : AbstractValidator&lt;GetAccountRequest&gt;
  {
      public GetAccountRequestValidationHandler()
      {
          RuleFor(m =&gt; m.Id).GreaterThan(0).WithMessage("Please specify an id.");
      }
  
      public Task Handle(GetAccountRequest request)
      {
          Debug.WriteLine("GetAccountPreProcessor Handler");
  
          return Task.FromResult(true);
      }
  }
  </code></pre>
  
  <p>Here is the request handler:</p>
  
  <pre><code>public class GetAccountRequestHandler : IAsyncRequestHandler&lt;GetAccountRequest, GetAccountResponse&gt;
  {
      private readonly IRedStripeDbContext _dbContext;
  
      public GetAccountRequestHandler(IRedStripeDbContext redStripeDbContext)
      {
          _dbContext = redStripeDbContext;
      }
  
      public async Task&lt;GetAccountResponse&gt; Handle(GetAccountRequest message)
      {
          return await _dbContext.Accounts.Where(a =&gt; a.AccountId == message.Id)
              .ProjectToSingleOrDefaultAsync&lt;GetAccountResponse&gt;();
      }
  }
  </code></pre>
  
  <p>Finally here is the Web Api 2 HttpGet method:</p>
  
  <pre><code>[Route("{id:int}")]
  [HttpGet]
  public async Task&lt;IHttpActionResult&gt; GetById([FromUri] GetAccountRequest request)
  {
      var model = await _mediator.SendAsync&lt;GetAccountResponse&gt;(request);
  
      return Ok(model);
  }
  </code></pre>
  
  <p>I put breakpoints all over the place and when I hit this endpoint, the first thing I get into is the GetAccountRequestValidationHandler. Then I get into the ValidatorHandler's constructor. The problem is, the IValidator[] validators parameter to the constructor is always null. </p>
  
  <p>I must be missing something with fluent validation and its registration via Autofac? Any help is much appreciated. </p>