title: Autofac: What is the difference between InstancePerRequest and InstancePerLifetimeScope in an MVC app
body: <p>I am trying to understand lifetime scopes in Autofac IOC and have the following question.</p>
  
  <p>Assume we have class:</p>
  
  <pre><code>public class TestMemLeak
      {
          SomeDisposableContext cn;
          public TestMemLeak(SomeDisposableContext context)
          {
              cn = context;
          }
      } 
  </code></pre>
  
  <p>where the injected dependency SomeDisposableContext implements IDisposable. </p>
  
  <p>In an MVC application I get a memory leak when resolving TestMemLeak object if SomeDisposableContext is registered as InstancePerDependency (default option). The leak is gone if I register it as InstancePerRequest or InstancePerLifetimeScope.</p>
  
  <p>I don't understand why InstancePerLifetimeScope fixes the leak. My understanding is that if we resolve dependency from root then InstancePerLifetimeScope should behave in same way as InstancePerDependency, and the solution to problem would be to pass ILifetimeScope to TestMemLeak class and resolve the dependency using lifetime scope. Why is this assumption wrong and what is the difference between InstancePerRequest and InstancePerLifetimeScope in MVC app scenario (apart from InstancePerRequest looks for specific 'httpRequest' lifetime scope)? When should I use InstancePerRequest? It would be great if someone could explain this especially from memory leaks perspective.</p>