Disappearing Sessions

M

Mark Rae

Hi,

I'm presently converting a v1.1 ASP.NET app to v2, and am encountering a
strange problem.

When running in Debug mode (i.e. hitting F5), all is well.

However, when running without debugging (i.e. hitting Ctrl-F5), every so
often (it seems to be totally random) the entire Session seems to get torn
down causing Session_Start to fire again, thereby losing any Session
variables which have been created. This, in turn, causes the standard
"Object reference not set to an instance of an object" error because the
Session object which the code is expecting is not there.

Also, strangely, the Error Source for the above errors is always slightly
different e.g.

App_Web_zsxmxts_
App_Web_oxwoppmb
App_Web_82ilyo2x
App_Web_5iwdsnnv
App_Web_5iwdsnnv
App_Web_sog3df_f

I found the following article: http://forums.asp.net/thread/1271201.aspx but
the MS HotFix it refers to doesn't seem to have anything do do with my
current problem.

Has anyone seen anything like this before? I must have converted a dozen or
so v1.1 apps to v2 by now, and I've never encountered anything remotely
similar...

Any assistance gratefully received.

Mark
 
G

Guest

Hi

The only thing I've seen similar is when a runtime error occurs in the
ASP.NET application, causing the application to restart. Are you receiving
Application_OnStart events too? Have you got anything that could be hiding an
error (e.g. an Application_Error handler)

Cheers

Wayne
 
M

Mark Rae

Wayne,
The only thing I've seen similar is when a runtime error occurs in the
ASP.NET application, causing the application to restart. Are you receiving
Application_OnStart events too? Have you got anything that could be hiding
an
error (e.g. an Application_Error handler)

That's interesting - I don't *think* I'm getting Application_OnStart events,
but what you're saying certainly sounds possible.

The app does indeed have an Application_Error handler, but it's a fairly
standard one from what I can see - as part of my testing the upgrade, I've
obviously deliberately forced an unhandled error, and the Application_Error
handler certainly does seem to handle it cleanly enough, i.e. redirects to
an error.aspx page, displays a friendly message to the user, and emails
details of the error to the webmaster...

However, I'll disable that temporarily and see what I find.

Thanks for the reply.

Mark
 
M

Mark Rae

However, I'll disable that temporarily and see what I find.

A trawl round Google turned up this site:
http://west-wind.com/weblog/posts/4057.aspx which mentions a "similar"
problem of Sessions disappearing when moving between http and https.
Initially, I thought I was onto something because the app I'm working on
does do that, but it uses Matt Sollars' excellent HttpModule for this
(http://www.codeproject.com/aspnet/WebPageSecurity_v2.asp), which works
perfectly inasmuch as I'm able to switch between secure and unsecure pages
without affecting the Session collection, so I'm pretty sure that's not the
problem. In fact, I even tried removing that HttpModule entirely from the
app, and the problem didn't go away...

So, I disabled the Application_Error code and, temporarily, even removed all
try..catch statements from the code. The problem (i.e. the disappearing
Session) still occurs, but I'm still no closer to finding out why. Thus far,
I can say:

1) the Session is getting torn down and rebuilt at random intervals

2) the Application is not getting recylced

3) the tearing down of the Session "appears" not to be being caused by an
unhandled exception

Incidentally, forgive my ignorance, but is there a "Break on all errors"
equivalent in VS.NET 2005, specifically for ASP.NET / C#...? In v1.1, there
was a Debug, Exceptions window where you could do something similar...
 
G

Guest

Hi Mark

Have you tried Debug->Exceptions, check the Thrown boxes.

To try and find the cause of the problem, I would add logging the session
and application events in global.asax. Session_Start, Session_End,
Application_Start, Application_End, Application_Error. I'd also put
breakpoints on them and run the pages in debug mode that way it might help to
narrow down when exactly the problem is occurring

Cheers

Wayne
 
M

Mark Rae

Have you tried Debug->Exceptions, check the Thrown boxes.

I would if I could... that opion doesn't appear under my Debug menu. FYI,
the options I have are:

Windows
--------------------
Continue
Break All
Stop debugging
Detach All
Terminate All
Restart
Attach to Process
--------------------
Step Over
--------------------
Toggle Breakpoint
Delete All Breakpoints
To try and find the cause of the problem, I would add logging the session
and application events in global.asax. Session_Start, Session_End,
Application_Start, Application_End, Application_Error.

I've done that.
I'd also put breakpoints on them and run the pages in debug mode that way
it might help to narrow down when exactly the problem is occurring

As I mentioned in my original post, the problem doesn't occur in debug mode.
 
M

Mark Rae

Wayne,
Have you tried Debug->Exceptions, check the Thrown boxes.

Yes - finally found that dialog (!) and have set it to break every time an
error is thrown. No errors appear to being thrown before the Session
disappears.
To try and find the cause of the problem, I would add logging the session
and application events in global.asax. Session_Start, Session_End,
Application_Start, Application_End, Application_Error.

Done that - all that has shown is that Sessio_End is being fired at random
moments - there appears to be neither pattern nor logic to it.
I'd also put breakpoints on them and run the pages in debug mode that way
it might help to
narrow down when exactly the problem is occurring

Again, done that - there is nothing to indicate why Session_End is being
fired. When the code reaches the breakpoint I've set in Session_End, the
Stack window displays slight variations on the following each time...
App_Code.jpjgc0sh.dll!Global.Session_End(object sender =
{System.Web.SessionState.SessionOnEndTarget}, System.EventArgs e =
{System.EventArgs}) Line 61 C#
[Frames below may be incorrect and/or missing, no symbols loaded for
mscorwks.dll]
System.Web.dll!System.Web.HttpApplication.ProcessSpecialRequest(System.Web.HttpContext
context = null, System.Reflection.MethodInfo method, int paramCount, object
eventSource, System.EventArgs eventArgs,
System.Web.SessionState.HttpSessionState session) + 0xe4 bytes
System.Web.dll!System.Web.HttpApplicationFactory.FireSessionOnEnd(System.Web.SessionState.HttpSessionState
session, object eventSource, System.EventArgs eventArgs) + 0xb9 bytes
System.Web.dll!System.Web.SessionState.SessionOnEndTarget.RaiseSessionOnEnd(string
id, System.Web.SessionState.SessionStateStoreData item) + 0x101 bytes
System.Web.dll!System.Web.SessionState.InProcSessionStateStore.OnCacheItemRemoved(string
key, object value, System.Web.Caching.CacheItemRemovedReason reason) + 0x120
bytes
System.Web.dll!System.Web.Caching.CacheEntry.CallCacheItemRemovedCallback(System.Web.Caching.CacheItemRemovedCallback
callback, System.Web.Caching.CacheItemRemovedReason reason) + 0xef bytes
System.Web.dll!System.Web.Caching.CacheEntry.Close(System.Web.Caching.CacheItemRemovedReason
reason) + 0x1c5 bytes
System.Web.dll!System.Web.Caching.CacheSingle.UpdateCache(System.Web.Caching.CacheKey
cacheKey, System.Web.Caching.CacheEntry newEntry = null, bool replace,
System.Web.Caching.CacheItemRemovedReason removedReason, out object
valueOld) + 0x98c bytes
System.Web.dll!System.Web.Caching.CacheInternal.DoRemove(System.Web.Caching.CacheKey
cacheKey, System.Web.Caching.CacheItemRemovedReason reason) + 0x18 bytes
System.Web.dll!System.Web.Caching.CacheSingle.Dispose(bool disposing =
true) + 0x198 bytes
System.Web.dll!System.Web.Caching.CacheMultiple.Dispose(bool disposing =
true) + 0x46 bytes
System.Web.dll!System.Web.HttpRuntime.Dispose() + 0xd5 bytes
System.Web.dll!System.Web.HttpRuntime.ReleaseResourcesAndUnloadAppDomain(object
state) + 0x6a bytes
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(object
state) + 0x2f bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext
executionContext, System.Threading.ContextCallback callback, object state) +
0x81 bytes
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(object
state) + 0x6c bytes
[Appdomain Transition]

I don't know enough about the internals of the Framework to glean very much
from this, but it is clear that something somewhere is causing
Global.Session_End to fire...

I'm really out of options now, so I'm thinking all I can do is to create a
new web app from the ground up and add the pages in one by one to see if
that fixes it...

Mark
 
H

Hans Kesting

Done that - all that has shown is that Sessio_End is being fired at random
moments - there appears to be neither pattern nor logic to it.

Did you check the SessionID for the session whose Session_End was fired
to see that it was *your* session ending?
The breakpoint will be hit on *any* execution of that code, so you
might be seeing the regular end of previous sessions (by default 20
minutes after the last request in that session)

A hint: if you change web.config (even if you add/remove just a space)
then the application will reset, so any previous sessions will be
cancelled.

Hans Kesting
 
M

Mark Rae

Did you check the SessionID for the session whose Session_End was fired
to see that it was *your* session ending?
Yes.

The breakpoint will be hit on *any* execution of that code, so you
might be seeing the regular end of previous sessions (by default 20
minutes after the last request in that session)

Fair comment, though even if it was another session ending (it wasn't) it
shouldn't tear down *my* session, right...?
A hint: if you change web.config (even if you add/remove just a space)
then the application will reset, so any previous sessions will be
cancelled.

I'm not changing web.config anywhere.
 
A

Alvin Bruney

Do you see any entries in the event log for application pool recycling? Or
anything out of place?

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------
 
H

Hans Kesting

Hans Kesting said:
Fair comment, though even if it was another session ending (it wasn't) it
shouldn't tear down *my* session, right...?

No, it shouldn't
I'm not changing web.config anywhere.

Sorry, the remark wasn't clear enough. What I meant was that by
recycling the application on purpose, you are sure that there are no
"other sessions" troubling you. (but that was not the case)

Hans Kesting
 
M

Mark Rae

Sorry, the remark wasn't clear enough. What I meant was that by recycling
the application on purpose, you are sure that there are no "other
sessions" troubling you. (but that was not the case)

Ah - I'm periodically running iisreset for precisely this purpose...
 
J

JDP

Has there been any resolution to why the sessions would just disappear, for no reason. I'm having this issue with a few client. It seems like for no reason the session is gone and the website dies.
 
N

NumbLock

JDP, Here is a post of mine from another thread. Hope this helps.
I've implemented code to detect timeouts now in an app that was giving
me problems. It seems to be working fine now. I added a redirect to a
"session expired" page which has a link to log back on (I have it
dropping the auth cookie when the session times out) I recommend
setting your auth timeout to a value at least twice as long as the
session timeout just to be safe. Here is my previous post.

I have seen this happen before. It could be that your session actually
is timing out. There is a session timeout and an authentication
timeout. The fact that you are not redirected to the login page does
not prove that your session has not timed out.

Here is a good site with info on detecting a session timeout:

http://aspalliance.com/520

reason is that you are actually removing it somewhere in your code.
Thanks for the confirmation.. looks like I'll have to debug a bit
further.. it certainly only happens very occasionally
 
J

Jose A. Fernandez

Session lost.
when..
- Various attributes (for example, the memoryLimit attribute) have
particular settings in the <processModel> section of the configuration
file.
- The Global.asax or the Web.config file was modified.
- The Bin directory of the Web application was modified.
- Virus scanning software touched some .config files


When you use F5 es WORLD PEFECT :)

(textual solution for msdn) ...
To work around this problem, you can use StateServer or SqlServer
session state mode. ASP.NET provides these other approaches for storing
session state data. In the StateServer and SqlServer modes, your
session state is not stored in the AppDomain of the Web application

It work.! and my solution

I implemented State Server Mode
To use State Server, you must first make sure ASP.NET state service is
running on the remote server used for the session store. This service
is installed with ASP.NET and Visual Studio .NET at the following
location:

systemroot\Microsoft.NET\Framework\versionNumber\aspnet_state.exe

Next, set the mode attribute of the <sessionState> element to
StateServer in the Web.config file of the application. Finally, set the
connectionString attribute to tcpip=serverName:portNumber.


Try this example with STATE SERVER MODE in your webapp



(Link)
Session Data Is Lost When You Use ASP.NET InProc Session State Mode
http://support.microsoft.com/default.aspx?scid=kb;en-us;324772

Session State
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconsessionstate.asp

Best Practices
Fast, Scalable, and Secure Session State Management for Your Web
Applications
http://msdn.microsoft.com/msdnmag/issues/05/09/SessionState/default.aspx

Why is my ASP.NET application restarting?
http://support.microsoft.com/kb/871042/en-us
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top