更新:我现在已经正确实现了这一点。有关更多信息,请参阅我的blog post。
我正在尝试使用带有NHibernate的AppFabric作为我的二级缓存提供程序,但是我得到了以下错误:错误代码:初始化:无法联系缓存服务。有关可能的原因,请与管理员联系并参阅产品帮助文档。
我认为问题出在我在web.config中的配置:
<section name="dcacheClient"
type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core"
allowLocation="true"
allowDefinition="Everywhere"/>
...
<dcacheClient deployment="routing" localCache="False">
<localCache isEnabled="false" sync="TimeoutBased" ttlValue="300" />
<hosts>
<host name="localhost" cachePort="22233" cacheHostName="AppFabricCachingService" />
</hosts>
</dcacheClient>
我已经下载了NHibernate.Caches源代码,试图找出问题所在,当调用GetCache方法时,在VelocityClient构造函数中抛出异常:
public VelocityClient(string regionName, IDictionary<string, string> properties)
{
region = regionName.GetHashCode().ToString(); //because the region name length is limited
var cacheCluster = new CacheFactory();
cache = cacheCluster.GetCache(CacheName);
try
{
cache.CreateRegion(region, true);
}
catch (CacheException) {}
}
如果我向cacheCluster变量添加一个监视,我可以找到一个_servers私有变量,它有一个将System.Data.Caching.EndpointID属性设置为cacheCluster的MyURI属性,我认为它来自web.config中的配置。
如果您不知道问题的确切原因,但对如何解决此问题有一些想法,也将不胜感激。
附加信息
我从命令Get-CacheHostConfig -HostName tn-staylor-02 -CachePort 22233
获得了以下结果
HostName : tn-staylor-02
ClusterPort : 22234
CachePort : 22233
ArbitrationPort : 22235
ReplicationPort : 22236
Size : 3001 MB
ServiceName : AppFabricCachingService
HighWatermark : 90%
LowWatermark : 70%
IsLeadHost : True
因此,我认为我在web.config中配置的值是正确的。
在谷歌上搜索这个问题并首先研究如何设置AppFabric时,我遇到了两种在web.config中配置缓存的略有不同的方法。我上面描述的方式和汉斯尔曼在他的AppFabric blog post中的方式
我实际上是这样开始的,但是,我得到了以下错误,这就是我如何让它配置我现在拥有它的方式:
ErrorCode:应用程序配置文件中未指定“dcacheClient”标记。在配置文件中指定有效的标记。
在VelocityClient中抛出的异常的完整堆栈跟踪:
应用程序配置文件中未指定System.Data.Caching.CacheException“ErrorCode:\”dcacheClient\“”标记。请在配置文件中指定有效标记。“Source="CacheBaseLibrary“ErrorCode="ERRCMC0004”StackTrace: at System.Data.Caching.ClientConfigFile.ThrowException(String errorCode,字符串参数)在System.Data.Caching.ClientConfigReader.GetDeployementMode()在System.Data.Caching.ClientConfigurationManager.InitializeDepMode(ClientConfigReader cfr)在System.Data.Caching.ClientConfigurationManager.Initialize(String路径)在System.Data.Caching.ClientConfigurationManager..ctor()在System.Data.Caching.CacheFactory.InitCacheFactory()在System.Data.Caching.CacheFactory.GetCache(String cacheName)在NHibernate.Caches.Velocity.VelocityClient..ctor(String regionName,C:\Source\Projects\NHibernate.contrib\trunk\src\NHibernate.Caches\Velocity\NHibernate.Caches.Velocity\VelocityClient.cs:line 67 InnerException中的IDictionary`2属性):
编辑:根据@PhilPursglove的请求添加来自get-cachehost
的输出
来自get-cachehost
的输出
HostName : CachePort Service Name Service Status Version Info
-------------------- ------------ -------------- ------------
tn-staylor-02:22233 AppFabricCachingService UP 1 [1,1][1,1]
解决方案:@PhilPursglove恰到好处。NHibernate速度提供者使用的是旧的动态链接库,所以升级它们并进行一些代码更改就解决了我的问题。我想我应该在这里包含我的完整解决方案。
VelocityClient.cs
using System;
using System.Collections.Generic;
using Microsoft.ApplicationServer.Caching;
using log4net;
using NHibernate.Cache;
using CacheException = Microsoft.ApplicationServer.Caching.DataCacheException;
using CacheFactory = Microsoft.ApplicationServer.Caching.DataCacheFactory;
namespace NHibernate.Caches.Velocity
{
public class VelocityClient : ICache
{
private const string CacheName = "nhibernate";
private static readonly ILog log;
private readonly DataCache cache;
private readonly string region;
private Dictionary<string, DataCacheLockHandle> locks = new Dictionary<string, DataCacheLockHandle>();
static VelocityClient()
{
log = LogManager.GetLogger(typeof (VelocityClient));
}
public VelocityClient() : this("nhibernate", null) {}
public VelocityClient(string regionName) : this(regionName, null) {}
public VelocityClient(string regionName, IDictionary<string, string> properties)
{
region = regionName.GetHashCode().ToString(); //because the region name length is limited
var cacheCluster = new CacheFactory();
cache = cacheCluster.GetCache(CacheName);
try
{
cache.CreateRegion(region);
}
catch (CacheException) {}
}
#region ICache Members
public object Get(object key)
{
if (key == null)
{
return null;
}
if (log.IsDebugEnabled)
{
log.DebugFormat("fetching object {0} from the cache", key);
}
DataCacheItemVersion version = null;
return cache.Get(key.ToString(), out version, region);
}
public void Put(object key, object value)
{
if (key == null)
{
throw new ArgumentNullException("key", "null key not allowed");
}
if (value == null)
{
throw new ArgumentNullException("value", "null value not allowed");
}
if (log.IsDebugEnabled)
{
log.DebugFormat("setting value for item {0}", key);
}
cache.Put(key.ToString(), value, region);
}
public void Remove(object key)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
if (log.IsDebugEnabled)
{
log.DebugFormat("removing item {0}", key);
}
if (Get(key.ToString()) != null)
{
cache.Remove(region, key.ToString());
}
}
public void Clear()
{
cache.ClearRegion(region);
}
public void Destroy()
{
Clear();
}
public void Lock(object key)
{
DataCacheLockHandle lockHandle = null;
if (Get(key.ToString()) != null)
{
try
{
cache.GetAndLock(key.ToString(), TimeSpan.FromMilliseconds(Timeout), out lockHandle, region);
locks.Add(key.ToString(), lockHandle);
}
catch (CacheException) {}
}
}
public void Unlock(object key)
{
DataCacheLockHandle lockHandle = null;
if (Get(key.ToString()) != null)
{
try
{
if (locks.ContainsKey(key.ToString()))
{
cache.Unlock(key.ToString(), locks[key.ToString()], region);
locks.Remove(key.ToString());
}
}
catch (CacheException) {}
}
}
public long NextTimestamp()
{
return Timestamper.Next();
}
public int Timeout
{
get { return Timestamper.OneMs * 60000; } // 60 seconds
}
public string RegionName
{
get { return region; }
}
#endregion
}
}
NHibernate.config:
...
<property name="cache.provider_class">NHibernate.Caches.Velocity.VelocityProvider, NHibernate.Caches.Velocity</property>
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache">true</property>
...
web.config
...
<section name="dataCacheClient"
type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
allowLocation="true"
allowDefinition="Everywhere"/>
...
<dataCacheClient>
<!-- cache host(s) -->
<hosts>
<host
name="localhost"
cachePort="22233"/>
</hosts>
</dataCacheClient>
...
我没有对我的App Fabric配置或其他任何东西做任何进一步的更改。
发布于 2010-07-13 22:08:35
我认为这里有两个可能的元凶:
hosts
元素下,您列出了localhost
-我会尝试将其替换为实际的服务器名称tn-staylor-02
CacheBaseLibrary
-我不太了解(阅读:任何内容!)关于NHibernate,但我敢说缓存可能不是用AppFabric的发布版本构建的- CacheBaseLibrary是一个出现在CTP和betas中的程序集,但我不认为它在RTM版本中使用。请注意,在dcacheclient
的section元素中,它引用Microsoft.ApplicationServer.Caching.Core
程序集。https://stackoverflow.com/questions/3233792
复制相似问题