views:

144

answers:

5

I'm currently convering my ASP.NET v2 application to serialize/deserialize it's objects because I want to shift from inproc session state to stateserver. This is because my host, webhost4life, has a nasty tendency to recycle the worker process frequently thus causing session timeouts. Anyway... the question...

I'm trying to not serialize things I don't need to, i.e. variables that are re-initialised each page, don't need to be serialised. Here's one of them:

Private RollbackQueue As New Queue(Of DataServer.Rollback)

On deserialisation, will RollbackQueue be a) nothing or b) an empty queue? My guess is that when .NET deserialises, it creates the parent object as normal and then fills in the fields one by one. Therefore, the NEW bit will fire.

But that is a guess.

Thanks, Rob.

+2  A: 

It will be nothing. The CLR serialization logic will create the object uninitialized by way of FormatterServices.GetSafeUnitializedObject without running any construction logic. If you need to ensure the field has a value I would recommend moving such initialization into an Initialize() method that is called both from your constructor and from a method marked with the OnDeserialized attribute.

HTH, Kent

Kent Boogaart
A: 

Is the "New" here classed as construction?

Cheers, Rob.

Rob Nicholson
Yes, the compiler will place that code in your type's constructor.
Kent Boogaart
A: 

BTW, I did include NonSerialized() in my original post but stackoverflow didn't show it. Was it because of the "<" and ">" characters?

Cheers, Rob.

Rob Nicholson
Yes, had the same problem when trying to wrap my 'OnDeserialized' in "<" and ">"
Kent Boogaart
If you click the button with the 'ones and zeros' pattern while highlighting the code, it will mark it as a code block and not remove things like angle bracket characters.
Greg Beech
A: 

Why not write a simple test application to find out? Here's one I wrote (excuse the C# instead of VB, but I have the C# Express version of VS2008 open at the moment).

[Serializable]
class TestClass
{
    [NonSerialized]
    public Queue<string> queue = new Queue<string>();
}

class Program
{
    static void Main(string[] args)
    {
        var obj = new TestClass();
        Console.WriteLine("Original is null? {0}", obj.queue == null);
        var stream = new MemoryStream();
        var formatter = new BinaryFormatter();
        formatter.Serialize(stream, obj);
        stream.Position = 0L;
        var copy = (TestClass)formatter.Deserialize(stream);
        Console.WriteLine("Copy is null? {0}", copy.queue == null);
        Console.ReadLine();
    }
}

The output from this is

Original is null? False 
Copy is null? True

Now you know for sure, that it will be null when deserialized. Kent has already explained in another post why this is the case, and what you can do about it, so I won't re-state it.

Greg Beech
A: 

Why not write a simple test application to find out?

Because sometimes it's easier to ask :-) I was working here with the ASP.NET session state so to be honest, I wasn't familiar with this standalone way of serialising. But I am now.

Thanks, Rob.

Rob Nicholson