现在的位置: 首页 > 综合 > 正文

使用SQL Server存储ASP.NET Session变量

2013年08月02日 ⁄ 综合 ⁄ 共 1403字 ⁄ 字号 评论关闭

       创建和配置ASP.NET Session状态数据库

  在基于NLB(网络负载平衡)环境下的ASP.NET Web应用程序开发,我们需要将Session存储在数据库中供多个Web应用程序调用,以下为配置方法及注意事项。

  1.创建用于存储ASP.NET Session的数据库(远程、本地皆可,使用数据库用户身份认证)
  在Windows\Microsoft.NET\Framework/V2.0.50727目录下使用如下命令:
  aspnet_regsql.exe -S <SQL Server IP> -U <User Name> -P <Password> -E -ssadd -sstype c -d <Database Name>
  命令执行后就会成功建立起用于存储ASP.NET Session变量的数据库了。

  2.Web.Config文件配置项
  我们需要在ASP.NET Web应用程序中的Web.Config文件修改sessionState配置项以使Session状态数据库生效。
配置节点如下:

以下为引用的内容:
<sessionState mode="SQLServer"
            sqlConnectionString="server=<Server IP>;database=<Database Name>;uid=<User Name>;pwd=<Password>;"
allowCustomSqlDatabase="True"
            cookieless="false" 
            timeout="20" />

  3.注意在进行系统测试(主要是负载测试)的时候,因为数据库访问负载的增加,需要调整SQL Server相应超时的配置项以适应负载。(默认值为10,请适度进行调整。)

  ASP.NET Session状态数据库数据模型
  1.ASPStateTempSessions表定义

列名 类型 描述
SessionId nvarchar(88) Session ID + application ID
Created datetime Date and time session was created (UTC)
Expires datetime Date and time session expires (UTC)
LockDate datetime UTC date and time session was locked
LockDateLocal datetime Local date and time session was locked
LockCookie int Lock ID
Timeout int Session timeout in minutes
Locked bit 1=Session locked, 0=Session not locked
SessionItemShort varbinary(7000) Serialized session state (if <= 7,000 bytes)
SessionItemLong image Serialized session state (if > 7,000 bytes)
Flags int Session state flags (1=Uninitialized session)

  2.ASPStateTempApplications表定义
列名 类型 描述
AppId int Application ID
AppName char(280) Application name

抱歉!评论已关闭.