WCF集合数据契约
我最近在一篇文章中注意到,wcf 服务操作返回了一个集合数据契约
Users GetUsers(string someInput);
,并且用户类型定义如下:
[CollectionDataContract]
public class Users : List<User>
{
public Users()
{
}
public Users(IEnumerable<User> users) : base(users)
{
}
}
返回集合数据契约(如本例中的用户)是否与简单返回 List
I recently noticed in one of the articles the wcf service operation returned a collectiondatacontract
Users GetUsers(string someInput);
And Users type was defined as below:
[CollectionDataContract]
public class Users : List<User>
{
public Users()
{
}
public Users(IEnumerable<User> users) : base(users)
{
}
}
Does returning a collectiondatacontract (like Users in this case) serve a different purpose than simply returning List<User>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我了解,在 DataContractSerializer 完成序列化集合的工作后,此属性将使您能够对最终 xml 字符串中元素的名称进行一些控制。
当您稍后必须手动解析结果时,这会很有用(换句话说,您将知道要在该 xml 文本中查找什么元素,以找到您的集合及其部分)。
查看示例和更多信息:
http://msdn.microsoft。 com/en-us/library/aa347850.aspx
As far as I understand, this attribute will give you some control over what names the elements will have in the final xml string, after the DataContractSerializer has done its work serializing your collection.
This can be useful when you have to parse the result later manualy( in other words you will know what element to look for in that xml text, to find your collection and its parts).
Take a look at this for examples and more info:
http://msdn.microsoft.com/en-us/library/aa347850.aspx
如果返回一个 List,则数据序列化器有一种生成 xml 的特定方式。我不知道它如何对 List 执行此操作,但如果它是一个数组,它会生成类似的内容 -
但使用 CollectionDataContract 您可以将其序列化,并更好地向可能手动创建 XML 的消费者公开它。例子 - 我可以给 -
CollectionDataCOntract(Name="AllUsers") // 我不记得 ItemName 或 Name
,那么预期的 XML 会类似于 -
这是一个实用程序。
if you return a List, the data serializer has a specific way of generating xml. I dont know how it does it for List, but if it was an array it would have generated something like -
But using CollectionDataContract you can serialize it and better expose it for consumers who may be creating XML by hand. Example - I would be able to give -
CollectionDataCOntract(Name="AllUsers") // i dont remember ItemName or Name
then the XML expected would be something similar to -
Thats one utility for this.
只是为了阐述 Andrei 的答案并分享我的经验,我刚刚经历了一个最终使用 CollectionDataContract 解决的问题。基本上,为了与特定系统交互,我希望能够发送和接收以下格式的 xml:
但是,如果我使用数组或 List 对象,则根标记始终称为 ArrayOfMessage。如果我创建了一个包含 Message 对象数组的类(假设称为 MsgList),那么 WCF 会将其添加为混合中的额外标记,但我找不到摆脱它的方法。所以它看起来像:
所以 CollectionDataContract 只是给了我一个简单的方法来控制根列表元素的名称。
Just to expound on Andrei's answer and share my experience, I just went through an issue that I finally resolved using CollectionDataContract. Basically, in order to interface with a specific system, I wanted to be able to send and recieve xml of the format:
However, if I used an array or a List object, the root tag was always called ArrayOfMessage. And if I created a class that held an array of Message objects (lets say called MsgList), then WCF would add that as an extra tag in the mix, which I could not find a way to get rid of. So it would have looked like:
So CollectionDataContract just gave me a simple way to control the name of the root list element.