【问题标题】:WCF SerializableAttribute DataContractAttribute XmlTypeAttribute xsd.exe toolWCF SerializableAttribute DataContractAttribute XmlTypeAttribute xsd.exe 工具
【发布时间】:2011-12-06 12:20:24
【问题描述】:

我已经使用 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;
        }
    }
}

由于现有的live版本使用的是版本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?

【问题讨论】:

  • 如果要生成数据合约,请不要使用 xsd.exe。这是旧的 XML 序列化器技术的一部分。请改用 svcutil.exe。
  • 谢谢!是否有充分的理由使用 svcutil.exe 代替。 xsd.exe 会被弃用吗?还是 svcutilexe 更适合 WCF?
  • svcutil.exe 用于 WCF。根本不要使用 xsd.exe。 “已弃用”是一个法律术语,但“不要使用它”是事实。
  • 非常感谢!我想知道您是否可以查看有关如何使用 svcutil.exe 的链接。 stackoverflow.com/questions/8407889/…

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


【解决方案1】:

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

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

但是,您应该阅读使用 DataContractSerializer 与 XmlSerializer 的优缺点。

【讨论】:

  • 据我所知,该属性在类型或方法上都是必需的,但在两者上都是多余的。
猜你喜欢
  • 2011-05-26
  • 2010-12-27
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多