用Appfabric cache存储asp.net Session遇到的问题及总结

折腾了一天, 才初步搭成功测试环境, 步骤如下:

安装, 配置appfabric在此就不赘述了, 直奔主题.

 

1.新建一个asp.net website, 使用.net 4.0,.net2.0, 3.5还没测试。


2.添加引用
如果是32位机器,cache类库在下面位置:

C:\Windows\System32\AppFabric\Microsoft.ApplicationServer.Caching.Client.dll
C:\Windows\System32\AppFabric\Microsoft.ApplicationServer.Caching.Core.dll

如果是win2008 server 64位,则cache类库在下面位置:

C:\Windows\SysNative\AppFabric\Microsoft.ApplicationServer.Caching.Client.dll
C:\Windows\SysNative\AppFabric\Microsoft.ApplicationServer.Caching.Core.dll

当然,直接把这两个文件拷贝到方便寻找的地方再引用应该也没问题。

 

非常奇怪的是,直接在windows目录下找SysNative这个目录还找不着搜不到,但添加引用就能找到,着实莫名其妙了一阵子,ms在搞什么?

看了下面这篇帖子才知道, SysNative这个目录是不存在的, 它是windows的一种目录重定向机制.

神奇的SysNative文件夹 http://leonax.net/p/2601/magic-of-sysnative-folder/


3.然后,就该修改web.config了。

完整的内容如下:

<configuration>
 <configSections>
  <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"/>
 </configSections>
 <dataCacheClient>
  <hosts>
   <host name="BobDesktopPc" cachePort="22233"/>
  </hosts>
    <securityProperties mode="None" protectionLevel="None"/>
  </dataCacheClient>
 <system.web>
  <sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
   <providers>
    <!-- specify the named cache for session data -->
    <add name="AppFabricCacheSessionStoreProvider" type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider" cacheName="default"/>
   </providers>
  </sessionState>
  <compilation debug="true" targetFramework="4.0">
   <assemblies>
    <add assembly="Microsoft.ApplicationServer.Caching.Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
   </assemblies>
  </compilation>
 </system.web>
</configuration>

上边粗体的两行值得注意,我就是没注意到要添加<securityProperties mode="None" protectionLevel="None"/>这句,从而报出下面这个错。

Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: ErrorCode<ERRCA0017>:SubStatus<ES0006>:There is a temporary failure. Please retry later. (One or more specified Cache servers are unavailable, which could be caused by busy network or servers. Ensure that security permission has been granted for this client account on the cluster and that the AppFabric Caching Service is allowed through the firewall on all cache hosts. Retry later.)

 
我是按照下面ms的指导一步步做的, 里面没有提到要加上这句, 因此试了多次都不成功, 里面只是简单给了一个警告建议, 我认为就是暗示要加上这句的, 但是, 这...这也太隐晦了吧, 真是不象ms的风格, 其实这句在appfabric安装时的xml配置文件里都有, 但没想到也要加到这里, 强烈建议ms改改这个指导帖.

 

配置 ASP.NET 会话状态提供程序(Windows Server AppFabric 缓存)

http://msdn.microsoft.com/zh-cn/library/ee790859.aspx

 

Ee790859.Warning(zh-cn,MSDN.10).gif警告

建议您确保用于指定缓存主机名称的 web.config 文件的安全。

 

还是ldljlq兄弟的帖子里首先看到要加上这句<securityProperties mode="None" protectionLevel="None"/>的.

配置ASP.NET网站使用AppFabric Caching存储Session数据.
http://blog.csdn.net/ldljlq/article/details/5801765

 

cacheName="default"这个属性在web.config里总是提示, The 'cacheName' attribute is not allowed, 当初以为这样不行,后来才知,没关系,就这样就可以,但是cache一定要写对,如果指定的cache写错了或者还没有建立,则会报出下面的错误。

Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: ErrorCode<ERRCA0009>:SubStatus<ES0001>:Cache referred to does not exist. Contact administrator or use the Cache administration tool to create a Cache.

Source Error:
Line 18:    <providers>
Line 19:     <!-- specify the named cache for session data -->
Line 20:     <add name="AppFabricCacheSessionStoreProvider" type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider" cacheName="default123"/>
Line 21:    </providers>
Line 22:   </sessionState>

4.将session写入cache.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["UserName"] = "lzd";
        }       
    }
    protected void btnGet_Click(object sender, EventArgs e)
    {
        if (Session["UserName"] != null)
        {
            Response.Write(Session["UserName"]);
        }
    }
}

 

运行前后分别用Get-CacheStatistics  default统计一下cache数据, 即可知道, session已经存进去了.

PS C:\Windows\System32\WindowsPowerShell\v1.0\Modules\DistributedCacheAdministration> Get-CacheStatistics  default

Size         : 0
ItemCount    : 0
RegionCount  : 0
RequestCount : 0
MissCount    : 0

 

PS C:\Windows\System32\WindowsPowerShell\v1.0\Modules\DistributedCacheAdministration> Get-CacheStatistics  default

Size         : 265
ItemCount    : 1
RegionCount  : 1
RequestCount : 1
MissCount    : 1

 

5.关于Key referred to does not exist的异常.

有朋友说偶尔遇到下面的错误.

ErrorCode<ERRCA0006>:SubStatus<ES0001>:Key referred to does not exist. Create objects based on a Key to fix the error.

我试了一段时间, 未能重现此异常.

又搜了一下,找到下面几个链接都是类似的问题,这个异常不常发生, 但却是存在, ms也承认在Windows Azure AppFabric上存在这样的bug, 已经在2011 April的release中修复了.

Windows Azure Caching 服务与 Windows Server AppFabric Caching 基于相同的代码库,因此其开发人员体验与内部缓存相同。
因此我觉得在非Azure的AppFabric上也会有此问题, 不知道ms有没有在v1.1 ctp里解决它。

http://social.msdn.microsoft.com/Forums/en-US/windowsazuredevelopment/thread/024a5775-73d2-44f8-8701-2e9d2ecbd345/
http://social.msdn.microsoft.com/Forums/en-MY/windowsazuredevelopment/thread/024a5775-73d2-44f8-8701-2e9d2ecbd345
http://social.msdn.microsoft.com/Forums/en-US/velocity/thread/947663ce-f423-487f-a501-cb023295ebcb

http://social.msdn.microsoft.com/Forums/en-US/velocity/thread/df0231d8-3d51-4d61-9b2a-4e34a64c002a

 

个人意见, 一直以来, 我们一直把session放在state server上, 其实它除了不能共享多台机器的内存外, 其他方面和appfabric类似, 都是独立服务, 都有独立端口,  而state service这个服务久经考验, 很稳定,反而appfabric是刚出来,很有些bug之类的,  要慎重使用. 

 

参考资料:

准备缓存客户端开发环境(Windows Server AppFabric 缓存)
http://msdn.microsoft.com/zh-cn/library/ee790876.aspx

配置 ASP.NET 会话状态提供程序(Windows Server AppFabric 缓存)

http://msdn.microsoft.com/zh-cn/library/ee790859.aspx

配置ASP.NET网站使用AppFabric Caching存储Session数据.
http://blog.csdn.net/ldljlq/article/details/5801765

AppFabric Configuration Error
http://social.msdn.microsoft.com/Forums/en-US/velocity/thread/f3b579b4-6448-4308-88c7-34f412d3ea59/

Configuration Settings for the ASP.NET 4 Caching Session State Provider
http://msdn.microsoft.com/en-us/library/windowsazure/gg185682.aspx

 

你可能感兴趣的:(asp.net)