AppFabric:无法联系缓存服务

s1m*_*m0t 18 nhibernate appfabric

更新:我现在已经正确实现了这一点.有关更多信息,请参阅我的博客文章.

我正在尝试使用AppFabric和NHibernate作为我的二级缓存提供程序,但是我收到以下错误:ErrorCode:Initialization:无法联系缓存服务.有可能的原因,请联系管理员并参阅产品帮助文档.

我认为问题出在我在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>
Run Code Online (Sandbox Code Playgroud)

我已经下载了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) {}
  }
Run Code Online (Sandbox Code Playgroud)

如果我将一个监视添加到cacheCluster变量,我可以找到一个_servers私有变量,它有一个System.Data.Caching.EndpointID,其MyURI属性设置为net.tcp:// localhost:22234/AppFabricCachingServive,我认为它已经来了来自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
Run Code Online (Sandbox Code Playgroud)

所以我认为我在web.config中配置的值是可以的.


谷歌搜索这个问题,并首先研究如何设置AppFabric,我遇到了两种稍微不同的方式来配置web.config中的缓存.我上面描述的方式和Hanselman在AppFabric博客文章中的方式

我实际上是这样开始的,然而,我得到了以下错误,这就是我如何配置它现在如何拥有它:

ErrorCode:应用程序配置文件中未指定"dcacheClient"标记.在配置文件中指定有效标记.


在VelocityClient中抛出异常的完整堆栈跟踪:

发生System.Data.Caching.CacheException消息="ErrorCode:\"dcacheClient \"标记未在应用程序配置文件中指定.在配置文件中指定有效标记." Source ="CacheBaseLibrary"ErrorCode ="ERRCMC0004"StackTrace:System.Data.Caching.ChrowException(String errorCode,String param),位于System.Data.Caching.ClientConfigurationManager的System.Data.Caching.ClientConfigReader.GetDeployementMode()处. System.Data.Caching中的System.Data.Caching.CartFactory.InitCacheFactory()中的System.Data.Caching.ClientConfigurationManager..ctor()处的System.Data.Caching.ClientConfigurationManager.Initialize(String path)中的InitializeDepMode(ClientConfigReader cfr) NHibernate.Caches中的.CacheFactory.GetCache(String cacheName).


编辑:get-cachehost根据@PhilPursglove的要求添加输出

输出来自get-cachehost:

HostName : CachePort      Service Name            Service Status Version Info
--------------------      ------------            -------------- ------------
tn-staylor-02:22233       AppFabricCachingService UP             1 [1,1][1,1]
Run Code Online (Sandbox Code Playgroud)

解决方案:@PhilPursglove是现货.NHibernate速度提供程序使用旧的dll,因此升级它们并进行一些代码更改解决了我的问题.我想我会在这里提供完整的解决方案.

  1. 通过https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk从SVN存储库下载NHibernate.contrib源代码
  2. 打开NHibernate.Caches.Everything解决方案并从NHibernate.Caches.Velocity项目中删除对旧的velocity dll的引用.
  3. 添加了对安装App Fabric时安装的App Fabric dll的引用.这不是在GAC中添加对程序集的引用的常规情况,但本文介绍了如何执行此操作.
  4. 添加新引用意味着不再编译VelocityClient类.随着从一点点帮助这个我想出了下面VelocityClient.cs的版本.
  5. 我在我的项目中添加了对NHibernate.Caches.Velocity新版本的引用,将下面的更改改为我的配置,一切正常.

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
...
Run Code Online (Sandbox Code Playgroud)

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>
...
Run Code Online (Sandbox Code Playgroud)

我没有对App Fabric配置或其他任何内容进行任何进一步的更改.

Phi*_*ove 6

我认为这里有两个可能的罪魁祸首:

  1. hosts元素下的web.config中,您将列出localhost- 我会尝试将其交换为实际的服务器名称tn-staylor-02

  2. 该异常堆栈跟踪指的是CacheBaseLibrary- 我不知道有关NHibernate的很多内容(阅读:任何内容!),但我猜测该缓存可能无法使用AppFabric的发布版本构建 - CacheBaseLibrary是一个出现在CTP和beta版,但我不认为它在RTM版本中使用过.请注意,在section元素中dcacheclient,它指的是Microsoft.ApplicationServer.Caching.Core程序集.