title: How to use Autofac to resolve instance per request dependencies for types in a child lifetime scope created by Nancy
body: <p>
  We have several applications hosted in Windows services that self host a Nancy endpoint in order to expose instrumentation about the operation of the applications.</p>
  
  <p>We use Autofac as our IOC. Several repositories are registered into the root container in a core DLL shared by all applications; this container is then passed to Nancy as its container using a bootstrapper derived from the <code>Nancy.Autofac.Bootstrapper</code>.</p>
  
  <p>What we found was that when a web request is received by Nancy it resolves a request for a repository from the root container and this led to memory being consumed by non-garbage collected <code>IDisposable</code>s as the root container does not go out of scope (it has the lifetime of the windows service).  This led to the services "leaking" memory.</p>
  
  <p>We then switched to a model where we added registrations for the repositories using <code>InstancePerRequest</code> in the overridden <code>ConfigureRequestContainer()</code> method in our Nancy bootstrapper:</p>
  
  <pre class="lang-cs prettyprint-override"><code>protected override void ConfigureRequestContainer(ILifetimeScope container, NancyContext context)
  {
      base.ConfigureRequestContainer(container, context);
      PerRequestContainerBuilder().Update(container.ComponentRegistry);
  }
  
  private static ContainerBuilder PerRequestContainerBuilder()
  {
      var builder = new ContainerBuilder();
  
      // Dependency for repository
      builder.RegisterType&lt;SystemDateTimeProvider&gt;().InstancePerRequest().As&lt;IDateTimeProvider&gt;();
  
      // Repository
      builder.RegisterType&lt;BookmarkRepository&gt;().InstancePerRequest().As&lt;IBookmarkRepository&gt;();
  
      return builder;
  }
  </code></pre>
  
  <p>We also override the <code>CreateRequestContainer()</code> method to create the request container with the tag <code>MatchingScopeLifetimeTags.RequestLifetimeScopeTag</code>.</p>
  
  <pre class="lang-cs prettyprint-override"><code>protected override ILifetimeScope CreateRequestContainer(NancyContext context)
  {
       return ApplicationContainer.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);
  }
  </code></pre>
  
  <p>This appears to have solved the problem of <code>IDisposable</code>s not being disposed - the child request container is disposed at the end of the web request pipeline and objects resolved by it are also disposed and eventually garbage collected.</p>
  
  <p>Our problem is that this seems to be leaking the implementation details of the repositories into the services as we have to not only register the repository in <code>ConfigureRequestContainer()</code> but also any other objects required by the repository, i.e. if we want to change the implementation of a repository we have to "walk the dependency chain" to register required objects in each service using it - this seems wrong.</p>
  
  <p>Is there a way we can get Autofac to resolve supporting objects for the repositories out of the root container but keep the registration information within the scope of the web request container? Or is there a way to automatically copy existing registrations from the root container into the child container when it is created?</p>