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.