Session丢失,真让人郁闷

本文探讨了ASP.NET中Session丢失的问题及原因,包括应用程序重启的各种触发条件,并提供了避免Session丢失的方法。
1.网站发布后发现登录后Session丢失的问题,试了N多种方法,最后发现是配置文件的问题,出现了跨域访问的问题,从而导致Session丢失.
顺便记录下,做为以后参考.

2.
如果你发现session突然无理由的丢失,而当你认真的检查代码并且排除其它常规可能丢失
的可能性时,你可能会像我一样,几乎要接近崩溃~!
后来发现问题出在我用代码创建一个临时目录然后删除临时目录的 删除操作上。

现在和大家分享下:希望对碰到类似钉子的朋友有用。。

说白了session丢失实质就是:应用程序重起!

应用程序什么时候自动重起呢?

请参考:Anand在dotnetindia.com发表的文章

ContractedBlock.gif ExpandedBlockStart.gif 原文引用
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gifWhy ASP.NET Restarts
?
InBlock.gifFound 
this link from one of the blogs I was browsing today. This is ASP.NET Forum post by Patrick Y. Ng of MS.
InBlock.gif
InBlock.gifThis talks about both 
1.0 and 1.1
InBlock.gif
InBlock.gifThis behaviour has been bugging lots of people. ASP.Net will restart its application 
for various reasons. All these reasons are legitimate, but the side effect is that the application will reset all its cache, application state, and InProc session states. 
InBlock.gifYou can detect the restart of application and worker process by 
using the performance monitor. The counters to monitor are "ASP.NET\Application Restarts" and "ASP.NET\Worker Process Restarts"
InBlock.gif
InBlock.gifFor worker process restart, ASP.NET will write an entry 
in the eventlog to explain why (logLevel in controls how often eventlog is written). 
InBlock.gif
InBlock.gifFor application restart, however, 
in v1 and v1.1 there is no easy way for a customer to figure out why a restart happened. 
InBlock.gif
InBlock.gifSo by listing all reasons 
for app restart, I hope I can help some customers to understand and avoid the restart. 
InBlock.gif
InBlock.gifFor v1 
InBlock.gif
------ 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifThings that causes application restart: 
InBlock.gif
InBlock.gif
- The max compilation limit is reached (look for numRecompilesBeforeApprestart in machine.config) 
InBlock.gif
InBlock.gif
- Physical application path of the web application has changed. 
InBlock.gif
InBlock.gif
- Change in global.asax or one of the included files 
InBlock.gif
InBlock.gif
- Change in machine.config 
InBlock.gif
InBlock.gif
- Change in web.config in the app root 
InBlock.gif
InBlock.gif
- Change inside bin directory or its subdirs 
InBlock.gif
InBlock.gif
- A directory is renamed to bin, or bin is created 
InBlock.gif
InBlock.gif
- Overwhelming change notifications – too many files are changed too fast in one of content directories – could happen iffor example, files are generated on the fly per request 
InBlock.gif
InBlock.gif
- Change in the code-access security policy file 
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
- The settings of various attributes in in machine.config affect the restart/shutdown of the worker process itself. However, if you use Windows 2003, and (by default) you are NOT using IIS5 isolation mode, is ignored. Instead, the settings in "Application Pools" in IIS manager is used. 
InBlock.gif
InBlock.gif
InBlock.gifFor v1.
1 
InBlock.gif
-------- 
InBlock.gifThe list 
for v1.1 is the same as v1, with the following new conditions: 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
- User code called HttpRuntime.UnloadAppDomain 
InBlock.gif
InBlock.gif
- Any directory under the web application folder is renamed 
ExpandedSubBlockEnd.gif}

InBlock.gifIIS 
6.0 
InBlock.gif
-------- 
InBlock.gifIf you
're using IIS 6.0, and you're not using IIS 5 compatible mode, then aspnet_wp.exe will be replaced by w3svc.exe. You may want to go to IIS Manager, go to Application Pools/DefaultAppPool, and see if any of the parameters on the Recycling and Performance tabs are causing the IIS worker process (w3svc.exe) to shutdown. 
ExpandedBlockEnd.gif}

None.gif
None.gif

也就是说 asp.net 会监视应用程序目录,一但有被监视
的动作发生(比如:修改了config、重命名目录等)应用程序就会自动重起
当然这时你的session一定丢失了。

如果避免不了这种操作(如:重命名目录);这里我们可以用cookie存信息或存到数据库去;

注意:除了以上列出的英文条件外。目录的删除操作一定丢失session。asp.net的内部机制对待目录有点像个守财奴,它死守着目录,你创建它不会管(往里加),一但创建他就会监视该目录,若你要删除或重命名它的(动它的目录),它就发生重起了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值