views:

281

answers:

3

I have this huge domain object(say parent) which contains other domain objects. It takes a lot of time to "create" this parent object by querying a DB (OK we are optimizing the DB). So we decided to cache it using memcached (with northscale to be specific)

So I have gone through my code and marked all the classes (I think) as [Serializable], but when I add it to the cache, I see a Serialization Exception getting thrown in my VS.net output window.

var cache = new NorthScaleClient("MyBucket");
cache.Store(StoreMode.Set, key, value);

This is the exception:

A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll

SO my guess is, I have not marked all classes as [Serializable]. I am not using any third party libraries and can mark any class as [Serializable], but how do I find out which class is failing when the cache is trying to serialize the object ?

Edit1: casperOne comments make me think. I was able to cache these domain object with Microsoft Cache Application Block without marking them [Serializable], but not with NorthScale memcached. It makes me think that there might be something to do with their implementation, but just out of curiosity, am still interested in finding where it fails when trying to add the object to memcached

+1  A: 

Go to menu Debug -> Exceptions and expand Common Language Runtime Exceptions, then select the appropriate namespace for a SerializationException (System.Runtime.Serialization) and check the break when exception is thrown for the SerializationException.

This way you can check the details of the thrown exception.

Also note that a first chance exception does not absolutely mean that there is a problem with the application code.

João Angelo
+1  A: 

A common omission with BinaryFormatter is events; if you have objects subscribed that are not serializable, bad things happen. Plus you probably don't mean to serialize the subscribers. You can mark these as [NonSerialized] or [field:NonSerialized].

If all else fails, give it the byte[] of your object (or if that fails, base64). That said, I would advise against BinaryFormatter here - it is brittle and takes more bandwidth than it needs to. I've had some success using protobuf-net instead (see here, but a different implementation) ; which is smaller, faster, and version safe. I might be able to help shim this for you? Or you can use the ISerializable hook from protobuf-net.

Marc Gravell
nice one. I also see that Jon has something similar.Looks like yours is much easier to use, by just specifying the transcoder. Will give it a shot. And I guess I don't even have to mark the classes as `[Serializable]` as long as they are marked as `[DataContract]`
ram
@Ram - note that with [DataContract] it will need explicit (and unique) Order on the `[DataMember]` - for example, `[DataMember(Order=1)]` ... `[DataMember(Order=2)]` etc.
Marc Gravell
Thanks Marc, will try that out
ram
+1  A: 

This is how I resolved it. NorthScale was not throwing any error when the serialization failed. I serialized my domain object with Binary Serialization and was able to find out which classes were failing (since they were not marked as [Serializable]). Fixed it and it worked

Seeing the results of protobuf-net I am thinking about switching my serializer too

ram