title: How do I gain access to a repository in a custom ValidationAttribute?
body: <pre><code>public class UniqueNameAttribute : ValidationAttribute
  {
      private const string UniqueNameViolationMessage = "This name is already taken. Please select another.";
  
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
          //return somerepo.IsUniqueName(name)
          //how do I get access to repo here?  Tried DI with autofac but it came out as null
      }
  }
  </code></pre>
  
  <p>I'm using dependency injection to inject repositories into my Web API.  However, it seems I cannot inject into a ValidationAttribute.  It comes out as null if I try to inject.  I'm using Autofac for DI.  Is there another way I can gain access to my repo?</p>
  
  <p>Note that I am only using Web API, not MVC, so I cannot use the MVC DependencyResolver.</p>
  
  <pre><code>builder.RegisterType&lt;MyRepository&gt;().As&lt;IMyRepository&gt;().InstancePerRequest();
  
  var container = builder.Build();
  GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)container); ;
  </code></pre>