views:

132

answers:

3

Do objects added to the SqlException.Data collection need to be [Serializable]?

A: 

Well, strictly speaking, no... you can add any type to an Exception's Data dictionary... but why would you ask? What boundary do you think they are going to be serialized across?

ZeroBugBounce
A: 

If the exception is going to be propagated across appdomain boundaries, the exceptions and the data they contain need to be serializable.

One such scenario would be a client-server application communicating over remoting. If the server throws an exception and it needs to be handled at client side, framework will have to serialize/deserialize it.

Ishmaeel
+1  A: 

Yes, they need to be. It's because ISerializable's implementation in Exception type add Data property into StreamingContext. And all objects in Data property (which is a IDictionary) must be serializable. Having exception classes and instances serializable is good practice even if aren't going to build distributed app.

Shrike