Yogesh Rajput
Yogesh Rajput

Reputation: 87

Why serialization is not required for InProc session mode

I am using the state service session provider for my mvc application and if I don't serialize the type which I am going to store in session, application throws an error that class must be marked as Serializable. But if I switch to InProc session mode, session works even if my class is not marked as Serializable. As per my understanding, even in case of InProc mode ASP.Net serializes the data before storing in session then why InProc mode works without using Serializable attribute.

Upvotes: 2

Views: 2379

Answers (2)

CodeCaster
CodeCaster

Reputation: 151674

InProc doesn't serialize.

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.

Upvotes: 3

Hans Kesting
Hans Kesting

Reputation: 39329

Using InProc sessions, the data is not "stored" anywhere, it just stays in memory. This way no serialization is needed. For the other methods the data does need to be written to somewhere (state server, sql server) and needs to be converted to a stream of bytes (and back again).

So if you use InProc and have a lot of sessions with lots of data, you may run out of server memory.

Upvotes: 7

Related Questions