wcf服务中的数据和成员合同 [英] Data and Member contract in wcf service

查看:73
本文介绍了wcf服务中的数据和成员合同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我没有提到数据合同和成员合同,我的wcf服务也运行正常。那么在wcf服务中使用这两个合同有什么好处。

解决方案

由于使用.NET 3.5 SP1,很多程序员都被 [DataContract] [DataMember] 属性所淹没,微软让数据契约序列化程序处理所有类 - 即使没有任何属性 - 就像旧的XML序列化程序一样。



从.NET 3.5 SP1开始,你不要不再需要添加数据协定或数据成员属性 - 如果不这样做,那么数据协定序列化程序将序列化类上的所有公共属性,就像XML序列化程序一样。



但是:通过不添加这些属性,您将失去许多有用的功能:



没有 [DataContract] ,你不能为你的数据定义一个XML命名空间,而没有 [DataMem] ber] ,您无法序列化非公共属性或字段

没有 [DataMember] ,您无法定义订单序列化(Order =)和DCS将按字母顺序序列化所有属性

没有 [DataMember] ,您无法为您的属性定义不同的名称(名称=)

没有 [DataMember] ,你无法定义像IsRequired =或其他有用属性的东西

没有 [DataMember] ,你不能遗漏某些公共财产 - 所有公共财产都将由DCS序列化。



因此,对于quick'n'dirty解决方案,请忽略 [DataContract] [DataMember] 属性工作 - 但是将它们放在数据类上仍然是一个好主意 - 只是为了更明确地了解你正在做什么,并让自己访问所有那些没有它们就没有的附加功能。

[from here] [ ^ ]



您可以在此处找到有关合同的更多详细信息:

http://www.dotnet-tricks.com/Tutorial/wcf/3ISW280913-Understanding-different-types-of-WCF-Contracts.html [ ^ ]

了解WCF中的合同 [ ^ ]



- KR

If i not mentioned Data contract and Member contract also my wcf service is working good.Then what is benefits to use these 2 contracts in wcf service.

解决方案

Since a lot of programmers were overwhelmed with the [DataContract] and [DataMember] attributes, with .NET 3.5 SP1, Microsoft made the data contract serializer handle all classes - even without any of those attributes - much like the old XML serializer.

So as of .NET 3.5 SP1, you don't have to add data contract or data member attributes anymore - if you don't then the data contract serializer will serialize all public properties on your class, just like the XML serializer would.

HOWEVER: by not adding those attributes, you lose a lot of useful capabilities:

without [DataContract], you cannot define an XML namespace for your data to live in
without [DataMember], you cannot serialize non-public properties or fields
without [DataMember], you cannot define an order of serialization (Order=) and the DCS will serialize all properties alphabetically
without [DataMember], you cannot define a different name for your property (Name=)
without [DataMember], you cannot define things like IsRequired= or other useful attributes
without [DataMember], you cannot leave out certain public properties - all public properties will be serialized by the DCS.

So for a "quick'n'dirty" solution, leaving away the [DataContract] and [DataMember] attributes will work - but it's still a good idea to have them on your data classes - just to be more explicit about what you're doing, and to give yourself access to all those additional features that you don't get without them.
[from here][^]

You can find more details about contracts here:
http://www.dotnet-tricks.com/Tutorial/wcf/3ISW280913-Understanding-different-types-of-WCF-Contracts.html[^]
Understanding Contracts in WCF[^]

-KR


这篇关于wcf服务中的数据和成员合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆