title: How do I properly register AutoFac in a basic MVC5.1 website?
body: <p>AutoFac has recently been updated for MVC 5.1 but at the time of writing I find that the documentation is lacking (especially for a simple example).</p>
  
  <p>I would like to inject dependencies into MVC Controllers and register my own implementations for e.g. e-mail (actual sending vs print to output window) as a basic example.</p>
  
  <p>I'm do not know if I am missing a good resource for this and I am slight concerned that because it uses the OWIN specification that the implementation may differ for MVC5.1 (ASP.NET Identity uses OWIN and there is some special attributes used to properly instantiate OWIN) so need to check I am getting things right.</p>
  
  <p><strong>I have a working example with the below setup code - is this correct and good practice for a standard MVC5.1 web app?</strong> </p>
  
  <p>Bonus question: do I need to add InstancePerHttpRequest to the RegisterControllers line?
  i.e. builder.RegisterControllers(typeof(MvcApplication).Assembly).InstancePerHttpRequest();</p>
  
  <p>(Note: I see the examples on GitHub from Autofac but cannot find an plain example appropriate to MVC5.1.)</p>
  
  <pre><code>public static void RegisterDependencies()
  {
      // Register MVC-related dependencies
      var builder = new ContainerBuilder();
      builder.RegisterControllers(typeof(MvcApplication).Assembly);
      builder.RegisterModelBinders(typeof(MvcApplication).Assembly);
      builder.RegisterModule&lt;AutofacWebTypesModule&gt;();
  
      // Register e-mail service
      builder.RegisterType&lt;EmailSenderToDebug&gt;().As&lt;IEmailSender&gt;().InstancePerHttpRequest();
  
      builder.RegisterModelBinderProvider();
  
      // Set the MVC dependency resolver to use Autofac
      var container = builder.Build();
      DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  }
  </code></pre>
  
  <p>And my controller:</p>
  
  <pre><code>public class HomeController : Controller
  {
  
      public IEmailSender _email { get; set; }
  
      public  HomeController(IEmailSender es)
      {
          this._email = es;
      }
      //
      // GET: /Home/
      public ActionResult Index()
      {
  
          Email e = new Email();
          e.To(new MailAddress("something@something.com", "mr. something"));
          e.Subject("test");
          e.Message("hello world");
          e.From(new MailAddress("somethingElse@somethingElse.com", "mr. else"));
          this._email.SendEmail(e);
          return View();
      }
  }
  </code></pre>
  
  <p>Thanks!</p>