title: Autofac instance per lifetime scope only on child scope, not on root
body: <p>I'm using autofac in my application and struggling to get this situation working.</p>
  
  <p>I registered my type as a PerLifetimeScope, but this rule apply to the root container too.</p>
  
  <p>I have an instance that should be shared only for requests done within a child LifetimeScope, but should always returns a new instance for requests done direct to the root container.</p>
  
  <hr>
  
  <p><strong>To better describe:</strong></p>
  
  <p>I have an mobile app that uses autofac to resolve dependencies.</p>
  
  <p>I build one global container for the application. When the user navigates to a new page, I open a new scope from the container for this page.</p>
  
  <p>The page could access an internal sqlite database using EntityFrameworkCore. The instance of the DbContext used to access the database should be the same for all dependencies in the page's scope.</p>
  
  <p>I also have a ConfigurationsService class that I use like a dictionary (key/value) but stored in the database too.
  This class should perform updates in the database outside of any scope from pages, not interacting with other changes made from the page.</p>
  
  <p>When a single configuration is changed, it requests an instance of the DbContext, make the changes, and dispose the instance.</p>
  
  <p>The problem here is that when the ConfigurationService requests the DbContext instance from the container (not scope), the DbContext will now be a shared instance, and the connection will stay alive until the container is disposed.</p>
  
  <p>I could open an scope for this, but it leaves the control of the scope lifetime for every class that fall in the same situation.</p>
  
  <p>UPDATE 1:</p>
  
  <p>The next code represents really well the dependency hierarchy and the situation I described above.</p>
  
  <pre><code>// CLASSES
  public class Context
  {
  }
  
  public class Repository
  {
      public Repository(Context context)
      {
          Context = context;
      }
  
      public Context Context { get; }
  }
  
  public class ViewModel
  {
      public ViewModel(Repository repository1, Repository repository2)
      {
          Repository1 = repository1;
          Repository2 = repository2;
      }
  
      public Repository Repository1 { get; }
      public Repository Repository2 { get; }
  }
  
  // SAMPLE
  var repository1 = container.Resolve&lt;Repository&gt;();
  var repository2 = container.Resolve&lt;Repository&gt;();
  var viewModel = container.Resolve&lt;ViewModel&gt;();
  </code></pre>
  
  <ol>
  <li>The Context reference from repository1 and repository2 variables should be different instances.</li>
  <li>The Context reference from viewModel.Repository1 and viewModel.Repository2 should be the same instance.</li>
  </ol>
  
  <p>ViewModel is one of many classes that could have a dependency hierarchy like that.</p>
  
  <p>The way I could get it to work for my situation was creating a service that manages the instances as singletons only within a scope that is not root.</p>
  
  <pre><code>// CLASSES
  public class ScopeSingletons
  {
      private readonly List&lt;object&gt; _instances = new List&lt;object&gt;();
  
      public T Get&lt;T&gt;(Func&lt;T&gt; factory)
      {
          var instance = _instances.OfType&lt;T&gt;().SingleOrDefault();
  
          if (instance == null)
              _instances.Add(instance = factory());
  
          return instance;
      }
  }
  
  // SAMPLE
  var builder = new ContainerBuilder();
  
  builder.RegisterType&lt;ScopeSingletons&gt;().AsSelf().InstancePerLifetimeScope();
  builder.RegisterType&lt;ViewModel&gt;().AsSelf();
  builder.RegisterType&lt;Repository&gt;().AsSelf();
  builder.Register&lt;Context&gt;(c =&gt;
  {
      if (Equals((c as IInstanceLookup)?.ActivationScope.Tag, "root"))
          return new Context();
  
      return c.Resolve&lt;ScopeSingletons&gt;().Get(() =&gt; new Context());
  });
  
  var container = builder.Build();
  var scope = container.BeginLifetimeScope();
  
  var repository1 = container.Resolve&lt;Repository&gt;();
  var repository2 = container.Resolve&lt;Repository&gt;();
  var viewModel = scope.Resolve&lt;ViewModel&gt;();
  </code></pre>
  
  <p>In this case, everthing will work as I need. Context resolutions will be unique for every request within the root scope and shared within a child scope.</p>