title: Microsoft Dependency Injection & HttpClientFactory w/ .NET Framework 4.6+ / MVC 5.2 / Web API 2
body: <p>Our web application is built on the .NET Framework 4.6+. We're using WebForms, MVC 5.2, and Web API 2.</p>
  
  <p>I'm in the process of trying to integrate Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.Http into this solution so we can take advantage of the new HttpClientFactory that's in ASP.NET Core. We also want to start using DI in our MVC and API controllers.</p>
  
  <p>It appears there are two ways this can be achieved:</p>
  
  <ul>
  <li>Write a custom ControllerActivator</li>
  <li>Write a custom DependencyResolver</li>
  </ul>
  
  <p>Based on the reading I've done, it appears the ControllerActivator method is the old way of doing this, and the DependencyResolver is the current, preferred way of handling this. I've written code for both, and both methods appear to work.</p>
  
  <p>Considering the DependencyResolver appears to be the preferred solution for DI now, I'd like to use it but I'm not sure if I'm handling scope object disposal correctly.</p>
  
  <p>Here's how I'm configuring the DependencyResolvers in Global.asax:</p>
  
  <pre><code>Web.Mvc.DependencyResolver.SetResolver(New Mvc.DependencyInjection.DependencyResolver(serviceProvider))
  GlobalConfiguration.Configuration.DependencyResolver = New Api.DependencyInjection.DependencyResolver(serviceProvider)
  </code></pre>
  
  <p>My System.Web.Http.Dependencies.IDependencyResolver implementation for Web API is:</p>
  
  <pre><code>public class DependencyResolver : IDependencyResolver
  {
      private IServiceProvider ServiceProvider { get; }
  
      public DependencyResolver(IServiceProvider serviceProvider) =&gt; ServiceProvider = serviceProvider;
  
      public IDependencyScope BeginScope() =&gt; new DependencyResolver(ServiceProvider.CreateScope().ServiceProvider);
  
      public void Dispose() =&gt; (ServiceProvider as IDisposable)?.Dispose();
  
      public object GetService(Type serviceType) =&gt; ServiceProvider.GetService(serviceType);
  
      public IEnumerable&lt;object&gt; GetServices(Type serviceType) =&gt; ServiceProvider.GetServices(serviceType);
  }
  </code></pre>
  
  <p>My System.Web.Mvc.IDependencyResolver implementation for MVC is:</p>
  
  <pre><code>public class DependencyResolver : IDependencyResolver
  {
      private IServiceProvider ServiceProvider { get; }
  
      public DependencyResolver(IServiceProvider serviceProvider) =&gt; ServiceProvider = serviceProvider;
  
      public object GetService(Type serviceType) =&gt; ServiceProvider.GetService(serviceType);
  
      public IEnumerable&lt;object&gt; GetServices(Type serviceType) =&gt; ServiceProvider.GetServices(serviceType);
  }
  </code></pre>
  
  <p>The <code>System.Web.Http.Dependencies.IDependencyResolver</code> interface has <code>Dispose()</code>, and it appears API requests do call my implementation's Dispose method. So that appears to be working (I think).</p>
  
  <p>My concern is the <code>System.Web.Mvc.IDependencyResolver</code> interface doesn't have <code>Dispose()</code>, I'm not clear if these service objects are being properly disposed after a MVC request.</p>
  
  <p>If anyone can provide some insight into this I'd really appreciate it.  Don't want to roll this out and find out we're leaking memory.</p>