Windows 7 で VS 2010 Ultimate を実行していて、MemoryCache (System.Runtime.Caching) を使用しようとしていますが、何らかの理由で、メソッドが終了するとすぐにキャッシュがクリアされ、メソッドを再実行すると、試行されます。新しいものを作成します。MSDN ドキュメントから使用しているコードは次のとおりです。
ObjectCache cache = MemoryCache.Default;
string fileContents = cache["filecontents"] as string;
if (fileContents == null)
{
CacheItemPolicy policy = new CacheItemPolicy();
List<string> filePaths = new List<string>();
filePaths.Add("c:\\Windows\\Enterprise.xml");
// policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
policy.Priority = CacheItemPriority.NotRemovable;
policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(3600);
// Fetch the file contents.
fileContents =
File.ReadAllText("c:\\Windows\\Enterprise.xml");
cache.Set("filecontents", fileContents, policy);
}
Console.WriteLine(fileContents);
このコードは Console Main メソッドにあります。
驚くべきことは、QTP から使用しているラッパー C# 4.0 アセンブリがあり、それが完全にうまく機能していることです。キャッシュは、QTP の実行ごとに保持されます。
助けてください。