title: Proper Way to Dispose: object not disposed along all exception paths
body: <p>I get this message for line 84 and line 85 (the two, stacked <em>using</em> lines):</p>
  
  <blockquote>
    <p>CA2000 : Microsoft.Reliability : In method 'RavenDataAccess.GetRavenDatabase()', object '&lt;>g_<em>initLocal9' is not disposed along all exception paths. Call System.IDisposable.Dispose on object '&lt;>g</em>_initLocal9' before all references to it are out of scope.</p>
  </blockquote>
  
  <p>DocumentStore implements IDisposable.</p>
  
  <p>Why? How else can I dispose the DocumentStore objects? They're created in a using block, and I dispose of them in my catch block. How should this be fixed?</p>
  
  <pre><code>private static IDocumentStore GetRavenDatabase()
  {
      Shards shards = new Shards();
  
      try
      {
          using (DocumentStore docStore1 = new DocumentStore { Url = ConfigurationManager.AppSettings["RavenShard1"] })  // Line 84
          using (DocumentStore docStore2 = new DocumentStore { Url = ConfigurationManager.AppSettings["RavenShard2"] })  // Line 85
          {
              shards.Add(docStore1);
              shards.Add(docStore2);
          }
  
          using (ShardedDocumentStore documentStore = new ShardedDocumentStore(new ShardStrategy(), shards))
          {
              documentStore.Initialize();
  
              IndexCreation.CreateIndexes(typeof(RavenDataAccess).Assembly, documentStore);
  
              return documentStore;
          }
      }
      catch
      {
          shards.ForEach(docStore =&gt; docStore.Dispose());
  
          throw;
      }
  }
  </code></pre>