WCF SerializedAttribute DataContractAttribute XmlTypeAttribute 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;
}
}
}
因为现有的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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要更改生成的代码。您只需告诉 WCF 您在服务协定定义中使用不同的序列化器:
但是,您应该阅读使用 DataContractSerializer 与 XmlSerializer 的优缺点。
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:
However, you should read the pro's and con's of using DataContractSerializer vs XmlSerializer.