开发者

Serialization for memcached

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 thei开发者_JAVA技巧r implementation, but just out of curiosity, am still interested in finding where it fails when trying to add the object to memcached


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜