title: Autofac dispose order
body: <p>I started using Autofac for DI in a project of mine and there's one thing about it I cannot get clear from documentation / googling (maybe I'm missing something).</p>
  
  <p>According to <a href="http://docs.autofac.org/en/latest/lifetime/disposal.html" rel="nofollow">documentation</a>, when a container (or a LifetimeScope) is disposed, Autofac automatically disposes all Disposable instances resolved in this container / scope.</p>
  
  <p>The question is: are those instances disposed in some specific order (which is officially guaranteed)?</p>
  
  <p>It seems natural to expect that if some <code>Client</code> instance is injected with a reference to a <code>Service</code> instance, then the <code>Client</code> should be disposed <strong>before</strong> the <code>Service</code>. (Let's assume the dependency graph has no circular references, and such order can be correctly defined).</p>
  
  <p>If this is not true, and the dependency graph nodes may be disposed in arbitrary order, it means that I have to take some additional precautions in component implementation, so that every component behaves correctly when some of its dependencies suddenly turn out dead. This makes life harder.</p>
  
  <p>I made a set of simple tests, and in (almost) all scenarios the order of disposal is indeed the "natural" one.</p>
  
  <p>I also skimmed through the Autofac sources and found out that all auto-disposable instances are stored internally on a stack, and are disposed in the pop() order (i.e. reverse to oder of store), which makes me believe that some specific dispose order is actually enforced.</p>
  
  <p>Can someone comment on this? Thank you.</p>
  
  <p><strong>EDIT</strong> A violation of the "natural" dispose order happens when I'm trying to do property injection using <code>PropertiesAutowired()</code> (which works by hooking <code>OnActivated</code> event). The following code:</p>
  
  <pre><code>class Service : IDisposable
  {
      public Service()
      {
          Console.WriteLine("Service.ctor()");
      }
  
      public void Dispose()
      {
          Console.WriteLine("Service.Dispose()");
      }
  }
  
  class Client : IDisposable
  {
      public Service Service { get; set; }
  
      public Client()
      {
          Console.WriteLine("Client.ctor()");
      }
  
      public void Dispose()
      {
          Console.WriteLine("Client.Dispose()");
      }
  }
  
  
  class Program
  {
      static void Main(string[] args)
      {
          ContainerBuilder builder = new ContainerBuilder();
          builder.RegisterType&lt;Service&gt;();
          builder.RegisterType&lt;Client&gt;().PropertiesAutowired();
          using (var container = builder.Build())
          {
              var clientProp = container.Resolve&lt;Client&gt;();
          }
      }
  }
  </code></pre>
  
  <p>produces the following output:</p>
  
  <pre><code>Client.ctor()
  Service.ctor()
  Service.Dispose()
  Client.Dispose()
  </code></pre>