title: NancyFx - How to Dispose instances registered as singleton in application container?
body: <p>My specific context is a self hosted Nancy application, where I'm using RavenDb embedded document database.</p>
  
  <p>My bootstrap looks like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MyBootstrapper : DefaultNancyBootstrapper
  {
      protected override void ConfigureApplicationContainer(TinyIoCContainer container)
      {
          base.ConfigureApplicationContainer(container);
  
          container.Register&lt;IDocumentStore&gt;(CreateDefaultStore());
          container.Register&lt;IDoSomething, MyOtherType&gt;().AsSingleton(); // implements IDisposable
      }
  
      private static IDocumentStore CreateDefaultStore(){...}
  }
  </code></pre>
  
  <p>The problem is that I was expecting the Dispose method of the IDocumentStore and of my custom  disposable type to be called when the NanyHost is stopped.</p>
  
  <p>The NancyHost implements IDisposable, the TinyIoCContainer implements IDisposable, if I register a type that implements IDisposable into the request container it will be disposed. I was almost sure that when the Host is disposed the container will be disposed and that all registered instances will be disposed. </p>
  
  <p>I know I can store a reference to the document store outside the bootstrapper and dispose it after the host is stopped and I can also think of some other tricks like having the bootstraper implement IDisposable and dispose it <strong>manually</strong> after the host stops, but I was hopping that there is a more automatic way of handling the disposal of all the instances registered. </p>
  
  <p>Am I missing some clever way to call Dispose() on the singleton instances that are registered in the application container?</p>
  
  <p>My guess is that since the host &amp; application container are expected to live until the application ends, disposing the container was not a priority, but still, this seems like a bad practice if that is the case. </p>
  
  <p><strong>UPDATE</strong>:
  After posting, i'I've found <a href="https://groups.google.com/forum/#!searchin/nancy-web-framework/dispose/nancy-web-framework/s87mfvdEmhw/PWx8fXNYRCIJ" rel="nofollow">this thread on the nancy group</a> which has some information related to this, but is a bit old. </p>