views:

103

answers:

2

Problem: When caching an instance of a class and immediately getting it back out of cache, i get the object back (its not null), but all of its properties / fields are null or defaults.

    _cacheHelper.PutInCache("testModuleControlInfoOne", mci);
    //mci has populated fields

    var mciFromCacheOne = _cacheHelper.GetFromCache("testModuleControlInfoOne");
    //mciFromCacheOne now has null or default fields

So I suspect the way the object is structured is the problem and AbbFabric is not serializing the object properly for some reason.

When I use the following Serialization method however, I get the object back with all properties / fields as they were prior to serialization.

    public T SerializeThenDeserialize<T>(T o) where T : class
    {
            BinaryFormatter bf = new BinaryFormatter();

            using (MemoryStream ms = new MemoryStream())
            {
                bf.Serialize(ms, o);

                ms.Position = 0;

                return (T)bf.Deserialize(ms);
            }
    }

How can an object serialize and deserialize properly using the binary formatter and not do exactly the same thing via caching?

Has anyone encountered this or does anyone have any suggestions or tips on generally what to look out for?

A: 

Ok found it.

The object implemented IXmlSerializable so AppFabric used that instead of the regular serialization.

Running it through an XmlSerializer (instead of a BinaryFormatter) gives the same null fields as I was experiencing.

It seems the IXmlSerializable implementation has issues.

CRice
A: 

I believe when serializing to Xml (using IXmlSerializable), the private fields of an object are ignored, which may be why your object was incomplete upon retrieval.

Using Binary Serialization will insure the entire object (including references to other objects) are included.

ScottF
Even worse, its a class in a dll I can't alter with poor IXmlSerializable implementation. Do you know if AppFabric can be configured to ignore IXmlSerializable and always use binary serialization?
CRice