`

转:Session丢失原因

阅读更多
In-process Mode
In-process mode simply means using ASP.NET session state in a similar manner to classic ASP session state. That is, session state is managed in process and if the process is re-cycled, state is lost. Given the new settings that ASP.NET provides, you might wonder why you would ever use this mode. The reasoning is quite simple: performance. The performance of session state, e.g. the time it takes to read from and write to the session state dictionary, will be much faster when the memory read to and from is in process, as cross-process calls add overhead when data is marshaled back and forth or possibly read from SQL Server.
In-process mode is the default setting for ASP.NET. When this setting is used, the only other session config.web settings used are cookieless and timeout.
If we call SessionState.aspx, set a session state value, and stop and start the ASP.NET process (iisreset), the value set before the process was cycled will be lost.
原来做大型网站,服务器压力大的时候经常出现ASPX进程内Session丢失的问题。现在的公司主要开发应用系统,服务器压力不大,访问量也比较小,很长时间以来没有发生过Session丢失的问题。但是在一次给用户演示的时候发生了奇怪的现象。我经过仔细分析是Session丢失造成的。但是和公司很多技术人员交流,他们都觉得ASPX的Session没有过期的条件下怎么会丢失呢?!
上面摘的这段MSDN上的文章里面确实指出进程内Session在没有过期的情况下也可能会丢失。
由于进程内Session保存于ASPX应用程序域中。其生死是随着ASPX应用程序域生死而变的。当ASPX应用程序重新启动时,进程内Session就会丢失。
当IIS出发.NET对ASPX应用程序进行垃圾回收的时候,就会使ASPX应用程序重起,这一点ASPX应用程序和WinForm应用程序是不同的。这是因为.NET框架会重新创建一个ASPX应用程序副本来响应在垃圾回收过程中的WEB请求,以保证Web服务的连贯性。
而且在windows2003的IIS的应用程序池设置里面有一个设定项,“空闲超时”即在设定的时间内应用程序处于空闲状态的话就会中止这个应用程序以获得更多的资源。这里就隐藏着一个矛盾,因为应用程序中止肯定会造成进程内Session丢失,所以如果这个时间小于ASPX的Session过期时间,那就有可能发生Session未过期而丢失的现象。
关于ASPX进程内Sesion丢失,我在MSDN上面搜到了很多文章,但是没有一个能给出比较好的解决办法,一般的,实在没辙就只有使用进程外Session或SQLServerSession了。事实上如果只保存简单的数据,SQLServerSession也并不是很慢的。我曾经在一个日访问量在几十万的网站上使用SQLServerSession保存登录信息啥的,效果很好
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics