views:

78

answers:

2

While I realize there is a similar question (How to serialize an Exception object in C#?), and though the answers on that page were helpful, they didn't exactly solve the problem or answer the question posed.

I believe the question was how to serialize the object to allow it to be reconstructed (deserialized) into the same object. I've attempted to use the solution given by davogones and Antony Booth, but without adding the System.Exception base class on the consuming side (as in: SerializationException: Exception), it is impossible to use these types (by themselves) as actual exception objects that can be thrown.

Before I continue, let me explain that last statement. I've tried to use Antony Booth's solution in a web service (the service contains the definition for the serializable object) in an attempt to have all consumers use the same exception (hopefully creating a reusable serializable exception type instead of recreating it all over).

Unfortunately, since neither of the types explicitly derive from System.Exception, you cannot throw them, which would obviously be useful. Like I mentioned above, it does seem that adding : Exception to the type class definition on the consuming side allows the object to be thrown, but that requires editing auto-generated WSDL/web service code, which seems intuitively like a bad/non-maintainable practice to me (correct me if I'm wrong).

My first question is, again, is it possible to serialize System.Exception or to create a derived type that can be serialized, and if it is possible, how would one go about doing such? I should mention that I've looked at what seems the official way to reconstitute the Exception object, but I'm afraid I don't understand it very well.

My second question is about the architecture of System.Exception itself. What I would like to know is why the System.Exception type is marked as [Serializable] when it has been documented and apparently designed to disallow you from serializing it properly (at least with XML) because it's Data object implements IDictionary?

From MSDN:

Q: Why can't I serialize hashtables?

A: The XmlSerializer cannot process classes implementing the IDictionary interface. This was partly due to schedule constraints and partly due to the fact that a hashtable does not have a counterpart in the XSD type system. The only solution is to implement a custom hashtable that does not implement the IDictionary interface.

Given that XML is becoming (if not already is) a new standard for data transportation (officially recommended by Microsoft, nonetheless), it seems absurdly stupid to not allow the only object type in .NET that can be thrown to not be XML-serializable.

I look forward to hearing some thoughts from all the SO'rs (especially since this is my first post).

If you've got questions or need clarification, please don't hesitate to let me know.


Note: I just found this SO post, which seems to answer a few questions, but I guess I'd like to take my own whack at it. let me know if it's too close to a duplicate, though.

+1  A: 

You can create a class derived from Exception and do the serialization and deserialization yourself by implementing the ISerializable interface.

Example taken from wrox forums, subclassing ApplicationException:

EDIT: As pointed, ApplicationException is deprecated. Using the base Exception class should work just fine.

using System;
using System.Collections;
using System.Runtime.Serialization;

namespace Common.CustomExceptions
{

    /// <summary>
    /// Custom exception.
    /// </summary>
    [Serializable]
    public class CustomExceptionBase: ApplicationException
        {

        // Local private members
        protected DateTime _dateTime = DateTime.Now;
        protected String _machineName = Environment.MachineName;
        protected String _exceptionType = "";
        private String _exceptionDescription = "";
        protected String _stackTrace = "";
        protected String _assemblyName = "";
        protected String _messageName = "";
        protected String _messageId = "";
        protected Hashtable _data = null;
        protected String _source = "";
        protected Int32 _exceptionNumber = 0;

        public CustomExceptionBase(): base()
        {
            if (Environment.StackTrace != null)
                this._stackTrace = Environment.StackTrace;
        }

        public CustomExceptionBase(Int32 exceptionNumber): base()
        {
            this._exceptionNumber = exceptionNumber;
            if (Environment.StackTrace != null)
                this._stackTrace = Environment.StackTrace;
        }

        public CustomExceptionBase(Int32 exceptionNumber, String message): base(message)
        {
            this._exceptionNumber = exceptionNumber;
            if (Environment.StackTrace != null)
                this._stackTrace = Environment.StackTrace;
        }

        public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException): 
            base(message, innerException)
        {
            this._exceptionNumber = exceptionNumber;
            if (Environment.StackTrace != null)
                this._stackTrace = Environment.StackTrace;
        }

        public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException, String messageName, String mqMessageId): 
            base(message, innerException)
        {
            this._exceptionNumber = exceptionNumber;
            this._messageId = mqMessageId;
            this._messageName = messageName;
            if (Environment.StackTrace != null)
                this._stackTrace = Environment.StackTrace;
        }

        public CustomExceptionBase(Int32 exceptionNumber, String message, Exception innerException, String messageName, String mqMessageId, String source): 
            base(message, innerException)
        {
            this._exceptionNumber = exceptionNumber;
            this._messageId = mqMessageId;
            this._messageName = messageName;
            this._source = source.Equals("") ? this._source : source;
            if (Environment.StackTrace != null)
                this._stackTrace = Environment.StackTrace;
        }


        #region ISerializable members

        /// <summary>
        /// This CTor allows exceptions to be marhalled accross remoting boundaries
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        protected CustomExceptionBase(SerializationInfo info, StreamingContext context) :
            base(info,context)
        {
            this._dateTime = info.GetDateTime("_dateTime");
            this._machineName = info.GetString("_machineName");
            this._stackTrace = info.GetString("_stackTrace");
            this._exceptionType = info.GetString("_exceptionType");
            this._assemblyName = info.GetString("_assemblyName");
            this._messageName = info.GetString("_messageName");
            this._messageId = info.GetString("_messageId");
            this._exceptionDescription = info.GetString("_exceptionDescription");
            this._data = (Hashtable)info.GetValue("_data", Type.GetType("System.Collections.Hashtable"));
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("_dateTime", this._dateTime);
            info.AddValue("_machineName", this._machineName);
            info.AddValue("_stackTrace", this._stackTrace);
            info.AddValue("_exceptionType", this._exceptionType);
            info.AddValue("_assemblyName", this._assemblyName);
            info.AddValue("_messageName", this._messageName);
            info.AddValue("_messageId", this._messageId);
            info.AddValue("_exceptionDescription", this._exceptionDescription);
            info.AddValue("_data", this._data, Type.GetType("System.Collections.Hashtable"));
            base.GetObjectData (info, context);
        }

        #endregion
    }
}
axel_c
Just a quick note that `ApplicationException` has been deprecated and should _not_ be used or subclassed. Use `Exception` instead.
thecoop
Unfortunately, this doesn't seem to work in the context of web services. I copied your code and changed `ApplicationException` to `Exception`, and I got the same error code I was referring to before: Cannot serialize member System.Exception.Data of type System.Collections.IDictionary, because it implements IDictionary. Simply adding `[Serializable]` is not sufficient.
liquidrogue
Hrm. Maybe you can change the _data member to a serializable collection that does not implement IDictionary? There are some implementations out there that do this.
axel_c
A: 

Consider having two classes.

The first will be the serializable ExceptionDescription class, which must implement the attributes you want serialized, and inherits from nothing. The second is CustomException which will inherit from Exception, and have a single reference to an instance of ExceptionDescription. Also, CustomException will implement "public static implicit operator" so you can naturally move between the two implementations.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Xml.Serialization;

namespace SerializableException {


public class CustomException : Exception {


    public CustomException(ExceptionDescription d) {
        this.description = d;
    }//method


    public CustomException(String message, Exception e) {
        this.description = new ExceptionDescription(message, e, 2);
    }//method

    public CustomException(String message, Exception e, int stackDepth) {
        this.description = new ExceptionDescription(message, e, stackDepth + 1);
    }//method


    public CustomException(String message, IEnumerable<Exception> causes) {
        this.description = new ExceptionDescription(message, causes, 2);
    }//method


    public CustomException(String message, IEnumerable<Exception> causes, int stackDepth) {
        this.description = new ExceptionDescription(message, causes, stackDepth + 1);
    }//method


    public CustomException(String message) {
        this.description = new ExceptionDescription(message, 2);
    }//method


    public CustomException(String message, int stackDepth) {
        this.description = new ExceptionDescription(message, stackDepth + 1);
    }//method


    public CustomException() {
    }//method


    public static CustomException newInstance(Exception e) {
        if (e == null) return null;
        if (e is CustomException) return (CustomException)e;

        CustomException output = new CustomException();
        output.description = ExceptionDescription.newInstance(e);
        return output;
    }//method


    public static implicit operator ExceptionDescription(CustomException e) {
        if (e == null) return null;
        return e.description;
    }//method

    public static implicit operator CustomException(ExceptionDescription d) {
        return d == null ? null : new CustomException(d);
    }//method


    public ExceptionDescription description;



    public String RawStackTrace {
        get { return description.RawStackTrace; }
        //set { rawStackTrace = value; }
    }//method


    public DateTime Time {
        get { return description.Time; }
    }//method

    public override String Message {
        get { return description.Message; }
    }//method


}//class




[XmlRoot]
public class ExceptionDescription {

    public ExceptionDescription() {
    }//method


    public ExceptionDescription(String message, Exception cause, int stackDepth) {
        this.Message = message;
        this.Time = DateTime.Now;
        this.RawStackTrace = new StackTrace(1 + stackDepth, true).ToString();
        this.Causes = new ExceptionDescription[] { ExceptionDescription.newInstance(cause) };
    }//method



    public ExceptionDescription(String message, IEnumerable<Exception> causes, int stackDepth) {
        this.Message = message;
        this.Time = DateTime.Now;
        this.RawStackTrace = new StackTrace(1 + stackDepth, true).ToString();
        this.Causes = (from Exception e in causes select ExceptionDescription.newInstance(e)).ToArray();
    }//method


    public ExceptionDescription(String message, int stackDepth) {
        this.Message = message;
        this.Time = DateTime.Now;
        this.RawStackTrace = new StackTrace(stackDepth + 1, true).ToString();
        this.Causes = new ExceptionDescription[0];
    }//method



    public static ExceptionDescription newInstance(Exception e) {
        if (e == null) return null;
        if (e is CustomException) return ((CustomException)e).description;

        ExceptionDescription output = new ExceptionDescription();
        output.Time = DateTime.Now;
        output.Message = e.Message;
        output.RawStackTrace = e.StackTrace;

        if (e.InnerException != null) {
            output.Causes = new ExceptionDescription[] { ExceptionDescription.newInstance(e.InnerException) };
        } else {
            output.Causes = new ExceptionDescription[0];
        }//endif
        return output;
    }//method





    public String Message;
    public ExceptionDescription[] Causes;       //MORE THAN ONE CAUSE IS LEGITIMATE             
    public String RawStackTrace;
    public DateTime Time;



}//class



}//namespace
Kyle Lahnakoski