How can I verify that my ASP.NET session data can be serialized correctly?

I am trying to switch from saving session data "InProc" to "StateServer".

To do this, I marked the class group as [Serializable] and rewrote some classes that could not be serialized before, and marked some values ​​that should not be serialized as [NonSerialized].

Now my problem is that instead of getting a compile-time error, an exception, or any other symptom of a problem from the framework, I get sessions back where some of the stored values ​​change to zero, either in the session itself or inside the objects contained in the session.

Why is there no indication of an error?

What causes null values?

How can I determine if the serialization of the session was correct?

thank

+3
source share
1 answer

It looks like you need some unit tests to confirm that your serialization is working correctly.

[Serializable]
public class SomeClass {
    public string SomeValue1;
    public string SomeValue2;
}

class Program {
    static void Main(string[] args) {
        var value1 = new SomeClass() { SomeValue1 = "Hello", SomeValue2 = "World" };
        var ms = new MemoryStream();
        var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        formatter.Serialize(ms, value1);
        ms.Position = 0;
        var value2 = (SomeClass)formatter.Deserialize(ms);
        Debug.Assert(value1.SomeValue1 == value2.SomeValue1);
        Debug.Assert(value1.SomeValue2 == value2.SomeValue2);
    }
}
+3
source

Source: https://habr.com/ru/post/1788428/


All Articles