When to use DataContract and DataMember attributes?
When to use DataContract and DataMember attributes?
I am very confused about the DataContract attribute in WCF. As per my knowledge it is used for serialization user defined type like classes. I write a one class which is expose at client side.
[DataContract]
public class Contact
{
[DataMember]
public int Roll { get; set; } [DataMember]
public string Name { get; set; } [DataMember]
public string Address { get; set; } [DataMember]
public int Age { get; set; }
}
It is working properly but when I remove DataContract at class level as well as DataMember it is also work properly. I can't understand that if it is working properly so why there is a need of DataContract? Can any one tell me what is the actual use of DataContract?
My service contract looks like this
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
Contact XmlData(string id);
}
解答:
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 likeIsRequired=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...
When to use DataContract and DataMember attributes?的更多相关文章
- Configure JSON.NET to ignore DataContract/DataMember attributes
https://stackoverflow.com/questions/11055225/configure-json-net-to-ignore-datacontract-datamember-at ...
- DataContract和DataMember的作用
数据契约(DataContract)里的DataMember特性 来源于网络,用于个人收集及备忘 数据契约(DataContract) 服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端 ...
- DataContract 和 DataMember
数据契约(DataContract) 服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可以被 ...
- [DataContract]和[DataMember]缺少引用
1.项目->右键->添加引用->找到System.Runtime.Serialization 2.代码中加上 Using System.Runtime.Serialization
- WCF Windows Service Using TopShelf and ServiceModelEx z
http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are ...
- 数据契约(DataContract)及序列化指定输出字段
服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可以被序列化在服务端和客户端之间传送,如下所 ...
- 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化
谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...
- Conversion between json and object
public static string ObjToJson<T>(T obj) { DataContractJsonSerializer serializer = new DataCon ...
- 使用ServiceStack构建Web服务
提到构建WebService服务,大家肯定第一个想到的是使用WCF,因为简单快捷嘛.首先要说明的是,本人对WCF不太了解,但是想快速建立一个WebService,于是看到了MSDN上的这一篇文章 Bu ...
随机推荐
- iOS NSNumber语法糖
BOOL equal; NSNumber * num1 = [NSNumber numberWithInteger:]; NSNumber * num2 = @; equal = [num1 isEq ...
- 设置contentType
//定义编码 header( 'Content-Type:text/html;charset=utf-8 '); //Atom header('Content-type: application/at ...
- 10.21_Nutz批量插入顺序,POI,wiki持续关注,POI,SSH,数据库优先
(1)Nutz,dao的批量插入会关注顺序吗? http://www.douban.com/group/topic/64322582/ (2)工作需要优先!!! POI,SSH,数据库管理及plsq ...
- 手动通过Lucene判断该pom文件中jar是否存在,子依赖没判断
package lucne.test; import java.io.File; import java.io.FileNotFoundException; import java.io.IOExce ...
- mvc3.0ModelFirst生成实体
前沿 这几天想用mvc写点东西,mvc现在自己工作也不用,所以有些生.于是弄点视频研究一下.可能一些经常接触mvc的对这个问题看来,就是小kiss,但是我感觉自己研究出来了还是比较兴奋.在3.0根据模 ...
- js事件对象--DOM中的事件对象/IE中的事件对象/跨浏览器的事件对象
事件对象 在触发DOM上的某个事件时,会产生一个事件对象event,这个对象中包含着所有与事件有关的信息.包括导致事件的元素.事件的类型,以及其他与特定事件相关的信息.例如,鼠标操作导致的事件对 ...
- ubuntu远程登陆windows
首先安装rdesktop : apt-get install rdesktop.p 程序安装完后,在终端命令行中输入:$ rdesktop -g 1024x768 -d 24 ip,就进入了windo ...
- php中文编码
header("Content-type: text/html; charset=utf-8"); header('Location: http://www.example.com ...
- lucene开发序之luke神器
lucene是一款很优秀的全文检索的开源库,目前最新的版本是lucene4.4,关于lucene的历史背景以及发展状况,在这里笔者就不多介绍了,如果你真心想学习lucene,想必在这之前你已经对此作过 ...
- 由 OR 引起的死循环
在客商迁移测试时,程序一旦开始执行就不能自动停止.只能通过手动中断应用服务器的进程来停止.检查迁移的一个表,这个表迁移前没有数据,迁移最多会插入3w条左右数据,但是迁移过程执行2个多小时候再看,已经有 ...