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 ...
随机推荐
- 20151211Jquery Ajax进阶学习笔记
四.JSON 和 JSONP 如果在同一个域下,$.ajax()方法只要设置 dataType 属性即可加载 JSON 文件.而在非 同域下,可以使用 JSONP,但也是有条件的. //$.ajax( ...
- ASP.NET MVC5总结(三)登陆中常用技术解析之session与cookie
1.session机制 session机制是在服务器端保持状态的方案,在做系统登陆时,我们往往会用到session来存储一些用户登录的重要信息,而这些信息是不能存在cookie中的. 当访问量增多时, ...
- 最新的 cocoapods 安装与使用(2016.11)
cocoapods简介: cocoapods 是iOS的类库管理工具,可以让开发者很方便集成各种第三方库,而不用去网站上一个个下载,再一个个文件夹的拖进项目中,还得添加相关的系统依赖库.只需要安装好c ...
- DBHelper 数据库帮助类
/// <summary> /// 数据库帮助类 /// <author>vito</author> /// </summary> public cla ...
- jQuery动态效果实例
jQuery常见的动态效果: 隐藏/显示效果: 1.(1):隐藏,显示:通过 jQuery,使用 hide() 和 show() 方法可以用来隐藏和显示 HTML 元素. (2):隐藏/显示的速度: ...
- 暑假集训(1)第二弹 -----Catch the cow(Poj3278)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- [工具]toolbox_graph基本操作
toolbox_graph提供了对3D模型的一些操作.MATLAB代码源自:http://www.mathworks.com/matlabcentral/fileexchange/5355-toolb ...
- 【实习记】2014-08-27堆排序理解总结+使用typedef指代函数指针
过程记录 4个月前C语言版的七大排序算法实践让我在写C++版时轻车熟路.特别是冒泡,插入,希尔,选择这四种排序不用调试即运行成功.输出的效果与C语言做的版本完全一样,其中令我印象深刻的是,co ...
- Makefile隐含规则和用到的默认变量
如果要使用隐含规则生成你需要的目标,你所需要做的就是不要写出这个目标的规则.那么,make会试图去自动推导产生这个目标的规则和命令,如果make可以自动推导生成这个目标的规则和命令,那么这个行为就是隐 ...
- Android学习6—单元测试的使用
在这里对单元测试的使用,主要介绍两种方法,1.手动添加配置信息,然后编写测试类.2.通过Eclipse创建测试项目 1.手动添加配置信息,然后编写测试类: 待测试的类:/src/com/example ...