title: MissingMethodException [Cannot create an instance of an interface] thrown selectively by ASP.NET MVC controller
body: <p>I'm getting strange (at least for me) behavior in an MVC 4 application.</p>
  
  <p>I'm googling around this whole evening; also I read many SO posts - no joy; I know I can't instantiate an interface directly.</p>
  
  <p>In this particular application I'm using Ninject for DI.
  In the controller <strong>constructor</strong> I'm <em>instantiating an interface</em> and everything is peachy at that point - in fact I believe there is no problem with Ninject or its usage at all. </p>
  
  <p>Also I'm sure this code was working once and no changes were introduced since then, except update of the Ninject library to 3.2.2.0 - can't remember which one was before that; obviously older one...</p>
  
  <p>I have simple CRUD actions in that same controller which consumes another WEB service of mine. 2 out of 4 methods work correctly (LOL) - <code>Index()</code> and <code>Create()</code>. <code>Update()</code> and <code>Delete()</code> however are throwing the above mentioned exception; here it is the stack trace:</p>
  
  <pre><code>[MissingMethodException: Cannot create an instance of an interface.]
      System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean&amp;amp; canBeCached, RuntimeMethodHandleInternal&amp;amp; ctor, Boolean&amp;amp; bNeedSecurityCheck) +0
      System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark&amp;amp; stackMark) +119
      System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark&amp;amp; stackMark) +232
      System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
      System.Activator.CreateInstance(Type type) +11
      System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +183
      System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +563
      System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +416
      .
      .
      .
  </code></pre>
  
  <p>And here is the controller, very simple one, really...</p>
  
  <pre><code>public class NonFederalWorkingDayController : BaseController
  {
      private readonly INonFederalWorkingDayManagementService _nonFederalWorkingDayService = null;
  
      public NonFederalWorkingDayController(INonFederalWorkingDayManagementService nonFederalWorkingDayService)
      {
          _nonFederalWorkingDayService = nonFederalWorkingDayService;
      }
  
      private JsonResultModel&lt;IEnumerable&lt;NonFederalWorkingDayDto&gt;&gt; InvokeService(Func&lt;IEnumerable&lt;NonFederalWorkingDayDto&gt;, I9UserDto, IEnumerable&lt;NonFederalWorkingDayDto&gt;&gt; operation, IEnumerable&lt;NonFederalWorkingDayDto&gt; models)
      {
          var result = new JsonResultModel&lt;IEnumerable&lt;NonFederalWorkingDayDto&gt;&gt;();
  
          if (ModelState.IsValid)
          {
              var modifier = new I9UserDto
              {
                  Id = SiteContext.User.UserId
              };
  
              try
              {
                  result.Model = operation(models, modifier);
                  result.Message = "Operation completed successfully.";
              }
              catch (Exception ex)
              {
                  result.HasError = true;
  #if DEBUG
                  result.Message = string.Concat("Operation failed: ", ex.Message);
  #else
                  result.Message = "Operation failed: Internal server error.";
  #endif
              }
  
              if (result.Model != null)
              {
                  foreach (var dto in result.Model)
                  {
                      dto.CreatedOn = null;
                      dto.Creator = null;
                      dto.ModifiedOn = null;
                      dto.Modifier = null;
                  }
              }
          }
          else
          {
              result.HasError = true;
              result.Message = "Invalid data.";
          }
  
          return result;
      }
  
      public ActionResult Index()
      {
          var model = _nonFederalWorkingDayService.Load();
  
          return View(model);
      }
  
      [HttpPost]
      public ActionResult Create(IEnumerable&lt;NonFederalWorkingDayDto&gt; models)
      {
          var result = InvokeService(_nonFederalWorkingDayService.Create, models);
  
          return Json(result);
      }
  
      [HttpPost]
      public ActionResult Update(IEnumerable&lt;NonFederalWorkingDayDto&gt; models)
      {
          var result = InvokeService(_nonFederalWorkingDayService.Update, models);
  
          return Json(result);
      }
  
      [HttpPost]
      public ActionResult Delete(IEnumerable&lt;NonFederalWorkingDayDto&gt; models)
      {
          var result = InvokeService(_nonFederalWorkingDayService.Delete, models);
  
          if (!result.HasError)
          {
              var plurality = models.Count() == 1 ? "Entity" : "Entities";
              result.Message = string.Concat(plurality, " deleted successfully.");
          }
  
          return Json(result);
      }
  }
  </code></pre>
  
  <p>As I mentioned, the problem lies within the last two methods: <code>Update()</code> and <code>Delete()</code>. In fact, the execution never enters any one of them - the error gets raising during the model binding. But note how there is no interfaces except <code>IEnumerable&lt;&gt;</code> which binds just fine in other methods in the same application, including the <code>Create()</code> method in this very controller. I'm not sure how exactly, but the <code>DefaultBinder</code> seems smart enough to figure out that <code>IEnumerable</code> is actually a <code>List</code>. So this shouldn't be it, is it? </p>
  
  <p>Also, I need to mention that I'm calling these actions through AJAX, not normal requests. Does that matters?</p>
  
  <p>I seem to can't figure what is wrong ... please help!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've inherit <code>DefaultModelBinder</code> in order to hook into the model binding pipeline. So I hooked into <code>BindModel()</code> and the <code>controllerContext.Controller</code> is actually fully functional instance of the <code>NonFederalWorkingDayController</code></p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Here is the code listing for <code>NonFederalWorkingDayDto</code> :</p>
  
  <pre><code>public class NonFederalWorkingDayDto : DtoBase, INonFederalWorkingDay
  {
      public static NonFederalWorkingDayDto Consume(INonFederalWorkingDay Donor)
      {
          NonFederalWorkingDayDto result = null;
  
          if (Donor != null)
          {
              result = DtoBase.Consume&lt;NonFederalWorkingDayDto&gt;(Donor);
  
              result.Description = Donor.Description;
              result.FallsOn = Donor.FallsOn;
              result.Id = Donor.Id;
              result.Name = Donor.Name;
          }
  
          return result;
      }
  
      public string Description { get; set; }
  
      public DateTime? FallsOn { get; set; }
  
      public string Name { get; set; }
  
      #region INonFederalWorkingDay support
  
      DateTime INonFederalWorkingDay.FallsOn
      {
          get { return FallsOn ?? default(DateTime); }
      }
  
      int IIdentifier&lt;int&gt;.Id
      {
          get { return Id ?? default(int); }
      }
  
      #endregion
  }
  </code></pre>