在Nhibernate配置中添加二级缓存时出错



我已经迁移了我的遗留Asp。. Net项目。Net Core 6解决方案。我试图通过添加第二级缓存提供程序('CoreDistributedCacheProvider')来构建NHibernate会话工厂,但我得到以下错误:

InvalidOperationException: CacheFactory是null,没有缓存工厂不能建立分布式缓存。在构建会话工厂之前,请在coredistributedcache配置部分提供工厂类属性或setCacheFactory。

我曾经有一个"Web"。Config'文件包含上述错误中提到的相关配置部分,但迁移后只有'appsettings '。json文件。我是。net Core概念的新手,我想给我一个关于如何定义适当的配置的提示。

有一个选项可以根据文档(https://nhibernate.info/doc/nhibernate-reference/caches.html#NHibernate.Caches.ConfigurationProvider)以编程方式设置配置,使用如下:

ConfigurationProvider.SetConfiguration(yourConfig);

,但我如何定义和读取xml格式的设置配置在。net Core 6应用程序?

Nhibernate配制:

FluentConfiguration fluentConfiguration = Fluently.Configure()
.Database(PostgreSQLConfiguration.Standard.Dialect<MySQL5Dialect>().ConnectionString(x => 
x.Host("**")
.Port(5432)
.Database("**")
.Username("**")
.Password("**")
))
.Mappings(m => m.FluentMappings.AddFromAssembly(assembly).Conventions.Add(new DatabaseConvention())).Cache(o => o.UseSecondLevelCache().UseQueryCache().ProviderClass<CoreDistributedCacheProvider>());

网络。Config legacy Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="coredistributedcache"
type="NHibernate.Caches.CoreDistributedCache.CoreDistributedCacheSectionHandler,
NHibernate.Caches.CoreDistributedCache" />
</configSections>
<coredistributedcache
factory-class="NHibernate.Caches.CoreDistributedCache.Memory.MemoryFactory,
NHibernate.Caches.CoreDistributedCache.Memory">
<properties>
<property name="expiration-scan-frequency">00:10:00</property>
<property name="size-limit">1048576</property>
<property name="cache.serializer"
>NHibernate.Caches.Util.JsonSerializer.JsonCacheSerializer, NHibernate.Caches.Util.JsonSerializer</property>
</properties>
<cache region="foo" expiration="500" sliding="true" />
<cache region="noExplicitExpiration" sliding="true" />
<cache region="specificSerializer"
serializer="NHibernate.Caches.Common.BinaryCacheSerializer, NHibernate.Caches.Common" />
</coredistributedcache>
</configuration>

您可以将其保存在xml中(作为嵌入式资源或作为复制到输出目录或您选择的文件),并使用以下自定义ConfigurationProvider加载:

//Call it before session factory is created
ConfigurationProvider.Current = new CoreDistributedCacheXmlConfiguration(xmlCacheConfig);

public class CoreDistributedCacheXmlConfiguration : ConfigurationProvider
{
private readonly string _xmlCacheConfig;
public CoreDistributedCacheXmlConfiguration(string xmlCacheConfig)
{
_xmlCacheConfig = xmlCacheConfig;
}
public override CacheConfig GetConfiguration()
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(_xmlCacheConfig);
return (CacheConfig)new CoreDistributedCacheSectionHandler().Create(null, null, xmlDocument.DocumentElement);
}
}

其中xmlCacheConfig是作为字符串加载的xml。只需要coredistributedcachexml元素:

//Example with hardcoded C# 11 Raw string literal
string xmlCacheConfig =
"""
<?xml version="1.0" encoding="utf-8" ?>
<coredistributedcache
factory-class="NHibernate.Caches.CoreDistributedCache.Memory.MemoryFactory,
NHibernate.Caches.CoreDistributedCache.Memory">
<properties>
<property name="expiration-scan-frequency">00:10:00</property>
<property name="size-limit">1048576</property>
<property name="cache.serializer"
>NHibernate.Caches.Util.JsonSerializer.JsonCacheSerializer, NHibernate.Caches.Util.JsonSerializer</property>
</properties>
<cache region="foo" expiration="500" sliding="true" />
<cache region="noExplicitExpiration" sliding="true" />
<cache region="specificSerializer"
serializer="NHibernate.Caches.Common.BinaryCacheSerializer, NHibernate.Caches.Common" />
</coredistributedcache>
""";

最新更新