1
votes

When I store a value in a session and some exception occurs afterwards, the session state might not be persisted.

Session["MyKey"] = value;
throw new Exception("Test exception");

I found out that if I'm using InProc mode, the value gets persisted immediately, so if an exception occurs, it's stored anyway.

But if I switch to StateServer mode, the value will not get persisted if an exception occurs. I can write to the session, read it again from the session, but after an exception occurs, it's like all the changes I made to the session state in that request will be discarded (i.e. not persisted). And any future request will read the "old" state of the session.

At first I was thinking that it's related to the session cookie not being sent in response in case of the exception, but this behavior occurs for sessions that already exist and users already hold their identifiers. It also clearly differs from InProc to StateServer, while both of these approaches handle cookies the same way, it's just the persistance layer that is different.

How does the session state persistance work? At which point in the request lifecycle are the changes actually persisted to StateServer? Is it possible to force persisting of the session state, so it would be persisted even after an exception occurs?

1
Could be a duplicate (or very close) to stackoverflow.com/questions/9985499/…Wiktor Zychla

1 Answers

0
votes

The docs says

https://msdn.microsoft.com/en-us/library/system.web.httpapplication.releaserequeststate.aspx

HttpApplication.ReleaseRequestState Event. Occurs after ASP.NET finishes executing all request event handlers. This event causes state modules to save the current state data.