Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update asp net webforms or mvc application without losing session?

Here is my scenario. I publish my webforms or mvc application on iis 8 on windows server 2012. Then my customer requests something else and I have to update codebehind.

For example a new input on form or a new column on db. And I am using session variables for membership or shop cart processes.

When I copy the new bin file in server,(when I publish my app), the session dies. How can I solve this update problem? What shall I do to keep session alive? Because Visitors are losing their shopping carts.

Thanks for help.

like image 672
oneNiceFriend Avatar asked Jan 21 '16 12:01

oneNiceFriend


2 Answers

A number of things will cause IIS to drop a session:

  • Lots of files update and ASP.NET proactively recompiles for you.
  • The session times out naturally
  • Updating the web.config which would cause it to recycle
  • The site app pool recycles for another reason

It sounds like you are falling afoul of the first, and there's no way to prevent that behavior.

It sounds like you are using "In Process" Session, which is the default, which is why you are losing the baskets etc.

From MSDN:

InProc mode, which stores session state in memory on the Web server. This is the default.

The other options are:

StateServer mode, which stores session state in a separate process called the ASP.NET state service. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.

SQLServer mode stores session state in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm.

Custom mode, which enables you to specify a custom storage provider.

Switching session to any of these others would enable you to update, but migration isn't instant.

like image 76
NikolaiDante Avatar answered Sep 17 '22 22:09

NikolaiDante


When I copy the new bin file in server,(when I publish my app), the session dies. How can I solve this update problem? What shall I do to keep session alive?

By default, Session-State Mode is InProc. You want to use either StateServer or SQLServer.

In Windows Azure, we use Custom mode and store in Redis Cache.

Because Visitors are losing their shopping carts.

Even then, Session State is not reliable.

I normally create a Shopping Cart table in database. Then save the Id of the shopping cart inside cookie on the client browser.

In addition, if user logins, I also save the UserId in the same table, so user can still view his/her shopping cart at any computer.

For example, amazon.com has similar approach.

like image 25
Win Avatar answered Sep 17 '22 22:09

Win