Configure Fluent Nhibernate with NHibernate.Caches

2019-07-26 19:32发布

问题:

I am trying to configure Fluent NHibernate with MemCache but I get the following error. I am using MVC5+CastleWindsor+FluentNHibernate+UOW

private static ISessionFactory CreateNhSessionFactory() { var connStr = System.Configuration.ConfigurationManager.ConnectionStrings["AESConnect"].ConnectionString; return Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008.ConnectionString(connStr)) .Cache(c=>c.ProviderClass(typeof(NHibernate.Caches.MemCache.MemCacheProvider).AssemblyQualifiedName).UseSecondLevelCache()) .ExposeConfiguration(e=>e.SetProperty("hibernate.cache.use_second_level_cache","true")) .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetAssembly(typeof(HotelsMap)))) .BuildSessionFactory(); }

Error:

[NullReferenceException: Object reference not set to an instance of an object.] Memcached.ClientLibrary.SockIOPool.Initialize() in C:\devroot\memcacheddotnet\trunk\clientlib\src\clientlib\SockIOPool.cs:441 NHibernate.Caches.MemCache.MemCacheProvider.Start(IDictionary`2 properties) +3627
NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners) +1098
NHibernate.Cfg.Configuration.BuildSessionFactory() +181
FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() +73

[FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

] FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() +117 AESRepositories.Installers.AESInstaller.CreateNhSessionFactory() ....

Thanks in advance

回答1:

It looks to be a problem with the server list not being initialised.

This is from the SockIOPool.cs file:

/// The pool must be initialized prior to use. This should typically be early on
/// in the lifecycle of the application instance.

The same file has an initialisation example like this:

/// String[] serverlist = {"cache0.server.com:12345", "cache1.server.com:12345"};
///
/// SockIOPool pool = SockIOPool.GetInstance();
/// pool.SetServers(serverlist);
/// pool.Initialize();  

Maybe you are missing the configuration of the connection pool, or maybe you are not initialising it.