用Appfabric cache儲存asp.net Session遇到的問題及總結

來源:互聯網
上載者:User

折騰了一天, 才初步搭成功測試環境, 步驟如下:

安裝, 配置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

 

警告

建議您確保用於指定緩衝主機名稱的 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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.