Error Serializing a CLR object for use in a WCF service
- by user208662
Hello,
I have written a custom exception object. The reason for this is I want to track additional information when an error occurs. My CLR object is defined as follows:
public class MyException : Exception
{
public override string StackTrace
{
get { return base.StackTrace; }
}
private readonly string stackTrace;
public override string Message
{
get { return base.Message; }
}
private readonly string message;
public string Element
{
get { return element; }
}
private readonly string element;
public string ErrorType
{
get { return errorType; }
}
private readonly string errorType;
public string Misc
{
get { return misc; }
}
private readonly string misc;
#endregion Properties
#region Constructors
public MyException()
{}
public MyException(string message) : base(message)
{ }
public MyException(string message, Exception inner) : base(message, inner)
{ }
public MyException(string message, string stackTrace) : base()
{
this.message = message;
this.stackTrace = stackTrace;
}
public MyException(string message, string stackTrace, string element, string errorType, string misc) : base()
{
this.message = message;
this.stackTrace = stackTrace;
this.element = element;
this.errorType = errorType;
this.misc = misc;
}
protected MyException(SerializationInfo info, StreamingContext context) : base(info, context)
{
element = info.GetString("element");
errorType = info.GetString("errorType");
misc = info.GetString("misc");
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("element", element);
info.AddValue("errorType", errorType);
info.AddValue("misc", misc);
}
}
I have created a copy of this custom xception in a WP7 application. The only difference is, I do not have the GetObjectData method defined or the constructor with SerializationInfo defined. If I run the application as is, I receive an error that says:
Type 'My.MyException' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute.
If I add the DataContract / DataMember attributes to the class and its appropriate members on the server-side, I receive an error that says:
Type cannot be ISerializable and have DataContractAttribute attribute.
How do I serialize MyException so that I can pass an instance of it to my WCF service. Please note, I want to use my service from an Android app. Because of this, I don't want to do anything too Microsoft centric. That was my fear with DataContract / DataMember stuff.
Thank you so much for your help!