Data Transfer in Service Contracts

DataContract is the default serialization programming model for WCF. However WCF supports more than just the types marked wth DataContract attribute. It supports serialization of the following kinds of types in the default mode.

  1. CLR built-in types
  2. Byte array, DateTimeTimeSpanGUIDUriXmlQualifiedNameXmlElement and XmlNodearray [This includes XElement and XNode array from .NET 3.5]
  3. Enums
  4. Types marked with DataContract or CollectionDataContract attribute
  5. Types that implement IXmlSerializable
  6. Arrays and Collection classes including List<T>Dictionary<K,V> and Hashtable.
  7. Types marked with Serializable attribute including those that implement ISerializable.
  8. Types with none of the above attributes (POCO) but with a default constructor (can be non-public). [This is supported only from .NET 3.5 SP1]

 Some types may implement more than one of the above programming model. In such cases a programming model is chosen based on its priority as given by the above list. For example,Hashtable is a collection class but also implements ISerializable and it is serialized as a collection type. DataSet implements both IXmlSerializable and ISerializable and it is serialized as IXmlSerializable.

you can manually switch to the XmlSerializer class by applying the XmlSerializerFormatAttributeattribute to your service

[Serializable]

    public class CustomerISerializable : ISerializable

    {

        public string Id { get; set; }

        public string Name { get; set; }

        public CustomerISerializable() { }



        public void GetObjectData(SerializationInfo info, StreamingContext context)

        {

            info.AddValue("Id", Id);

            info.AddValue("Name", Name);

        }



        protected CustomerISerializable(SerializationInfo info, StreamingContext context)

        {

            Id = info.GetString("Id");

            Name = info.GetString("Name");

        }

    }

but you shoule add XmlSerializerFormatAttributeattribute to your service

[ServiceContract]

    interface ICustomerServices

    {

        [OperationContract]

        [XmlSerializerFormat]

        string GetCustomer(CustomerISerializable c);

    }

As MSDN metioned . When you want to switch to XmlSerializer from the default for the serialization .You should apply the XmlSerializerFormatAttribute .

However. As i know. Serializable Attribute and Implement ISerializable interface is only  useful in the binary serialization . so .Why need apply XmlSerializerFormatAttribute  in the WCF when we apply Serializable Attribute or implement ISerializable interface in the types. 

otherwise this method will be not available. because of using this kind of type.(Serializble or ISerializable)

Can anyone tell me why ?

refer to :http://www.codeproject.com/Articles/30279/Serialization-in-Windows-Communication-Foundation

 http://www.cnblogs.com/chnking/archive/2008/06/06/1215411.html

http://www.cnblogs.com/chnking/archive/2008/02/25/1081417.html(soapformat and binary format in serialization)

http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx

你可能感兴趣的:(service)