views:

46

answers:

2

I'm trying to understand serialization more in context to the legacy .NET web services. Lets say I return a complex type from one of my .asmx methods. Does that type have to be marketed serializable? Or does the web service reference and web service iteself take care of all this serializing and deserializing?

+1  A: 

If you want to send a complex type across the wire it has to be serializable. You can do that a couple of ways, one being to use the Serializable attribute, which is common. However, if your custom type is not serializable, then your web service will not be able to take your object and put it on the wire to send to the client.

Joseph
... and if the custom type contains other custom types as internal fields, then those must also be serializable.
Charles Bretana
@Charles absolutely... just marking your type with the Serializable attribute doesn't mean your type can actually be serialized. If the web service tries to serialize it and fails because of a internal field or what have you, then you'll get a SerializationException
Joseph
+1  A: 

ASMX uses the XmlSerializer internally to serialize types, so all of the usual XmlSerializer rules apply (type needs to have public parameterless constructor, any public fields and public get/set properties will be serialized, System.Xml.Serialization attributes have special meaning, etc.)

The [Serializable] attribute has absolutely no meaning to the XmlSerializer, and hence to ASMX. It was originally introduced for the BinaryFormatter (used in .NET remoting), and is now supported by the DataContractSerializer (used in WCF) as an alternative to the recommended [DataContract] programming model.

Eugene Osovetsky