创建和配置ASP.NET Session状态数据库
在基于NLB(网络负载平衡)环境下的ASP.NET Web应用程序开发,我们需要将Session存储在数据库中供多个Web应用程序调用,以下为配置方法及注意事项。
1.创建用于存储ASP.NET Session的数据库(远程、本地皆可,使用数据库用户身份认证)
在Windows\Microsoft.NET\Framework/V2.0.50727目录下使用如下命令:
aspnet_regsql.exe -S
命令执行后就会成功建立起用于存储ASP.NET Session变量的数据库了。
2.Web.Config文件配置项
我们需要在ASP.NET Web应用程序中的Web.Config文件修改sessionState配置项以使Session状态数据库生效。
配置节点如下:
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) |
| 列名 | 类型 | 描述 |
|---|---|---|
| AppId | int | Application ID |
| AppName | char(280) | Application name |
3.使用的存储过程
| Stored Procedure | Description |
|---|---|
| CreateTempTables | Creates the ASPStateTempSessions and ASPStateTempApplications tables; called during setup, but not called by SqlSessionStateStore. |
| DeleteExpiredSessions | Used by SQL Server Agent to remove expired sessions. |
| GetHashCode | Hashes an application name and returns the hash; called by TempGetAppID. |
| GetMajorVersion | Returns SQL Server's major version number. |
| TempGetAppID | Converts an application name into an application ID; queries the ASPStateTempApplications table and inserts a new record if necessary. |
| TempGetStateItem | Retrieves read-only session state from the database (ASP.NET 1.0; ASP.NET 1.1/SQL Server 7). |
| TempGetStateItem2 | Retrieves read-only session state from the database (ASP.NET 1.1). |
| TempGetStateItem3 | Retrieves read-only session state from the database (ASP.NET 2.0). |
| TempGetStateItemExclusive | Retrieves read/write session state from the database (ASP.NET 1.0; ASP.NET 1.1/SQL Server 7). |
| TempGetStateItemExclusive2 | Retrieves read/write session state from the database (ASP.NET 1.1). |
| TempGetStateItemExclusive3 | Retrieves read/write session state from the database (ASP.NET 2.0). |
| TempGetVersion | Marker whose presence indicates to ASP.NET 2.0 that the session state database is ASP.NET 2.0-compatible. |
| TempInsertStateItemLong | Adds a new session, whose size is > 7,000 bytes, to the database. |
| TempInsertStateItemShort | Adds a new session, whose size is <= 7,000 bytes, to the database. |
| TempInsertUninitializedItem | Adds a new uninitialized session to the database in support of cookieless sessions. |
| TempReleaseStateItemExclusive | Releases a lock on a session; called when ASP.NET determines that a request has timed out and calls the provider's ReleaseItemExclusive method. |
| TempRemoveStateItem | Removes a session from the database when the session is abandoned. |
| TempResetTimeout | Resets a session's timeout by writing the current date and time to the corresponding record's Expires field. |
| TempUpdateStateItemLong | Updates a session whose size is > 7,000 bytes. |
| TempUpdateStateItemLongNullShort | Updates a session whose old size is <= 7,000 bytes, but whose new size is > 7,000 bytes. |
| TempUpdateStateItemShort | Updates a session whose size is <= 7,000 bytes. |
| TempUpdateStateItemShortNullLong | Updates a session whose old size is > 7,000 bytes, but whose new size is <= 7,000 bytes. |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/16436858/viewspace-551828/ ,如需转载,请注明出处,否则将追究法律责任。