ASP.NET Web应用程序中用Windows Server AppFabric Cache配制Session State

         阅读这篇POST时,您需要了解Asp.net web应用程序,Windows Server AppFabric Cache. 如您不熟悉可以先阅读Windows Server AppFabric Cache 介绍一

打开Cache PowerShell 控制台(Start –> Windows Server AppFabric –> Caching Administration Windows PowerShell).

启动,如果没有启动

Start-CacheCluster

创建一个新的Cache

New-Cache MySessionStateCache

好的,接下来看WEB.CONFIG如何配制,增加配制节:

<!--configSections must be the FIRST element -->
<configSections>
  <!-- required to read the <dataCacheClient> element -->
  <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>
  <!-- cache host(s) -->
  <hosts>
    <host
        name="YOURHOSTNAME"
        cachePort="22233"/>
  </hosts>
</dataCacheClient>

在System.web配制节中增加如下配制,注意chcheName是前面用PowerShell命令创建的cacheName

    <sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
      <providers>
        <!-- specify the named cache for session data -->
        <add
          name="AppFabricCacheSessionStoreProvider"
          type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider
          , Microsoft.ApplicationServer.Caching.Client, Version=1.0.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          cacheName="MySessionStateCache"
          sharedId="SharedApp"/>
      </providers>
    </sessionState>

下面在一个asp.net page中,测试一下:

   1:      protected void Page_Load(object sender, EventArgs e)
   2:      {
   3:          // Store information into session
   4:          if (!IsPostBack)
   5:          {
   6:              Session["PageLoadDateTime"] = DateTime.Now.ToString();
   7:          }
   8:      }
   9:   
  10:      protected void GetOrder_Click(object sender, EventArgs e)
  11:      {
  12:          OrderDesc.Text = Session["PageLoadDateTime"].ToString(); 
  13:      }

运行点击Button你 拿到一个相同的时间从缓存的中。

希望这篇POST对您开发帮助。


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog

原文地址:https://www.cnblogs.com/wintersun/p/1968637.html