首先,session其虽然是基于cookie的,同时只对本次会话来产生效果,(原理很简单,就是字典内保存一个字典,外层字典的key也就是asp_netsessionId是由.NET自动分配的并保存到只对本次会话有效的cookie中,我们的工作只是负责内部我们需要的字典的内容,并有.NET自动分配一块区域保存起来,当然这个是有一定的时效性的,模拟代码可以看看资料,或者反编译下看看,很简单,这里就不说了)但是有时候我们就会发现默认的session存储方式,可能造成数据的丢失,虽然他的存储类型是object,但是我们要是存储大量的数据就经常出现数据的丢失。
下面我介绍两种人人都知道,我也刚知道的配置手段。这两种都是进程外的session机制,可以在IIS或ASP.NET服务意外关闭时继续保持状态.
1.使用aspnet_state.exe
步骤:对于win7不用找,直接打开服务,其一般是已经注册了的。
开始---运行---services.msc 找到
后打开它,之后我们在web.config内配置Session状态
42424是默认端口号(可以修改),同时默认的也是本机访问,要是需要可以在注册表配置,以满足远程服务器的
然后AllowRemoteConnection值修改为1,0代表本机。之后字需要修改web.config中的IP即可。
现在我们来测试一下效果:
我前面的就不在演示了,这里演示一下,获取session后关闭了服务器,之后再打开,我就可以看到
多次刷新session任然保持存在。
2.使用SQLServer保存
微软提供了2中保存方式(附带创建脚本)
临时储存区:InstallSqlState.sql & UninstallSqlState.sql
永久储存区:InstallPersistSqlState.sql & UninstallPersistSqlState.sql
之后配置一下web.config,
?
我们打开SSMS.
运行我们上面官方提供的脚本,这里拿出一个临时储存区的脚本运行
临时脚本
代码如下 | 复制代码 |
/********************************************************************* InstallSqlState.SQL Installs the tables, and stored procedures necessary for supporting ASP.NET session state. Copyright Microsoft, Inc. All Rights Reserved. *********************************************************************/ SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO PRINT '' PRINT '-----------------------------------------' PRINT 'Starting execution of InstallSqlState.SQL' PRINT '-----------------------------------------' PRINT '--------------------------------------------------' PRINT 'Note: ' PRINT 'Do not run this file manually. ' PRINT 'You should use aspnet_regsql.exe to install ' PRINT 'and uninstall SQL session state. ' PRINT '' PRINT 'Run ''aspnet_regsql.exe -?'' for details. ' PRINT '--------------------------------------------------' GO /*****************************************************************************/ USE master GO /* Create and populate the session state database */ IF DB_ID(N'ASPState') IS NULL BEGIN DECLARE @cmd nvarchar(500) SET @cmd = N'CREATE DATABASE [ASPState]' EXEC(@cmd) END GO /* Drop all tables, startup procedures, stored procedures and types. */ /* Drop the DeleteExpiredSessions_Job */ DECLARE @jobname nvarchar(200) SET @jobname = N'ASPState' + '_Job_DeleteExpiredSessions' -- Delete the [local] job -- We expected to get an error if the job doesn't exist. PRINT 'If the job does not exist, an error from msdb.dbo.sp_delete_job is expected.' EXECUTE msdb.dbo.sp_delete_job @job_name = @jobname GO DECLARE @sstype nvarchar(128) SET @sstype = N'sstype_temp' IF UPPER(@sstype) = 'SSTYPE_TEMP' AND OBJECT_ID(N'dbo.ASPState_Startup', 'P') IS NOT NULL BEGIN DROP PROCEDURE dbo.ASPState_Startup END USE [tempdb] GO IF OBJECT_ID(N'dbo.ASPStateTempSessions','U') IS NOT NULL BEGIN DROP TABLE dbo.ASPStateTempSessions END IF OBJECT_ID(N'dbo.ASPStateTempApplications','U') IS NOT NULL BEGIN DROP TABLE dbo.ASPStateTempApplications END USE [ASPState] GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'GetMajorVersion') AND (type = 'P'))) DROP PROCEDURE [dbo].GetMajorVersion GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'CreateTempTables') AND (type = 'P'))) DROP PROCEDURE [dbo].CreateTempTables GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempGetVersion') AND (type = 'P'))) DROP PROCEDURE [dbo].TempGetVersion GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'GetHashCode') AND (type = 'P'))) DROP PROCEDURE [dbo].GetHashCode GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempGetAppID') AND (type = 'P'))) DROP PROCEDURE [dbo].TempGetAppID GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempGetStateItem') AND (type = 'P'))) DROP PROCEDURE [dbo].TempGetStateItem GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempGetStateItem2') AND (type = 'P'))) DROP PROCEDURE [dbo].TempGetStateItem2 GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempGetStateItem3') AND (type = 'P'))) DROP PROCEDURE [dbo].TempGetStateItem3 GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempGetStateItemExclusive') AND (type = 'P'))) DROP PROCEDURE [dbo].TempGetStateItemExclusive GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempGetStateItemExclusive2') AND (type = 'P'))) DROP PROCEDURE [dbo].TempGetStateItemExclusive2 GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempGetStateItemExclusive3') AND (type = 'P'))) DROP PROCEDURE [dbo].TempGetStateItemExclusive3 GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempReleaseStateItemExclusive') AND (type = 'P'))) DROP PROCEDURE [dbo].TempReleaseStateItemExclusive GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempInsertUninitializedItem') AND (type = 'P'))) DROP PROCEDURE [dbo].TempInsertUninitializedItem GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempInsertStateItemShort') AND (type = 'P'))) DROP PROCEDURE [dbo].TempInsertStateItemShort GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempInsertStateItemLong') AND (type = 'P'))) DROP PROCEDURE [dbo].TempInsertStateItemLong GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempUpdateStateItemShort') AND (type = 'P'))) DROP PROCEDURE [dbo].TempUpdateStateItemShort GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempUpdateStateItemShortNullLong') AND (type = 'P'))) DROP PROCEDURE [dbo].TempUpdateStateItemShortNullLong GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempUpdateStateItemLong') AND (type = 'P'))) DROP PROCEDURE [dbo].TempUpdateStateItemLong GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempUpdateStateItemLongNullShort') AND (type = 'P'))) DROP PROCEDURE [dbo].TempUpdateStateItemLongNullShort GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempRemoveStateItem') AND (type = 'P'))) DROP PROCEDURE [dbo].TempRemoveStateItem GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'TempResetTimeout') AND (type = 'P'))) DROP PROCEDURE [dbo].TempResetTimeout GO IF (EXISTS (SELECT name FROM sysobjects WHERE (name = N'DeleteExpiredSessions') AND (type = 'P'))) DROP PROCEDURE [dbo].DeleteExpiredSessions GO IF EXISTS(SELECT name FROM systypes WHERE name ='tSessionId') EXECUTE sp_droptype tSessionId GO IF EXISTS(SELECT name FROM systypes WHERE name ='tAppName') EXECUTE sp_droptype tAppName GO IF EXISTS(SELECT name FROM systypes WHERE name ='tSessionItemShort') EXECUTE sp_droptype tSessionItemShort GO IF EXISTS(SELECT name FROM systypes WHERE name ='tSessionItemLong') EXECUTE sp_droptype tSessionItemLong GO IF EXISTS(SELECT name FROM systypes WHERE name ='tTextPtr') EXECUTE sp_droptype tTextPtr GO /*****************************************************************************/ CREATE PROCEDURE dbo.GetMajorVersion @@ver int OUTPUT AS BEGIN DECLARE @version nchar(100) DECLARE @dot int DECLARE @hyphen int DECLARE @SqlToExec nchar(4000) SELECT @@ver = 7 SELECT @version = @@Version SELECT @hyphen = CHARINDEX(N' - ', @version) IF (NOT(@hyphen IS NULL) AND @hyphen > 0) BEGIN SELECT @hyphen = @hyphen + 3 SELECT @dot = CHARINDEX(N'.', @version, @hyphen) IF (NOT(@dot IS NULL) AND @dot > @hyphen) BEGIN SELECT @version = SUBSTRING(@version, @hyphen, @dot - @hyphen) SELECT @@ver = CONVERT(int, @version) END END END GO /*****************************************************************************/ USE [ASPState] /* Find out the version */ DECLARE @ver int EXEC dbo.GetMajorVersion @@ver=@ver OUTPUT DECLARE @cmd nchar(4000) IF (@ver >= 8) SET @cmd = N' CREATE PROCEDURE dbo.CreateTempTables AS CREATE TABLE [tempdb].dbo.ASPStateTempSessions ( SessionId nvarchar(88) NOT NULL PRIMARY KEY, Created datetime NOT NULL DEFAULT GETUTCDATE(), Expires datetime NOT NULL, LockDate datetime NOT NULL, LockDateLocal datetime NOT NULL, LockCookie int NOT NULL, Timeout int NOT NULL, Locked bit NOT NULL, SessionItemShort VARBINARY(7000) NULL, SessionItemLong image NULL, Flags int NOT NULL DEFAULT 0, ) CREATE NONCLUSTERED INDEX Index_Expires ON [tempdb].dbo.ASPStateTempSessions(Expires) CREATE TABLE [tempdb].dbo.ASPStateTempApplications ( AppId int NOT NULL PRIMARY KEY, AppName char(280) NOT NULL, ) CREATE NONCLUSTERED INDEX Index_AppName ON [tempdb].dbo.ASPStateTempApplications(AppName) RETURN 0' ELSE SET @cmd = N' CREATE PROCEDURE dbo.CreateTempTables AS CREATE TABLE [tempdb].dbo.ASPStateTempSessions ( SessionId nvarchar(88) NOT NULL PRIMARY KEY, Created datetime NOT NULL DEFAULT GETDATE(), Expires datetime NOT NULL, LockDate datetime NOT NULL, LockCookie int NOT NULL, Timeout int NOT NULL, Locked bit NOT NULL, SessionItemShort VARBINARY(7000) NULL, SessionItemLong image NULL, Flags int NOT NULL DEFAULT 0, ) CREATE NONCLUSTERED INDEX Index_Expires ON [tempdb].dbo.ASPStateTempSessions(Expires) CREATE TABLE [tempdb].dbo.ASPStateTempApplications ( AppId int NOT NULL PRIMARY KEY, AppName char(280) NOT NULL, ) CREATE NONCLUSTERED INDEX Index_AppName ON [tempdb].dbo.ASPStateTempApplications(AppName) RETURN 0' EXEC (@cmd) GO /*****************************************************************************/ EXECUTE sp_addtype tSessionId, 'nvarchar(88)', 'NOT NULL' GO EXECUTE sp_addtype tAppName, 'varchar(280)', 'NOT NULL' GO EXECUTE sp_addtype tSessionItemShort, 'varbinary(7000)' GO EXECUTE sp_addtype tSessionItemLong, 'image' GO EXECUTE sp_addtype tTextPtr, 'varbinary(16)' GO /*****************************************************************************/ CREATE PROCEDURE dbo.TempGetVersion @ver char(10) OUTPUT AS SELECT @ver = "2" RETURN 0 GO /*****************************************************************************/ CREATE PROCEDURE dbo.GetHashCode @input tAppName, @hash int OUTPUT AS /* This sproc is based on this C# hash function: int GetHashCode(string s) { int hash = 5381; int len = s.Length; for (int i = 0; i < len; i++) { int c = Convert.ToInt32(s[i]); hash = ((hash << 5) + hash) ^ c; } return hash; } However, SQL 7 doesn't provide a 32-bit integer type that allows rollover of bits, we have to divide our 32bit integer into the upper and lower 16 bits to do our calculation. */ DECLARE @hi_16bit int DECLARE @lo_16bit int DECLARE @hi_t int DECLARE @lo_t int DECLARE @len int DECLARE @i int DECLARE @c int DECLARE @carry int SET @hi_16bit = 0 SET @lo_16bit = 5381 SET @len = DATALENGTH(@input) SET @i = 1 WHILE (@i <= @len) BEGIN SET @c = ASCII(SUBSTRING(@input, @i, 1)) /* Formula: hash = ((hash << 5) + hash) ^ c */ /* hash << 5 */ SET @hi_t = @hi_16bit * 32 /* high 16bits << 5 */ SET @hi_t = @hi_t & 0xFFFF /* zero out overflow */ SET @lo_t = @lo_16bit * 32 /* low 16bits << 5 */ SET @carry = @lo_16bit & 0x1F0000 /* move low 16bits carryover to hi 16bits */ SET @carry = @carry / 0x10000 /* >> 16 */ SET @hi_t = @hi_t + @carry SET @hi_t = @hi_t & 0xFFFF /* zero out overflow */ /* + hash */ SET @lo_16bit = @lo_16bit + @lo_t SET @hi_16bit = @hi_16bit + @hi_t + (@lo_16bit / 0x10000) /* delay clearing the overflow */ /* ^c */ SET @lo_16bit = @lo_16bit ^ @c /* Now clear the overflow bits */ SET @hi_16bit = @hi_16bit & 0xFFFF SET @lo_16bit = @lo_16bit & 0xFFFF SET @i = @i + 1 END /* Do a sign extension of the hi-16bit if needed */ IF (@hi_16bit & 0x8000 <> 0) SET @hi_16bit = 0xFFFF0000 | @hi_16bit /* Merge hi and lo 16bit back together */ SET @hi_16bit = @hi_16bit * 0x10000 /* << 16 */ SET @hash = @hi_16bit | @lo_16bit RETURN 0 GO /*****************************************************************************/ DECLARE @cmd nchar(4000) SET @cmd = N' CREATE PROCEDURE dbo.TempGetAppID @appName tAppName, @appId int OUTPUT AS SET @appName = LOWER(@appName) SET @appId = NULL SELECT @appId = AppId FROM [tempdb].dbo.ASPStateTempApplications WHERE AppName = @appName IF @appId IS NULL BEGIN BEGIN TRAN SELECT @appId = AppId FROM [tempdb].dbo.ASPStateTempApplications WITH (TABLOCKX) WHERE AppName = @appName IF @appId IS NULL BEGIN EXEC GetHashCode @appName, @appId OUTPUT INSERT [tempdb].dbo.ASPStateTempApplications VALUES (@appId, @appName) IF @@ERROR = 2627 BEGIN DECLARE @dupApp tAppName SELECT @dupApp = RTRIM(AppName) FROM [tempdb].dbo.ASPStateTempApplications WHERE AppId = @appId RAISERROR(''SQL session state fatal error: hash-code collision between applications ''''%s'''' and ''''%s''''. Please rename the 1st application to resolve the problem.'', 18, 1, @appName, @dupApp) END END COMMIT END RETURN 0' EXEC(@cmd) GO /*****************************************************************************/ /* Find out the version */ DECLARE @ver int EXEC dbo.GetMajorVersion @@ver=@ver OUTPUT DECLARE @cmd nchar(4000) IF (@ver >= 8) SET @cmd = N' CREATE PROCEDURE dbo.TempGetStateItem @id tSessionId, @itemShort tSessionItemShort OUTPUT, @locked bit OUTPUT, @lockDate datetime OUTPUT, @lockCookie int OUTPUT AS DECLARE @textptr AS tTextPtr DECLARE @length AS int DECLARE @now AS datetime SET @now = GETUTCDATE() UPDATE [tempdb].dbo.ASPStateTempSessions SET Expires = DATEADD(n, Timeout, @now), @locked = Locked, @lockDate = LockDateLocal, @lockCookie = LockCookie, @itemShort = CASE @locked WHEN 0 THEN SessionItemShort ELSE NULL END, @textptr = CASE @locked WHEN 0 THEN TEXTPTR(SessionItemLong) ELSE NULL END, @length = CASE @locked WHEN 0 THEN DATALENGTH(SessionItemLong) ELSE NULL END WHERE SessionId = @id IF @length IS NOT NULL BEGIN READTEXT [tempdb].dbo.ASPStateTempSessions.SessionItemLong @textptr 0 @length END RETURN 0' ELSE SET @cmd = N' CREATE PROCEDURE dbo.TempGetStateItem @id tSessionId, @itemShort tSessionItemShort OUTPUT, @locked bit OUTPUT, @lockDate datetime OUTPUT, @lockCookie int OUTPUT AS DECLARE @textptr AS tTextPtr DECLARE @length AS int DECLARE @now AS datetime SET @now = GETDATE() UPDATE [tempdb].dbo.ASPStateTempSessions SET Expires = DATEADD(n, Timeout, @now), @locked = Locked, @lockDate = LockDate, @lockCookie = LockCookie, @itemShort = CASE @locked WHEN 0 THEN SessionItemShort ELSE NULL END, @textptr = CASE @locked WHEN 0 THEN TEXTPTR(SessionItemLong) ELSE NULL END, @length = CASE @locked WHEN 0 THEN DATALENGTH(SessionItemLong) ELSE NULL END WHERE SessionId = @id IF @length IS NOT NULL BEGIN READTEXT [tempdb].dbo.ASPStateTempSessions.SessionItemLong @textptr 0 @length END RETURN 0' EXEC (@cmd) GO /*****************************************************************************/ DECLARE @ver int EXEC dbo.GetMajorVersion @@ver=@ver OUTPUT DECLARE @cmd nchar(4000) IF (@ver >= 8) SET @cmd = N' CREATE PROCEDURE dbo.TempGetStateItem2 @id tSessionId, @itemShort tSessionItemShort OUTPUT, @locked bit OUTPUT, @lockAge int OUTPUT, @lockCookie int OUTPUT AS DECLARE @textptr AS tTextPtr DECLARE @length AS int DECLARE @now AS datetime SET @now = GETUTCDATE() UPDATE [tempdb].dbo.ASPStateTempSessions SET Expires = DATEADD(n, Timeout, @now), @locked = Locked, @lockAge = DATEDIFF(second, LockDate, @now), @lockCookie = LockCookie, @itemShort = CASE @locked WHEN 0 THEN SessionItemShort ELSE NULL END, @textptr = CASE @locked WHEN 0 THEN TEXTPTR(SessionItemLong) ELSE NULL END, @length = CASE @locked WHEN 0 THEN DATALENGTH(SessionItemLong) ELSE NULL END WHERE SessionId = @id IF @length IS NOT NULL BEGIN READTEXT [tempdb].dbo.ASPStateTempSessions.SessionItemLong @textptr 0 @length END RETURN 0' EXEC (@cmd) GO /*****************************************************************************/ /* Find out the version */ DECLARE @ver int EXEC dbo.GetMajorVersion @@ver=@ver OUTPUT DECLARE @cmd nchar(4000) IF (@ver >= 8) SET @cmd = N' CREATE PROCEDURE dbo.TempGetStateItem3 @id tSessionId, @itemShort tSessionItemShort OUTPUT, @locked bit OUTPUT, @lockAge int OUTPUT, @lockCookie int OUTPUT, @actionFlags int OUTPUT AS DECLARE @textptr AS tTextPtr DECLARE @length AS int DECLARE @now AS datetime SET @now = GETUTCDATE() UPDATE [tempdb].dbo.ASPStateTempSessions SET Expires = DATEADD(n, Timeout, @now), @locked = Locked, @lockAge = DATEDIFF(second, LockDate, @now), @lockCookie = LockCookie, @itemShort = CASE @locked WHEN 0 THEN SessionItemShort ELSE NULL END, @textptr = CASE @locked WHEN 0 THEN TEXTPTR(SessionItemLong) ELSE NULL END, @length = CASE @locked WHEN 0 THEN DATALENGTH(SessionItemLong) ELSE NULL END, /* If the Uninitialized flag (0x1) if it is set, remove it and return InitializeItem (0x1) in actionFlags */ Flags = CASE WHEN (Flags & 1) <> 0 THEN (Flags & ~1) ELSE Flags END, @actionFlags = CASE WHEN (Flags & 1) <> 0 THEN 1 ELSE 0 END WHERE SessionId = @id IF @length IS NOT NULL BEGIN READTEXT [tempdb].dbo.ASPStateTempSessions.SessionItemLong @textptr 0 @length END RETURN 0' ELSE SET @cmd = N' CREATE PROCEDURE dbo.TempGetStateItem3 @id tSessionId, @itemShort tSessionItemShort OUTPUT, @locked bit OUTPUT, @lockDate datetime OUTPUT, @lockCookie int OUTPUT, @actionFlags int OUTPUT AS DECLARE @textptr AS tTextPtr DECLARE @length AS int DECLARE @now AS datetime SET @now = GETDATE() UPDATE [tempdb].dbo.ASPStateTempSessions SET Expires = DATEADD(n, Timeout, @now), @locked = Locked, @lockDate = LockDate, @lockCookie = LockCookie, @itemShort = CASE @locked WHEN 0 THEN SessionItemShort ELSE NULL END, @textptr = CASE @locked WHEN 0 THEN TEXTPTR(SessionItemLong) ELSE NULL END, @length = CASE @locked WHEN 0 THEN DATALENGTH(SessionItemLong) ELSE NULL END, /* If the Uninitialized flag (0x1) if it is set, remove it and return InitializeItem (0x1) in actionFlags */ Flags = CASE WHEN (Flags & 1) <> 0 THEN (Flags & ~1) ELSE Flags END, @actionFlags = CASE WHEN (Flags & 1) <> 0 THEN 1 ELSE 0 END WHERE SessionId = @id IF @length IS NOT NULL BEGIN READTEXT [tempdb].dbo.ASPStateTempSessions.SessionItemLong @textptr 0 @length END RETURN 0' EXEC (@cmd) GO /*****************************************************************************/ DECLARE @ver int EXEC dbo.GetMajorVersion @@ver=@ver OUTPUT DECLARE @cmd nchar(4000) IF (@ver >= 8) SET @cmd = N' CREATE PROCEDURE dbo.TempGetStateItemExclusive @id tSessionId, @itemShort tSessionItemShort OUTPUT, @locked bit OUTPUT, @lockDate datetime OUTPUT, @lockCookie int OUTPUT AS DECLARE @textptr AS tTextPtr DECLARE @length AS int DECLARE @now AS datetime DECLARE @nowLocal AS datetime SET @now = GETUTCDATE() SET @nowLocal = GETDATE() UPDATE [tempdb].dbo.ASPStateTempSessions SET Expires = DATEADD(n, Timeout, @now), LockDate = CASE Locked WHEN 0 THEN @now &nbs 相关文章精彩推荐 |