Monday, 21 December 2009

DCS and XMLS - Serialization quick ref

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Web.Services;

namespace WcfServiceLibrary1
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
    [ServiceContract(Namespace="http://servicecontract"), XmlSerializerFormat(Style=OperationFormatStyle.Document)]
    public interface ITestService
    {
        [OperationContract]
        //[return: XmlElement(ElementName="MyType", Namespace="http://datacontract", Type= typeof(MyType))]
        MyServiceResponse GetDataUsingDataContract(MyServiceRequest composite);
    }


    /// 
    /// Control WSDL request wrapper
    /// 
    [MessageContract(WrapperNamespace = "http://datacontract")] //IsWrapped defaults to true
    public class MyServiceRequest
    {
        [MessageBodyMember()] 
        public CompositeType compostiteType { get; set; }
    }

    /// 
    /// Control wsdl response wrapper
    /// 
    [MessageContract(WrapperNamespace = "http://datacontract")] //IsWrapped defaults to true
    public class MyServiceResponse
    {
        [MessageBodyMember()]
        public CompositeType compostiteType { get; set; }
    }

    // Composite class for DCS and XMLS
    [DataContract, Serializable, XmlRoot(Namespace = "http://datacontract")] 
    public class CompositeType
    {
        [DataMember, XmlAttribute()]
        public bool BoolValue { get; set; }

        [DataMember, XmlAttribute]
        public string StringValue { get; set; }

        [DataMember, XmlElement]
        public string Description { get; set; }

        // ns required explicitly to ensure each array item has correct ns, but only required if the array item type has a different ns
        // to enclosing type
        [DataMember, XmlArray, XmlArrayItem(typeof(MyType), Namespace = "http://anotherdatacontract")]
        public List TypeList { get; set; }
    }

    /// 
    ///  Decorarated as a type rather than a root object (i.e. lives as a subtype not serialized as an instance directly)
    /// 
    [DataContract, Serializable, XmlType(Namespace = "http://anotherdatacontract")]
    public class MyType
    {
        [DataMember, XmlAttribute]
        public string StringValue { get;set; }
    }


/// Exposed as:
    public class TestService : ITestService
    {
        //[return: XmlElement("MyType", Namespace = "http://datacontract")]
        public MyServiceResponse GetDataUsingDataContract(MyServiceRequest request)
        {
            CompositeType composite = request.compostiteType;
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
                composite.TypeList[0].StringValue += " ... Hello";
                System.Diagnostics.Debug.WriteLine(HRG.Support.Serialisation.Serialise(composite));
            }
            MyServiceResponse resp = new MyServiceResponse();
            resp.compostiteType = composite;
            return resp;
        }
    }

}

No comments: