【问题标题】:InvalidOperationException when using azure cache locally在本地使用 azure 缓存时出现 InvalidOperationException
【发布时间】:2013-10-04 09:32:23
【问题描述】:

尝试在本地创建 Azure 缓存实例时出现 InvalidOperationException(异常和代码如下所示)。该代码正在从控制台应用程序运行。当我在 azure 模拟器中以工作角色运行它时,它工作正常。

是否可以在不运行模拟器的情况下使用 Azure 缓存?

{System.Reflection.TargetInvocationException: 出现异常 由调用的目标抛出。 ---> System.InvalidOperationException:角色发现数据不可用
在 Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.get_Roles()

配置

  <dataCacheClients>
    <dataCacheClient name="mycache.cache.windows.net">
      <autoDiscover
        isEnabled="true"
        identifier="mycache.cache.windows.net" />
      <securityProperties mode="Message" sslEnabled="false">
        <messageSecurity authorizationInfo="[mytoken]=" />
      </securityProperties>
    </dataCacheClient>
  </dataCacheClients>

代码:

        bool autoDiscover = true;
        string identifier = "foo";

        var cacheFactoryConfiguration = new DataCacheFactoryConfiguration
        {               
            AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(autoDiscover, identifier),
            TransportProperties = new DataCacheTransportProperties { MaxBufferSize = int.MaxValue },
            IsCompressionEnabled = true,
            MaxConnectionsToServer = 3
        };

        var dataCacheFactory = new DataCacheFactory(cacheFactoryConfiguration);

【问题讨论】:

  • 您使用的是哪个 Azure Cache 选项(服务、角色内 [专用]、角色内 [共同定位] 或共享)?

标签: c# azure azure-caching


【解决方案1】:

是的,但不是自动发现功能。 AutoDiscovery 是角色内缓存的一项功能,需要 Azure RoleEnvironment(因此会出现错误)。

// Cache client configured by settings in application configuration file.
DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration("mycache.cache.windows.net");
DataCacheFactory cacheFactory = new DataCacheFactory(config);
DataCache defaultCache = cacheFactory.GetDefaultCache();    

// Put and retrieve a test object from the default cache.
defaultCache.Put("testkey", "testobject");
string strObject = (string)defaultCache.Get("testkey");

另外注意 - 我不会在 Azure 共享缓存上进行任何开发 - 该服务将于 2014 年停用 - 请参阅 http://www.windowsazure.com/en-us/pricing/details/cache/

【讨论】:

    猜你喜欢
    • 2013-03-31
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    相关资源
    最近更新 更多