84 行目と 85 行目 (行を使用して積み重ねられた 2 つ) で次のメッセージが表示されます。
CA2000 : Microsoft.Reliability : メソッド 'RavenDataAccess.GetRavenDatabase()' で、オブジェクト '<> g_initLocal9' がすべての例外パスで破棄されません。オブジェクト '<>g _initLocal9' で System.IDisposable.Dispose を呼び出してから、オブジェクトへのすべての参照が範囲外になります。
DocumentStore は IDisposable を実装します。
なんで?DocumentStore オブジェクトを破棄するには、他にどのような方法がありますか? これらは using ブロックで作成され、catch ブロックで破棄します。これはどのように修正する必要がありますか?
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 => docStore.Dispose());
throw;
}
}