简体   繁体   English

WCF:DataMember属性中的[XmlElement,typeof]

[英]WCF : [XmlElement, typeof] in DataMember attribute

I want to do something like : 我想做这样的事情:

[XmlElement("Foo1", typeof(Foo1Type))]
[XmlElement("Foo2", typeof(Foo2Type))]
public object Foo { get; set; }

I want to do this with the DataContractSerializer for WCF. 我想使用WCF的DataContractSerializer做到这一点。 Something similar in Datamember attribute. Datamember属性中的内容类似。

This is the equivalent of xsd:Choice . 这等效于xsd:Choice

I aim to assign dynamically tags according to their types. 我旨在根据标签的类型动态分配标签。

Thank you. 谢谢。

According to the following MSDN link, the DataContractSerializer does not support mapping XSD choice to the CLR equivalent types. 根据以下MSDN链接, DataContractSerializer不支持将XSD选择映射到CLR等效类型。

choice  |  Forbidden

Forbidden . 禁止 The DataContractSerializer does not support importing a schema using the feature. DataContractSerializer不支持使用该功能导入架构。 For example, Svcutil.exe, when accessing a WSDL with a schema that uses such a feature, falls back to using the XmlSerializer instead. 例如,Svcutil.exe在使用具有此类功能的架构访问WSDL时,会转而使用XmlSerializer。 This is by default. 默认情况下。

http://msdn.microsoft.com/en-us/library/ms733112(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/ms733112(v=vs.110).aspx

Note: You may be able to work around the issue using Data Contract Known Types. 注意:您也许可以使用“数据合同已知类型”解决此问题。 http://msdn.microsoft.com/en-us/library/ms730167.aspx http://msdn.microsoft.com/en-us/library/ms730167.aspx

I had to change the ServiceContract as follows: 我不得不按如下方式更改ServiceContract

[ServiceContract(Name="ChoiceService", Namespace="http://dev.janus-net.de/example/choice")]
[XmlSerializerFormat(Style = OperationFormatStyle.Document)]
public interface IChoiceService

And add Serializable in DataContract: 并在DataContract中添加Serializable

[DataContract(Namespace = "http://...", Name = "yourContract")]
[Serializable]

The complet example are in: http://www.janus-net.de/2007/08/07/using-xschoice-in-wcf-services/ 完整的示例位于: http : //www.janus-net.de/2007/08/07/using-xschoice-in-wcf-services/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM