MVC アプリケーションでは、リクエストに対してドキュメント セッションを作成し、一連のオブジェクトを取得してメモリ内で処理します。この間にエラーが発生した場合は、Error オブジェクトを作成して Raven に格納します。この Error オブジェクトを保存するために SaveChanges を呼び出すと、メモリ内の他のすべてのオブジェクトの状態も保存されます。これを避ける必要があります。Error オブジェクトに対してのみ Savechanges を実行するにはどうすればよいですか? StructureMap を使用して DocumentSession のインスタンスを取得します。
public RavenDbRegistry(string connectionStringName)
{
For<IDocumentStore>()
.Singleton()
.Use(x =>
{
var documentStore = new DocumentStore { ConnectionStringName = connectionStringName };
documentStore.Initialize();
return documentStore;
}
)
.Named("RavenDB Document Store.");
For<IDocumentSession>()
.HttpContextScoped()
.Use(x =>
{
var documentStore = x.GetInstance<IDocumentStore>();
return documentStore.OpenSession();
})
.Named("RavenDb Session -> per Http Request.");
}
これは、エラー オブジェクトを保存する方法です。
private void SaveError(Error error)
{
documentSession.Store(error);
documentSession.SaveChanges();
}
私が試したいくつかのバリエーションは、期待どおりに機能しませんでした: 1.エラーログのためだけに新しい DocumentSession を作成します:
private void SaveError(Error error)
{
var documentStore = new DocumentStore { ConnectionStringName = "RavenDB" };
documentStore.Initialize();
using (var session = documentStore.OpenSession())
{
documentSession.Store(error);
documentSession.SaveChanges();
}
}
2.TransactionScope 内でのラッピング
private void SaveError(Error error)
{
using (var tx = new TransactionScope())
{
documentSession.Store(error);
documentSession.SaveChanges();
tx.Complete();
}
}
現在、私は何をすべきかわかりません。どんな助けでも大歓迎です。
** * ** * **アップデート** * ** * ** * ***
SaveChanges の前に以下の行を追加することで問題を解決できました
documentSession.Advanced.Clear();.
したがって、私の SaveError は次のようになります。
private void SaveError(Models.CMSError error)
{
documentSession.Advanced.Clear();
documentSession.Store(error);
documentSession.SaveChanges();
}