I have a small class what I made thread safe with the [Synchronization] attribute, it also implements the ContextBoundObject interface. I’d like to make this class [Serializable]. The code compiles but I receive an
System.Runtime.Remoting.RemotingException: Remoting cannot find field '__identity' on type 'System.MarshalByRefObject'
exception. I knew that classes marked with [Synchronization] are accessed through some ‘remote’ proxy but I hoped the default serialization mechanism will work.
This is a small app, the class is just an in-memory sequence (integer id) generator so no real remoting is done. I guess I can synchronize by other means (lock keyword) but I’d like to know what is the standard solution for this problem.
Update: at the end I abandoned the [Synchronization] attribute in order to use the basic serialization.
Instead of just using the Serializable attribute, you can implement the ISerializable interface.
You can read more about it here: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx
Basically you have to implement one method – GetObjectData – and a constructor with a similar signature.