具有普遍性的NHibernate二级缓存

时间:2012-09-21 04:34:31

标签: winforms nhibernate caching

我正在编写一个Windows窗体应用程序,它需要将一些NHibernate的实体数据存储在持久的第二层缓存中。据我所知,满足我的应用程序要求的唯一二级缓存提供程序是普遍性,但是当我配置它时,我遇到了一个尴尬的例外:

System.ArgumentNullException was unhandled
  Message=Value cannot be null.
Parameter name: key
  Source=mscorlib
  ParamName=key
   StackTrace:
   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at NHibernate.Impl.SessionFactoryObjectFactory.GetNamedInstance(String name)
   at NHibernate.Impl.SessionFactoryImpl.GetRealObject(StreamingContext context)
   at System.Runtime.Serialization.ObjectManager.ResolveObjectReference(ObjectHolder holder)
   at System.Runtime.Serialization.ObjectManager.DoFixups()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
   at Bamboo.Prevalence.Implementation.PendingCommandsEnumerator.NextCommand()
   at Bamboo.Prevalence.Implementation.PendingCommandsEnumerator.MoveNext()
   at Bamboo.Prevalence.PrevalenceEngine.RecoverCommands(CommandLogReader reader, ExceptionDuringRecoveryHandler handler)
   at Bamboo.Prevalence.PrevalenceEngine.RecoverSystem(Type systemType, CommandLogReader reader, ExceptionDuringRecoveryHandler handler)
   at Bamboo.Prevalence.PrevalenceEngine..ctor(Type systemType, String prevalenceBase, BinaryFormatter formatter, ExceptionDuringRecoveryHandler handler)
   at Bamboo.Prevalence.TransparentPrevalenceEngine..ctor(Type systemType, String prevalenceBase, BinaryFormatter formatter, ExceptionDuringRecoveryHandler handler)
   at Bamboo.Prevalence.TransparentPrevalenceEngine..ctor(Type systemType, String prevalenceBase, BinaryFormatter formatter)
   at Bamboo.Prevalence.PrevalenceActivator.CreateTransparentEngine(Type systemType, String prevalenceBase, BinaryFormatter formatter)
   at Bamboo.Prevalence.PrevalenceActivator.CreateTransparentEngine(Type systemType, String prevalenceBase)
   at NHibernate.Caches.Prevalence.PrevalenceCacheProvider.SetupEngine()
   at NHibernate.Caches.Prevalence.PrevalenceCacheProvider.Start(IDictionary`2 properties)
   at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
   at NHibernate.Cfg.Configuration.BuildSessionFactory()
   at AcessoDados.DB.Configure() in C:\Users\Herberth\MyProject\DataAccess\DB.cs:line 78

这是我正在使用的额外代码:

configuration.SessionFactory().Caching.Through<NHibernate.Caches.Prevalence.PrevalenceCacheProvider>().PrefixingRegionsWith("MyRegion").WithDefaultExpiration(60);

当我注释掉这一行时它工作正常(当然没有缓存);

以下是我正在使用的完整代码:

configuration = new Configuration();
var mapper = new ModelMapper();
mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
HbmMapping domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
configuration.DataBaseIntegration(c =>
{
    c.Dialect<MySQLDialect>();
    c.ConnectionString = @"Server=localhost;Database=mydb;Uid=root;Pwd=mypwd";
    c.ConnectionString = DBConnectionStrings.Principal;
    c.LogFormattedSql = true;
    c.LogSqlInConsole = true;
    c.IsolationLevel = System.Data.IsolationLevel.ReadCommitted;
});
configuration.AddMapping(domainMapping);
configuration.Cache(c => { c.UseQueryCache = true; });
configuration.SessionFactory().Caching.Through<NHibernate.Caches.Prevalence.PrevalenceCacheProvider>().PrefixingRegionsWith("MyRegion").WithDefaultExpiration(60);
SessionFactory = configuration.BuildSessionFactory();

所有依赖项都是最新版本。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我遇到此问题,因为可执行文件目录中的目录NHibernate.Cache.StandardQueryCacheUpdateTimestampsCache已过期。

但是,导致下一个问题 - 我无法在Program Files下安装,因为NHibernate试图在首次运行时创建这些目录。