WindowsAzure 之AppFabricCache
1.使用 windows azure平台账号(Live ID)登陆 https://windows.azure.com/
2.创建AppFabricCache
点击 ”Service Bus, Access Control&Caching” 导航
选择AppFabric 下的 Cache 节点,点击New按钮
输入cache的命名空间,选择区域,点击Create Namespace
系统将显示cache 正在activing ,大概需要15分钟左右才能ready:
查看客户端配置,这里的your access token 和 yournamespace name将在下面的实验中用到,千万不要泄漏这两项信息,因为有了这两项信息就可以访问(读写)cache 了,到时可是要付美元的哦:
3.如何在程序中使用AppFabric Cache
a.Web.config 配置如下(注意替换YourNamespaceName和 YourAccessToken):
<?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <configSections> <!-- Append below entry to configSections. Do not overwrite the full section. --> <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere"/> </configSections> <!-- Cache exposes one endpoint: one simple--> <dataCacheClients> <dataCacheClient name="default"> <hosts> <host name="YourNamespaceName.cache.windows.net" cachePort="22233" /> </hosts> <securityProperties mode="Message"> <messageSecurity authorizationInfo="YourAccessToken"> </messageSecurity> </securityProperties> </dataCacheClient> <dataCacheClient name="SslEndpoint"> <hosts> <host name="YourNamespaceName.cache.windows.net" cachePort="22243" /> </hosts> <securityProperties mode="Message" sslEnabled="true"> <messageSecurity authorizationInfo="YourAccessToken"> </messageSecurity> </securityProperties> </dataCacheClient> </dataCacheClients> <system.diagnostics> <trace> <listeners> <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics"> <filter type="" /> </add> </listeners> </trace> </system.diagnostics> <connectionStrings> <!--<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />--> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0" /> <authentication mode="Forms"> <forms loginUrl="~/Account/Login.aspx" timeout="2880" /> </authentication> <membership> <providers> <clear/> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> </providers> </membership> <profile> <providers> <clear/> <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> </providers> </profile> <roleManager enabled="false"> <providers> <clear/> <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> </providers> </roleManager> <!-- If session state needs to be saved in AppFabric Caching service, add the following to web.config inside system.web. If ssl is required, then change dataCacheClientName to "SslEndpoint". --> <sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider"> <providers> <add name="AppFabricCacheSessionStoreProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default" useBlobMode="true" dataCacheClientName="SslEndpoint" /> </providers> </sessionState> <!-- If output cache content needs to be saved in AppFabric Caching service, add the following to web.config inside system.web. --> <caching> <outputCache defaultProvider="DistributedCache"> <providers> <add name="DistributedCache" type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" /> </providers> </outputCache> </caching> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>b. 创建一个类,用于模拟cache要存的信息:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebRole1 { public class Person { public string ID; public string Name = string.Empty; public int Age = 25; } }
c. 用代码存取cache:
d. 将程序部署到云端(注意cache本地调试可能会有问题,本地无法解析cache的地址),效果如下
注: 如何创建并部署云应用程序,请参考http://blog.csdn.net/farawayplace613/article/details/6933915