2

I have a class which I want to put session:

[Serializable]
public class Model
{
    private readonly List<Uri> uris;

    public Model()
    {
        uris = new List<Uri>();
    }


    public IEnumerable<Uri> Uris { get { return uris; } }
}

An instance of this class and some strings are added to Session. Sql SessionState is used.

My problem is that sometimes when I get the Model object from session, the uris field is null. Please be noticed that all other primitive type session items are still correct. I'm using ASP.NET 4.0 btw.

Could anyone please explain to me how this can happen?

Thank you in advance :-)

Thuan
  • 1,618
  • 1
  • 10
  • 21
  • It might be because the SessionState had expired. – Răzvan Flavius Panda Mar 16 '12 at 14:42
  • [This question](http://stackoverflow.com/questions/3500429/how-does-binaryformatter-deserialize-create-new-objects) says that when deserializing the `Model` constructor is not called, so `uris` should be created with whatever was inserted. So how are you creating a `Model` and then adding it to the session? Is there any way that the `uris` field could be null at the point of insertion? – Peter Monks Mar 16 '12 at 15:04
  • @Peter Monks: there is no magic about creating model objects: var obj = new Model(); There is no way, or at least I couldn't think of one, that the uris field could be null before it is inserted to session. – Thuan Mar 16 '12 at 15:21

1 Answers1

0

I knew what happened here. The real cause involves some additional conditions: 1. Code is obfuscated. 2. Error happens when new code (update) is deployed and old session is still inuse.

Everytime code is obfuscated, fields and properties get random fuzzy names. Therefore, when sessionstate is deserialized, their values are null.

Thuan
  • 1,618
  • 1
  • 10
  • 21