租用的空间session极其容易丢失。asp.net中,为了解决session丢失的问题,提供了三种session的存储方式。
一种是常规方式,就是存储在asp.net服务器进程中,这种是最容易丢失的。进程池的回收、服务进程的崩溃,都会导致你网站的访客无缘无故被退出登录。
一种是StateServer,他是服务器上的一个系统服务。如果您已经打开了这项系统服务,即可在任务管理器中可以查看到名为aspnet_state.exe的进程。打开方法如图:
但是,有很多租用的空间都不会为了你打开这项服务,毕竟这项服务也要消耗系统的资源。
最后一种方式,就是使用SQLServer来存储Session状态。这种方法不在乎你服务器是否有打开状态服务。只需要你有一个SQL数据库即可。
重点描述第三种,使用SQLServer来存储Session状态:
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>
例子:aspnet_regsql.exe -S 61.135.163.94 -U DbUser -P DbPassWord -ssadd -sstype c -d DbName
命令执行后就会成功建立起用于存储ASP.NET Session变量的数据库了。(我尝试的时候返回一大堆错误,但是查看服务器,已经存在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
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.
ASP.NET 状态数据库FAQ
1.如果把SESSION值存放到数据库中去,用户关闭了程序那怎么样清空数据库里的SESSION值呢?
实际ASP.NET在创建状态数据库的时候会在SQL Server代理(SQL Server Agent)的作业中添加一个作业,名称为<状态数据库名>_Job_DeleteExpiredSessions。如果打开SQL Server代理服务数据库可以通过添加的状态记录的超时时间字段(Exprires)定期对超时的状态数据进行删除。
2.ASPStateTempSessions表中的SessionId字段如何使用?
数据库中此表的SessionID字段的值,由SessionID和AppID共同组成,最后8位为AppID所以,后8位之前一定是SessionID。例如,存储在数据库中的值为"ekr30c3mwvnc3145yrswew3a037e5e5a",后8位的"037e5e5a"为AppID,而前面的"ekr30c3mwvnc3145yrswew3a"为应用程序中你可以使用Session.SessionID获得的字符串。
3.如何判断Session何时被更新的?
Session记录被更新时会同时更新Expires和LockDateLocal,Expires字段为UTC时间,如果想通过本地之间进行比较判断还是需要使用LockDateLocal。
4.获得Web.config配置文件节点信息的程序?