title: Autofac/FluentValidation: No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested
body: <p>Attempting to inject data into a FluentValidation validator:</p>
  
  <pre><code>public class MyFormValidator : AbstractValidator&lt;MyForm&gt;
  {
      private readonly IQueryable&lt;Models.User&gt; _users;
  
      public MyFormValidator(IQueryable&lt;Models.User&gt; users)
      {
          _users = users;
          ...
      }
  }
  </code></pre>
  
  <p>My validator factory:</p>
  
  <pre><code>public class DependencyResolverValidatorFactory : ValidatorFactoryBase
  {
      private readonly IContainer container;
  
      public DependencyResolverValidatorFactory(IContainer container)
      {
          this.container = container;
      }
  
      public override IValidator CreateInstance(Type validatorType)
      {
          return container.ResolveOptionalKeyed&lt;IValidator&gt;(validatorType);
      }
  }
  </code></pre>
  
  <p>My Autofac configurator:</p>
  
  <pre><code>public class AutofacConfigurator
  {
      public static void Configure()
      {
          var builder = new ContainerBuilder();
          ...
  
          builder.RegisterType&lt;MyFormValidator&gt;()
              .Keyed&lt;IValidator&gt;(typeof(IValidator&lt;MyForm&gt;))
              .As&lt;IValidator&gt;()
               // 2nd parameter returns IQueryable&lt;User&gt;
              .WithParameter("users", new SqlRepository&lt;User&gt;(dataContext)) 
              .InstancePerRequest();
  
          builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
  
          var container = builder.Build();
  
          DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  
          // Register the validator factory with FluentValidation, and register 
          // FluentValidation as the model validator provider for the MVC framework. 
          // see http://www.jerriepelser.com/blog/using-fluent-validation-with-asp-net-mvc-part-3-adding-dependency-injection
          var fluentValidationModelValidatorProvider = 
              new FluentValidationModelValidatorProvider(
                  new DependencyResolverValidatorFactory(container));
          DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
          fluentValidationModelValidatorProvider.AddImplicitRequiredValidator = false;
          ModelValidatorProviders.Providers.Add(fluentValidationModelValidatorProvider);
  
      }
  }
  </code></pre>
  
  <p>Getting the following exception:</p>
  
  <blockquote>
    <p>No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.</p>
  </blockquote>
  
  <p>I have other validators, most of which will not need data injected into them. </p>
  
  <p>This is largely new ground for me (in both Autofac and FluentValidation) and am still trying to understand what I am doing here. I suspect I'm simply registering my type incorrectly. How do I fix this and properly register my type? </p>
  
  <p>(My apologies if this is too similar to other questions that were already asked.)</p>