为什么 InProc 会话模式不需要序列化

Why serialization is not required for InProc session mode

我正在为我的 mvc 应用程序使用 state service session provider,如果我不序列化我要在会话中存储的类型,应用程序会抛出一个错误,必须将 class 标记为可序列化。但是,如果我切换到 InProc 会话模式,即使我的 class 未标记为可序列化,会话也能正常工作。根据我的理解,即使在 InProc 模式下,ASP.Net 也会在存储到会话之前序列化数据,那么为什么 InProc 模式在不使用 Serializable 属性的情况下工作。

使用 InProc 会话,数据不会 "stored" 在任何地方,它只是留在内存中。这样就不需要序列化了。对于其他方法,数据确实需要写入某个地方(状态服务器,sql 服务器)并且需要转换为字节流(并再次返回)。

因此,如果您使用 InProc 并且有大量数据的大量会话,您可能 运行 服务器内存不足。

InProc 不序列化。

In-Proc Session State Management:

InProc state is considerably faster than either the state server or SQL Server session storage techniques. SQL Server and state server attribute their slowness to serialization/deserialization that happens while reading in/storing out the Session data from the SQL/State server.

InProc Mode in ASP.NET Session State:

Serialization/Deserialization is potential problem. InProc doesn't require serialization of complex objects. This is basically advantage, but in practice could be big problem when you least expect. Since InProc allows to put anything in session, you could end up with many non-serializable objects in session. If you need to switch to Session State or SQL Server later (e.g. your website became popular and now you have many visitors), you can't because stored classes are not serializable. Suddenly something that looks like advantage becomes a problem and you must change all problematic objects at once.