title: How do I use custom model binder that supports dependency injection in ASP.NET Core?
body: <p>I am trying to use a custom model binder in MVC that I want resolved from my IoC container.  The issue I am having is that I can't access my container while I am adding the MVC service, because my container isn't built yet (and I need to add MVC before building my container).  Feels like a chicken/egg issue, and I am sure I am missing a simple solution.</p>
  
  <p>Example:</p>
  
  <pre><code>services.AddMvc().AddMvcOptions(options =&gt;
  {
       options.ModelBinders.Add(serviceProvider.Resolve&lt;CustomModelBinder&gt;());
  });
  </code></pre>
  
  <p>My custom model binder looks like this:</p>
  
  <pre><code>public class CustomModelBinder : IModelBinder
  {
      private IServiceProvider serviceProvider;
  
      public CustomModelBinder(IServiceProvider serviceProvider)
      {
          this.serviceProvider = serviceProvider;
      }
  
      public Task&lt;ModelBindingResult&gt; BindModelAsync(ModelBindingContext bindingContext)
      {
          var model = serviceProvider.GetService(bindingContext.ModelType);
          bindingContext.Model = model;
  
          var binder = new GenericModelBinder();
          return binder.BindModelAsync(bindingContext);
      }
  }
  </code></pre>