Search code examples
wcfweb-servicesxsddatacontractxsd.exe

WCF SerializableAttribute DataContractAttribute XmlTypeAttribute xsd.exe tool


I have generated a set of classes based on a set of respective *.xsd files using xsd.exe tool, as below:

xsd /c file1.xsd File2.xsd 

One of the generated classes is shown below:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]

[System.SerializableAttribute()]   //please note here

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]

//please note here
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.xx.com/xx")]

public partial class SelfEmploymentTypePartner
{

    private string nameField;

    private SelfEmploymentTypePartnerAddress addressField;

    /// <remarks/>
    public string Name
    {
        get { return this.nameField; }
        set { this.nameField = value; }
    }

    /// <remarks/>
    public SelfEmploymentTypePartnerAddress Address
    {
        get { return this.addressField; }
        set { this.addressField = value; }
    }
}

I want to use all generated classes for WCF, do I need to add DataContractAttribute, and DataMemeberAttribute to the class and its members? as below:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]

[System.SerializableAttribute()]    //please note here

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]

   //please note here
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, 
Namespace ="urn:corexx:Application")]

[DataContract(Name = "SelfEmploymentTypePartner")]  //please note here

public partial class SelfEmploymentTypePartner
{

    private string nameField;

    private SelfEmploymentTypePartnerAddress addressField;

    /// <remarks/>
    [DataMember]
    public string Name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    /// <remarks/>
    [DataMember]
    public SelfEmploymentTypePartnerAddress Address
    {
        get
        {
            return this.addressField;
        }
        set
        {
            this.addressField = value;
        }
    }
}

Because the existing live version is using the version 1, If I change it to version 2, will that break the current users?

If I do need to, is there a easy way to do that. Because I have more than thousands of line of codes to edit.

The interface is below:

 [ServiceContract(SessionMode = SessionMode.Allowed, Namespace = "http://www.xxx.com" ,ProtectionLevel=ProtectionLevel.None)]
    [WsdlDocumentation("")]
    public interface IService
    {

        [OperationContract(ProtectionLevel= ProtectionLevel.None)]   //please note here
        [XmlSerializerFormat]   //please note here
        [FaultContract(typeof(ErrorDetailsStructure))]
        MyResponse GetInfo(RequestParameter parameter);

    }

When both OperationContract and XmlSerializerFormat are used for the interface, which one will be used.. Does WCF use XmlSerializer instead of DataContractSerializer?


Solution

  • You don't need to change the generated code. You only need to tell WCF you are using a different serializer in your service contract definition:

    [ServiceContract]
    [XmlSerializerFormat]
    public interface MyService
    {
        [OperationContract]
        [XmlSerializerFormat]
        void MyMethod();
    }
    

    However, you should read the pro's and con's of using DataContractSerializer vs XmlSerializer.