1

我正在创建一个新的 MVC 应用程序来从 Azure 数据缓存中提取数据。但是,一旦我尝试实例化一个DataCache对象,我的代码就会无限期地挂起。我没有收到错误或超时,它只是坐在那里试图创建新对象。

目前我的代码实际上不过是:

public ActionResult Index()
{
  DataCache cache = new DataCache();
  Debugger.break;
}

但是永远不会超过new DataCache()声明。如果我在 Visual Studio 中暂停调试器,我可以看到它new DataCache()在线上暂停,所以这绝对是执行停止的地方。

我的 web.config 在导入新的 azure 缓存包时包含 NuGet 添加的部分,如下所示:

<dataCacheClients>
  <dataCacheClient name="default">
    <!--To use the in-role flavor of Windows Azure Caching, set identifier to be the cache cluster role name -->
    <!--To use the Windows Azure Caching Service, set identifier to be the endpoint of the cache cluster -->
    <autoDiscover isEnabled="true" identifier="{{REMOVED}}" />

    <!--<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />-->

    <!--Use this section to specify security settings for connecting to your cache. This section is not required if your cache is hosted on a role that is a part of your cloud service. -->
    <securityProperties mode="Message" sslEnabled="false">
      <messageSecurity authorizationInfo="{{REMOVED}}" />
    </securityProperties>
  </dataCacheClient>
</dataCacheClients>

我已经仔细检查了 web.config 中的值与 Azure 门户中的值是否匹配,它们没问题。

有谁知道这是什么原因?考虑到它有多新,我猜它是非常基本的东西。

4

7 回答 7

1

我有一个开发人员坐在我对面,当他使用 IIS Express 时无法访问缓存。

当他切换到 IIS Proper 时,它可以工作。

于 2014-02-07T19:15:38.727 回答
0

我发现 DataCache 构造函数由于性能计数器初始化而无限期挂起。我已经看到了各种可能导致问题的报告,例如安装 Office 2013。

我已通过运行ClientPerfCountersInstaller.exe安装 Azure 缓存 NuGet 包时添加到项目中的程序来解决此问题。

解决方案资源管理器中的 ClientPerfCountersInstaller

使用管理权限打开命令提示符并打开cd此文件夹。然后使用以下命令运行安装程序:

ClientPerfCountersInstaller.exe install

之后我的项目运行没有挂起。

于 2014-04-17T09:45:07.487 回答
0

您肯定添加了正确的 NuGet 包。您使用的是缓存服务(预览版)的基本产品,而不是共享缓存 - 已被弃用。

在标识符字段中,您是否指定了完整的端点名称 - [yourcachename].cache.windows.net?

于 2013-09-12T20:27:46.590 回答
0

Here's my guess, based on my own experience. I'm betting you're using the Cache Service preview, you are trying to access from your local network (as opposed to from an Azure instance) and your network blocks outbound tcp ports (as many corporations do by default).

You said there were no timeouts, but how long did you wait? I have found that it takes an excessive amount of time (a few minutes perhaps?) to get failures in this case, but they do come. And the exceptions indicate the port number the cache client is trying to use. I've seen ports 22233 and 22234, both of which were blocked on my corporate network. The problem went away when I convinced my IT group to allow the traffic through on those ports.

I have yet to find documentation of all ports the cache client might want to use, nor has a forum post on the subject been answered.

于 2014-01-12T20:53:48.937 回答
0

几个小时以来,我一直在努力解决这个问题,直到我遇到了解释它的堆栈溢出。TL;DR:最新版本的软件包不能很好地与 SDK 的 2.3 版配合使用。当我回滚到 Azure 缓存 2.1 时,一切正常。

使用 Windows Azure 缓存时出现异常:不知道这样的主机

于 2014-08-12T07:12:39.653 回答
0

您正在使用共享缓存(缓存服务预览版) - 其他两个选项是“角色”缓存 - 在Windows Azure Cache上阅读更多内容。假设您在配置中的所有设置都是正确的,那么,您没有正确实例化缓存。阅读如何使用 Windows Azure 缓存服务(预览版)。您需要使用:

DataCache cache = new DataCache("default");

或者:

// Cache client configured by settings in application configuration file.
DataCacheFactory cacheFactory = new DataCacheFactory();
DataCache cache = cacheFactory.GetDefaultCache();
// Or DataCache cache = cacheFactory.GetCache("default");
// cache can now be used to add and retrieve items.

最后,创建缓存对象的成本很高——您应该为缓存创建一个新的单例类,以便创建一次——而不是每次调用操作时。这是单例类的完整示例:

public static class MyCache
{
    private static DataCacheFactory _cacheFactory = null;
    private static ObjectCache Cache
    {
        get
        {
            return MemoryCache.Default;
        }
    }

    private static DataCache ACache
    {
        get
        {
            if (_cacheFactory == null)
            {
                try
                {
                    _retryPolicy.ExecuteAction(() => { _cacheFactory = new DataCacheFactory(); });
                    return _cacheFactory == null ? null : _cacheFactory.GetDefaultCache();
                }
                catch (Exception ex)
                {
                    ErrorSignal.FromCurrentContext().Raise(ex);
                    return null;
                }
            }

            return _cacheFactory.GetDefaultCache();
        }
    }

    public static void FlushCache()
    {
        ACache.Clear();
    }

    // Define your retry strategy: retry 3 times, 1 second apart.
    private static readonly FixedInterval  _retryStrategy = new FixedInterval(3, TimeSpan.FromSeconds(1));

    // Define your retry policy using the retry strategy and the Windows Azure storage
    // transient fault detection strategy.
    private static RetryPolicy _retryPolicy = new RetryPolicy<StorageTransientErrorDetectionStrategy>(_retryStrategy);

    // Private constructor to prevent instantiation
    // and force consumers to use the Instance property
    static MyCache()
    { }

    /// <summary>
    /// Add an item to the cache with a key and set a absolute expiration on it
    /// </summary>
    public static void Add(string key, object value, int minutes = 90)
    {
        try
        {
            if (RoleEnvironment.IsAvailable)
            {
                _retryPolicy.ExecuteAction(() => { ACache.Put(key, value, TimeSpan.FromMinutes(minutes)); });
            }
            else
            {
                Cache.Add(key, value, new CacheItemPolicy { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(minutes) });
            }
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
        }
    }

    /// <summary>
    /// Add the object with the specified key to the cache if it does not exist, or replace the object if it does exist and set a absolute expiration on it
    /// only valid for Azure caching
    /// </summary>
    public static void Put(string key, object value, int minutes = 90)
    {
        try
        {  
            if (RoleEnvironment.IsAvailable)
            {
                _retryPolicy.ExecuteAction(() => { ACache.Put(key, value, TimeSpan.FromMinutes(minutes)); });
            }
            else
            {
                Cache.Add(key, value, new CacheItemPolicy { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(minutes) });
            }
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
        }
    }

    /// <summary>
    /// Get a strongly typed item out of cache
    /// </summary>
    public static T Get<T>(string key) where T : class
    {
        try
        {
            object value = null;

            if (RoleEnvironment.IsAvailable)
            {
                _retryPolicy.ExecuteAction(() => { value = ACache.Get(key); });
            }
            else
            {
                value = Cache[key];
            }

            if (value != null) return (T) value;
            return null;
        }
        catch (DataCacheException ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
            return null;
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
            return null;
        }
    }
    /// <summary>
    /// Microsoft's suggested method for cleaning up resources such as this in a static class
    /// to ensure connections and other consumed resources are returned to the resource pool
    /// as quickly as possible.
    /// </summary>
    public static void Uninitialize()
    {
        if (_cacheFactory == null) return;
        _cacheFactory.Dispose();
        _cacheFactory = null;
    }
}
于 2013-09-06T16:13:53.927 回答
0

我从 Hours 开始就在解决这个问题,并通过卸载 SDK v2.6 并安装 SDK 2.4 来解决这个问题。

有关说明,请参见下文。 https://www.nuget.org/packages/Microsoft.WindowsAzure.Caching/2.4.0

于 2015-07-06T17:45:43.337 回答