I am just in a stage like Alice in Wonderland, and not yet got out of the wonders of the .NET framework, C# and the VS 2003(5) IDE. I thought that the serialization is all not my thing until I do something big in C#. I had written this custom exception class in my project that has 3 processes connected by .NET Remoting Infrastructure. I throw my custom exception for a scenario but all I got was some other exception that said "The constructor to deserialize an object of type MyException was not found". But I had the Serializable attribute tagged to my custom expection class.
Let me to get to the point. Even though you attach the Serializable attribute to your custom exception class, the base class Exception implements the ISerializable interface and the constructor required during the deserialization [Exception()] is protected. So when you throw MyException, it may get serialized and cross the remoting boundaries but on the client side, it will not able to deserialize because the required ctor is not accessible. So what we do is simple as shown in the following code:-
If any custom data needs to be transferred with the MyException thown, it must be added to the SerializationInfo object on the call to GetObjectData() and to take them from this object in this secondary [deserialization] constructor.
Let me to get to the point. Even though you attach the Serializable attribute to your custom exception class, the base class Exception implements the ISerializable interface and the constructor required during the deserialization [Exception()] is protected. So when you throw MyException, it may get serialized and cross the remoting boundaries but on the client side, it will not able to deserialize because the required ctor is not accessible. So what we do is simple as shown in the following code:-
[Serializable]
public class MyException : ApplicationException
{
// Member Data
// Other necessary ctors
///
/// Let us call this ctor as Deserializing Ctor [DSCTOR]
///
public MyException(SerializationInfo info,
StreamingContext context)
: base(info, context)
{
// Any other custom data to be transferred
// info.AddValue(CustomDataName, DataValue);
}
///
/// If any custom information needs to be transferred
/// with the MyException thown, it must be added to the
/// SerializationInfo object on the call to
/// GetObjectData() and to take them from this object in
/// the constructor for deserialization.
///
public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
base.GetObjectData(info, context);
// Any other custom data to be transferred
// info.AddValue(CustomDataName, DataValue);
}
// Other methods
}
This DSCTOR is very much necessary, else the exception thrown will not be caught as the one thrown and you will get this excepetion "The constructor to deserialize an object of type MyException was not found".If any custom data needs to be transferred with the MyException thown, it must be added to the SerializationInfo object on the call to GetObjectData() and to take them from this object in this secondary [deserialization] constructor.
Comments