如何在不丢失会话的情况下更新 asp 网络表单或 mvc 应用程序?

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

这是我的场景。 我在 windows 服务器 2012 上的 iis 8 上发布我的 webforms 或 mvc 应用程序。 然后我的客户要求其他东西,我必须更新代码隐藏。

例如,表单上的新输入或数据库上的新列。我正在为会员或购物车流程使用会话变量。

当我在服务器中复制新的 bin 文件时(当我发布我的应用程序时),会话终止。我该如何解决这个更新问题? 我该怎么做才能使会话保持活动状态?因为访问者丢失了他们的购物车。

感谢您的帮助。

许多情况会导致 IIS 放弃会话:

  • 大量文件更新,ASP.NET 主动为您重新编译。
  • 会话自然超时
  • 更新 web.config 这会导致它 回收
  • 站点应用程序池因其他原因回收

听起来您好像与第一种行为发生了冲突,而且没有办法阻止这种行为。

听起来您正在使用 "In Process" Session,这是默认设置,这就是您丢失篮子等的原因。

来自MSDN

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

其他选项是:

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.

将会话切换到其他任何一个都可以让您更新,但迁移不是即时的。

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?

默认情况下,Session-State Mode 是 InProc。您想要使用 StateServer 或 SQLServer。

在 Windows Azure 中,我们使用自定义模式并存储在 Redis Cache 中。

Because Visitors are losing their shopping carts.

即便如此,会话状态也不可靠。

我通常在数据库中创建一个购物车table。然后将购物车的Id保存在客户端浏览器的cookie里面。

此外,如果用户登录,我也会将 UserId 保存在同一个 table 中,因此用户仍然可以在任何计算机上查看 his/her 购物车。

例如,amazon.com有类似的方法。