title: How can i have a IServiceProvider available in ValidationContext parameter of IValidatableObject.Validate method
body: <p>Controller calls <code>IValidatableObject.Validate</code> internally and passes a <code>ValidationContext</code> object as an argument. I want to use <code>validationContext.GetService()</code> method to get a service object and use it.</p>
  
  <p>I can pass this service as a dependency to controller constructor using AutoFac(DI Injection dll). How do i make it availale to the <code>ValidationContext</code> object?</p>
  
  <p>This stackoverflow question might contain the answer but i do not understand it fully : <a href="https://stackoverflow.com/questions/5218333/asp-net-mvc3-set-custom-iserviceprovider-in-validationcontext-so-validators-can">Asp.Net MVC3: Set custom IServiceProvider in ValidationContext so validators can resolve services</a> </p>
  
  <p>Here is the code:</p>
  
  <p>Model : Employee</p>
  
  <pre><code>using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  
  namespace ValidationContextDemo.Models
  {
      public class Employee : IValidatableObject
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public int DepartmentId { get; set; }
  
          public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext)
          {
              var result = new List&lt;ValidationResult&gt;();
              var EmployeeService = (Service.EmployeeService)validationContext.GetService(typeof(Service.EmployeeService));
              if (!EmployeeService.IsValidDepartment(this.DepartmentId))
              {
                  result.Add(new ValidationResult("This DepartmentId does not exists"));
              }
              return result;
          }
      }
  }
  </code></pre>
  
  <p>Repository : EmployeeRepository</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Linq;
  
  namespace ValidationContextDemo.Models
  {
      public class EmployeeRepository
      {
          public EmployeeRepository()
          {
              Employees = new List&lt;Employee&gt;() { 
              new Employee{Id=1, Name="Alpha", DepartmentId=1},
              new Employee{Id=2, Name="Beta", DepartmentId=1},
              new Employee{Id=3, Name="Gamma", DepartmentId=1}
              };
          }
          public List&lt;Employee&gt; Employees { get; set; }
  
          public void AddEmployee(Employee e)
          {
              Employees.Add(e);
          }
  
          public void UpdateEmployee(int id, Employee e)
          {
              Employee emp = Employees.Where(x =&gt; x.Id == id).FirstOrDefault();
              emp.Name = e.Name;
              emp.DepartmentId = e.DepartmentId;
          }
  
          public void DeleteEmployee(Employee e)
          {
              Employees.Remove(e);
          }
      }
  }
  </code></pre>
  
  <p>Validation Source : Enum</p>
  
  <pre><code>namespace ValidationContextDemo.Enums
  {
      public enum Department
      {
          Engineering=1,
          Sales=2,
          Shipping=3,
          HumanResources=4
      }
  }
  </code></pre>
  
  <p>Service : EmployeeService</p>
  
  <pre><code>using System;
  using System.Linq;
  
  namespace ValidationContextDemo.Service
  {
      public class EmployeeService
      {
          public bool IsValidDepartment(int departmentId)
          {
              return Enum.GetValues(typeof(Enums.Department)).Cast&lt;Enums.Department&gt;().Contains((Enums.Department)departmentId);
          }
      }
  }
  </code></pre>
  
  <p>IServiceProvider : EmployeeServiceProvider</p>
  
  <pre><code>using System;
  
  namespace ValidationContextDemo.Service
  {
      public class EmployeeServiceProvider: IServiceProvider
      {
  
          public object GetService(Type serviceType)
          {
              if (serviceType==typeof(EmployeeService))
              {
                  return new EmployeeService();
              }
              return null;
          }
      }
  }
  </code></pre>
  
  <p>Controller : EmployeeController</p>
  
  <pre><code>using System.ComponentModel.DataAnnotations;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  using ValidationContextDemo.Models;
  using ValidationContextDemo.Service;
  
  namespace ValidationContextDemo.Controllers
  {
      public class EmployeeController : ApiController
      {
          EmployeeRepository _repository;
          EmployeeServiceProvider _serviceProvider;
          public EmployeeController()
          {
              _repository = new EmployeeRepository();
              _serviceProvider = new EmployeeServiceProvider();
          }
          public IHttpActionResult Get()
          {
              return Json(_repository.Employees);
          }
  
          public HttpResponseMessage Post(Employee e)
          {
              ValidationContext vContext = new ValidationContext(e, _serviceProvider, null);
              e.Validate(vContext);
              _repository.AddEmployee(e);
              return new HttpResponseMessage(HttpStatusCode.Created);
          }
  
      }
  }
  </code></pre>
  
  <p>Notice the ValidationContext parameter in Validate method of Employee model. Before model binding, when the validation happens, the IServiceProvider part of ValidationContext is null.</p>
  
  <p>So, after model binding, when the code reaches inside my Controller Action "Post", i create another ValidationContext with a _serviceProvider and call Validate again.</p>
  
  <p>My question is , how can i have this _serviceProvider in my ValidationContext before model binding.</p>
  
  <p>Please let me know if this is still not clear.</p>
  
  <p>Note: I created this example for the sake of this question, i am not using Autofac as a DI container in this example.</p>