WCF SerializableAttribute DataContractAttribute XmlTypeAttribute xsd.exe工具

时间:2011-12-06 12:20:25

标签: wcf web-services xsd datacontract xsd.exe

我使用xsd.exe工具基于一组相应的* .xsd文件生成了一组类,如下所示:

xsd /c file1.xsd File2.xsd 

其中一个生成的类如下所示:

[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; }
    }
}

我想将所有生成的类用于WCF,我是否需要将 DataContractAttribute和DataMemeberAttribute添加到类及其成员中?如下:

[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;
        }
    }
}

由于现有的实时版本使用版本1,如果我将其更改为版本2,会破坏当前用户吗?

如果我确实需要,有一个简单的方法来做到这一点。因为我有超过数千行代码要编辑。

界面如下:

 [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);

    }

当OperationContract和XmlSerializerFormat都用于界面时,将使用哪一个。 WCF是否使用XmlSerializer 而不是DataContractSerializer?

1 个答案:

答案 0 :(得分:1)

您无需更改生成的代码。您只需要告诉WCF您在服务合同定义中使用不同的序列化程序:

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

但是,您应该阅读使用DataContractSerializer与XmlSerializer的专业版和版本。