丢失会话状态 [英] Losing Session State

查看:169
本文介绍了丢失会话状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.net应用程序,用户将无法成功完成某些动作,至于原因,我假设,只能与失去自己的会话(这是我维持目前的用户信息,并如何确定它们是否登录)

我不知所措,为什么他们会失去他们的会话,所以我的第一个问题是:

什么(一般)将导致用户失去在ASP.net他们的会话?

因为我当用户丢失他们的会话不知道也不能复制自己:

我如何当我失去用户跟踪他们的会话

下面是我的sessionState配置,以供参考。

 <的sessionState
           模式=是InProc
           无Cookie =假
           cookieName =My.Site.Com
           超时=480/>


解决方案

一个许多事情可能会导致会话状态神秘消失。


  1. 您的sessionState超时过期

  2. 您更新你的web.config或其他文件类型,导致你的AppDomain回收

  3. 您在IIS程序池回收

  4. 您有很多文件,更新你的网站,以及ASP.NET主动破坏你的AppDomain重新编译和preserve内存。

-

如果您使用的是IIS 7或7.5,这里有一些事情要查找:


  1. 默认情况下,IIS设置AppPools闲置一段时间后,把自己关闭。

  2. 默认情况下,IIS设置AppPools每1740分钟回收(显然这取决于你的根配置,但是这是默认设置)

  3. 在IIS中,检查你的程序池的高级设置。在有一个叫空闲超时属性。设置为零或大于默认值(20)一个较大的数字。

  4. 在IIS,请检查您的应用程序池的回收的设置。在这里,您可以启用或禁用回收你的程序池。在向导的第2页是记录到事件日志的方式每一种类型的应用程序池的关闭。

如果您使用的是IIS 6,同样的设置应用(大部分而是让他们不同的方式),但让他们记录的回收将是更多的是痛苦。这里是一个方式链接以获取IIS 6登录程序池回收事件:

<一个href=\"http://web.archive.org/web/20100803114054/http://surrealization.com/sample-$c$c/getnotifiedwhenapppoolrecycles/\">http://web.archive.org/web/20100803114054/http://surrealization.com/sample-$c$c/getnotifiedwhenapppoolrecycles/

-

如果你在你的web应用程序更新文件,你应该会丢失所有会话。这是野兽的只是性质。但是,您可能不希望它发生多次。如果更新15个或更多的文件(ASPX,DLL等),有一种情形产生,因为这些页面由用​​户访问该网站重新编译,你将有多个重新启动过一段时间。看到这两个链接:

http://support.microsoft.com/kb/319947

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.configuration.compilationsection.numrecompilesbeforeap$p$pstart.aspx\">http://msdn.microsoft.com/en-us/library/system.web.configuration.compilationsection.numrecompilesbeforeap$p$pstart.aspx

在numCompilesBeforeAp preSTART设置为一个较大的数字(或手动弹跳您的应用程序池)将消除这一问题。

-

您可以随时处理Application_SessionStart和Application_SessionEnd创建或结束会话时得到通知。该HttpSessionState类也有一个<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate_properties.aspx\">IsNewSession您可以检查任何页面请求属性,以确定是否为活动用户创建一个新的会话。

-

最后,如果它在你的情况有可能,我已经使用了 SQL Server会话模式具有良好的成功。它不建议,如果你存储大量的在里面的数据(每个请求的负载,并从SQL Server用于保存完整的数据量),如果你是把自定义对象在它,它可以是一个痛苦(因为它们是可序列化),但它帮助我在一个共享的主机场景,我无法配置我的程序池,以不回收几个小时。就我而言,我存储有限的信息,并没有任何不利的性能影响。添加到这样一个事实,即现有的用户将默认重复使用他们的SessionID,我的用户从来没有注意到的事实,它们在内存中的会话是由程序池回收丢弃,因为他们所有的状态被存储在SQL Server。

I have an ASP.net application where Users aren't able to successfully complete certain actions, for reasons, I'm assuming, can only be related to losing their session (which is where I maintain their current user information, and how determine whether they are logged in)

I'm at a loss as to why they would lose their session, so my first question is:

What (in general) would cause a user to lose their session in ASP.net?

and since I don't know when a user loses their session and can't reproduce it myself:

How can I track when I user loses their session

Below is my sessionState config for reference

<sessionState
           mode="InProc"
           cookieless="false"
           cookieName="My.Site.Com"
           timeout="480"/>

解决方案

A number of things can cause session state to mysteriously disappear.

  1. Your sessionState timeout has expired
  2. You update your web.config or other file type that causes your AppDomain to recycle
  3. Your AppPool in IIS recycles
  4. You update your site with a lot of files, and ASP.NET proactively destroys your AppDomain to recompile and preserve memory.

-

If you are using IIS 7 or 7.5, here are a few things to look for:

  1. By default, IIS sets AppPools to turn themselves off after a period of inactivity.
  2. By default, IIS sets AppPools to recycle every 1740 minutes (obviously depending on your root configuration, but that's the default)
  3. In IIS, check out the "Advanced Settings" of your AppPool. In there is a property called "Idle Time-out". Set that to zero or to a higher number than the default (20).
  4. In IIS, check the "Recycling" settings of your AppPool. Here you can enable or disable your AppPool from recycling. The 2nd page of the wizard is a way to log to the Event Log each type of AppPool shut down.

If you are using IIS 6, the same settings apply (for the most part but with different ways of getting to them), however getting them to log the recycles is more of a pain. Here is a link to a way to get IIS 6 to log AppPool recycle events:

http://web.archive.org/web/20100803114054/http://surrealization.com/sample-code/getnotifiedwhenapppoolrecycles/

-

If you are updating files on your web app, you should expect all session to be lost. That's just the nature of the beast. However, you might not expect it to happen multiple times. If you update 15 or more files (aspx, dll, etc), there is a likelyhood that you will have multiple restarts over a period of time as these pages are recompiled by users accessing the site. See these two links:

http://support.microsoft.com/kb/319947

http://msdn.microsoft.com/en-us/library/system.web.configuration.compilationsection.numrecompilesbeforeapprestart.aspx

Setting the numCompilesBeforeAppRestart to a higher number (or manually bouncing your AppPool) will eliminate this issue.

-

You can always handle Application_SessionStart and Application_SessionEnd to be notified when a session is created or ended. The HttpSessionState class also has an IsNewSession property you can check on any page request to determine if a new session is created for the active user.

-

Finally, if it's possible in your circumstance, I have used the SQL Server session mode with good success. It's not recommended if you are storing a large amount of data in it (every request loads and saves the full amount of data from SQL Server) and it can be a pain if you are putting custom objects in it (as they have to be serializable), but it has helped me in a shared hosting scenario where I couldn't configure my AppPool to not recycle couple hours. In my case, I stored limited information and it had no adverse performance effect. Add to this the fact that an existing user will reuse their SessionID by default and my users never noticed the fact that their in-memory Session was dropped by an AppPool recycle because all their state was stored in SQL Server.

这篇关于丢失会话状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆