2

I have the following session configuration in web.config

<sessionState mode="InProc" timeout="1440" cookieless="false">

However for some reason session is timing out. So all I want to do is if the session is expired then reload the saved session, if session serialization is possbile. Is it possible to save a Session object then reload it?

Kashif Khan
  • 685
  • 2
  • 11
  • 30

2 Answers2

1

Just increase the session timeout property. If you want more fine grained control of the Session itself, you can change the sessionState mode to use a state server or SQL server (https://msdn.microsoft.com/en-us/library/ms178586.aspx)

Jeff
  • 35,755
  • 15
  • 108
  • 220
  • the session time 1440 is for 24 hours, but the session time is expiring withing 15 mins. – Kashif Khan Nov 09 '15 at 13:07
  • also my question was whether we can serialize / deserialize session object. – Kashif Khan Nov 09 '15 at 13:12
  • The session itself should not be. As for the items, it depends on whether the items are serializable. But you can certainly enumerate the items in the session. – Jeff Nov 09 '15 at 13:39
  • 1
    Also, it can help to ask "is this really the problem I should be solving" – Jeff Nov 09 '15 at 13:39
  • 2
    @Kashif your session mode is `InProc`, that means that it is stored in the worker process' memory. If the worker process recycle, you will lose your session, no matter the timeout property. By default the process recycle after 20 minutes of inactivity, I think. – Johnny5 Nov 09 '15 at 14:12
  • @Johnny5 how can i prevent process recycle or to tell the CLR to look after the timeout property for recycling – Kashif Khan Nov 10 '15 at 06:10
  • @Jeff the problem here is i have limited hardware resources and limited privileges. Since the session is timing out and it takes time to navigate to debug a page especially if it has a lot of dependencies on other pages. So my requirement is to reduce time and skipping the dependencies for faster debugging. Any other suggestions/workarounds are also welcomed. – Kashif Khan Nov 10 '15 at 06:13
  • What you want is exactly what the link in the answer provides. If you put your sessions state in "SqlServer" or "StateServer" mode, it does what you ask for, i.e. serialize the session either in sql server or in a state server, and deserialize it when needed. You _do not_ want to do it yourself. Do not reinvent the wheel. – Johnny5 Nov 10 '15 at 13:18
  • @jeff the problem is when the organisation does not provide resources separately for a state server that is when where a alternative is looked for. – Kashif Khan Nov 12 '15 at 07:51
1

Johnny5's comment probably hit the nail on the head--your process will shut down after a period of inactivity and all InProc sessions will be lost. I don't think IISExpress under Visual Studio gives you a way to control this, so consider running under full-blown IIS during development and increase your app pool's "idle timeout" property to 1440.

But if you really are looking for a way to save a session, the Session_Start and Session_End events in Global.asax are one place to do this. HttpSessionState isn't serializable, so you'd need to either use a different serializer (as Ernesto suggested) or extract all of the session entries and put them into a different type of serializable collection. (The HttpApplication.Session property is read-only, so it'll probably be easiest to take the latter approach in any case since you can't just swap out full session instances..)

But be warned: you'll have problems with Session_End if you're using InProc--it's much too flaky for all but the most trivial uses. App pools regularly recycle themselves for any number of reasons (predefined intervals, high memory usage, inactivity, etc...) and you won't get the nice Session_End event prior to restart, so you'll lose your sessions without them being persisted.

That's why everyone tells you to use an out-of-process provider if you care the least bit about your sessions. The SQL Server and StateServer modes that ship with ASP.NET are the obvious choices, but if you're looking to do long term storage then you may run into a problem because they don't fire the Session_End event.

My employer (ScaleOut Software) has one of the rare out-of-process session providers that can fire Session_End. It's a commercial product, but it can be run for free on a single server if you only have basic needs and don't require all the scalability and fault tolerance that ScaleOut SessionServer can provide.

Mark Waterman
  • 961
  • 7
  • 16
  • 1
    can you provide the source of this knowledge like from any Microsoft blogs or community about this? – Rumi Nov 10 '15 at 06:22