WCF 和 DataContractSerializer 序列化 CollectionDataContract 修饰的集合类型是否不同?
我有一个非常简单的自定义集合类型,它继承自 List<>并使用 CollectionDataContract。
当我使用 DataContractSerializer.WriteObject 对其进行序列化时,它按照我期望的方式尊重 CollectionDataContract 属性;但是,当我将它用作 WCF 方法的返回类型时,我得到默认的 ArrayOfFoo。
我想知道服务合同中是否缺少一些装饰。
详细信息:
[DataContract(Namespace = "")]
public class Foo
{
[DataMember]
public string BarString { get; set; }
}
[CollectionDataContract(Namespace = "")]
[Serializable]
public class FooList : List<Foo> {}
如果我只是实例化 Foo,然后使用 DataContractSerializer.WriteObject 对其进行序列化,我会得到您所期望的结果:
<FooList>
<Foo>
<BarString>myString1</BarString>
</Foo>
</FooList>
但是,如果我有一个具有这样的方法的服务...
[ServiceContract Name = "MyService"]
public interface IMyService
{
[OperationContract, WebGet(UriTemplate = "foos/")]
FooList GetAllFoos();
}
然后对 http://www.someEndpoint.com/foos/,我明白了:
<ArrayOfFoo>
<Foo>
<BarString>myString1</BarString>
</Foo>
</ArrayOfFoo>
我也尝试过指定 Name= CollectionDataContract 属性中的“MyFooListName”。结果相同:DataContractSerializer 获取备忘录; WCF 没有。
I have a really simple customized collection type that inherits from List<> and uses a CollectionDataContract.
When I use DataContractSerializer.WriteObject to serialize it, it respects the CollectionDataContract attribute the way I'd expect; however, when I use it as a return type for a WCF method, I get the default ArrayOfFoo.
I'm wondering if there is some decoration I'm missing in the service contract.
Details:
[DataContract(Namespace = "")]
public class Foo
{
[DataMember]
public string BarString { get; set; }
}
[CollectionDataContract(Namespace = "")]
[Serializable]
public class FooList : List<Foo> {}
If I just instantiate a Foo and then use DataContractSerializer.WriteObject to serialize it, I get what you'd expect:
<FooList>
<Foo>
<BarString>myString1</BarString>
</Foo>
</FooList>
However, if I have a service with a method like this...
[ServiceContract Name = "MyService"]
public interface IMyService
{
[OperationContract, WebGet(UriTemplate = "foos/")]
FooList GetAllFoos();
}
and then do a GET for http://www.someEndpoint.com/foos/, I get this:
<ArrayOfFoo>
<Foo>
<BarString>myString1</BarString>
</Foo>
</ArrayOfFoo>
I've also tried specifying Name="MyFooListName" in the CollectionDataContract attribute. Same results: DataContractSerializer gets the memo; WCF doesn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Saeed 给我指明了正确的方向:当我一直希望使用 DataContractSerializer 时,我无意中最终得到了 XmlSerializer。
我最终得到了 XmlSerializer...好吧...通过要求它。
特别是,我在服务中使用 XmlSerializerFormat 修饰了方法,如下所示:
我这样做是希望能够在手工滚动的 Foo XML blob 中宽恕成员顺序。当然,当这样做时,最终会得到 XmlSerializer,而不是 DataContractSerializer。
当我去掉 XmlSerializerFormat 属性时,问题就解决了:WCF 现在正在按照我想要的方式序列化我的 FooList 集合。
Saeed sent me in the right direction: I inadvertently ended up with XmlSerializer, when I had been hoping for DataContractSerializer.
I had ended up with XmlSerializer... well... by asking for it.
In particular, I had decorated methods in my service with the XmlSerializerFormat like this:
I had done this in the hopes of forgiving member order in hand-rolled Foo XML blobs. Of course, when one does this one ends up with XmlSerializer, not DataContractSerializer.
When I take away the XmlSerializerFormat attribute, problem solved: WCF is now serializing my FooList collection the way I want.
有关详细信息,请参阅 MSDN:
因此序列化将由 XMLSerializer 完成,并且您无法更改它。
See MSDN for detail:
So the serialization going to be done by XMLSerializer, and you can't change it.
您在配置 WCF 服务时是否选择了通用类型?如果没有的话,
右键进入配置,然后选择Generic Type,默认是arraylist类型。
Have you selected Generic types while configuring your WCF service? if not then,
right click and go to configuration, then select Generic Type, by default it is arraylist type.