C# 无webconfig的Azure托管缓存

C# 无webconfig的Azure托管缓存,c#,azure,caching,C#,Azure,Caching,我正在按照教程使用Azure Redis缓存。如何在不使用webconfig的情况下使用此缓存?这是我的密码 c代码 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用Microsoft.ApplicationServer.Caching; 使用系统数据; 使用System.Data.SqlClient; 命名空间MoviesDB.CacheWrappers { 公共类AzureManagedCache { DataCach

我正在按照教程使用Azure Redis缓存。如何在不使用webconfig的情况下使用此缓存?这是我的密码

c代码

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.ApplicationServer.Caching;
使用系统数据;
使用System.Data.SqlClient;
命名空间MoviesDB.CacheWrappers
{
公共类AzureManagedCache
{
DataCache AzureCache=新数据缓存(“默认”);
public void Add(字符串sqlQuery,数据表记录)
{
列表电影=新列表();
movies=MovieDBContext.ConvertToMovies(记录);
Add(“sqlQuery”,movies);
}
公共作废删除(字符串sqlQuery)
{
删除(“sqlQuery”);
}
公共对象读取(字符串sqlQuery)
{
var movie=AzureCache.Get(“sqlQuery”);
回归电影;
}
}
}
以及webconfig.xml

<dataCacheClients>
    <dataCacheClient name="default">
      <!--To use the in-role flavor of Windows Azure Cache, set identifier to be the cache cluster role name 
      To use the Windows Azure Cache Service, set identifier to be the endpoint of the cache cluster--> 
      <autoDiscover isEnabled="true" identifier="******.cache.windows.net" />

      <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="true">
        <messageSecurity authorizationInfo="******" />
      </securityProperties>
    </dataCacheClient>
  </dataCacheClients>

以编程方式与托管缓存对话的代码:

var cacheSvcUri = "<your-cache-name>.cache.windows.net";

// Setup DataCacheSecurity configuration
string cacheSvcAcsKey = "<your-acs-key>";
var secureAcsKey = new SecureString();
foreach (char c in cacheSvcAcsKey)
{
secureAcsKey.AppendChar(c);
}
secureAcsKey.MakeReadOnly();

// Setup the DataCacheFactory configuration
DataCacheFactoryConfiguration dcfConfig = new DataCacheFactoryConfiguration();

// Set autodiscover for in-role

dcfConfig.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, cacheSvcUri);

DataCacheSecurity factorySecurity = new DataCacheSecurity(secureAcsKey);
dcfConfig.SecurityProperties = factorySecurity;

// Create a configured DataCacheFactory object.
DataCacheFactory dcf= new DataCacheFactory(dcfConfig); // make the DataCacheFactory static as it connects to the cache cluster.

// Get a cache client for the default cache.
DataCache defaultCache = dcf.GetDefaultCache();

defaultCache.Put("One", 1);
defaultCache.Put("Two", 2);
var cacheSvcUri=“.cache.windows.net”;
//设置DataCacheSecurity配置
字符串cacheSvcAcsKey=“”;
var secureAcsKey=new SecureString();
foreach(cacheSvcAcsKey中的字符c)
{
secureAcsKey.AppendChar(c);
}
secureAcsKey.MakeReadOnly();
//设置DataCacheFactory配置
DataCacheFactoryConfiguration dcfConfig=新的DataCacheFactoryConfiguration();
//为角色中的设置自动发现
dcfConfig.AutoDiscoverProperty=新数据缓存AutoDiscoverProperty(true,cacheSvcUri);
DataCacheSecurity factorySecurity=新的DataCacheSecurity(secureAcsKey);
dcfConfig.SecurityProperties=factorySecurity;
//创建已配置的DataCacheFactory对象。
DataCacheFactory dcf=新的DataCacheFactory(dcfConfig);//使DataCacheFactory在连接到缓存群集时保持静态。
//获取默认缓存的缓存客户端。
DataCache defaultCache=dcf.GetDefaultCache();
defaultCache.Put(“一”,1);
defaultCache.Put(“两个”,2);
在缓存租户中定义要与哪个命名缓存对话后,可以添加缓存操作。在这里,它是“默认值”(
DataCache-defaultCache=dcf.GetDefaultCache()
)。如果您想与任何其他命名缓存对话,那么它应该是
DataCache defaultCache=dcf.GetCache(“”)

var cacheSvcUri = "<your-cache-name>.cache.windows.net";

// Setup DataCacheSecurity configuration
string cacheSvcAcsKey = "<your-acs-key>";
var secureAcsKey = new SecureString();
foreach (char c in cacheSvcAcsKey)
{
secureAcsKey.AppendChar(c);
}
secureAcsKey.MakeReadOnly();

// Setup the DataCacheFactory configuration
DataCacheFactoryConfiguration dcfConfig = new DataCacheFactoryConfiguration();

// Set autodiscover for in-role

dcfConfig.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, cacheSvcUri);

DataCacheSecurity factorySecurity = new DataCacheSecurity(secureAcsKey);
dcfConfig.SecurityProperties = factorySecurity;

// Create a configured DataCacheFactory object.
DataCacheFactory dcf= new DataCacheFactory(dcfConfig); // make the DataCacheFactory static as it connects to the cache cluster.

// Get a cache client for the default cache.
DataCache defaultCache = dcf.GetDefaultCache();

defaultCache.Put("One", 1);
defaultCache.Put("Two", 2);