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 ...
随机推荐
- Linux基础1之磁盘与分区
Linux上面设备皆文件,目前需要知道的,比如U盘和SARA硬盘的在Linux上面的文件名,/dev/sd[a-p].与IDE接口不同的是,SATA/USB接口的磁盘没有一定的顺序,这里就根据Linu ...
- jasper2
package jasper; import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;impo ...
- 淘宝开源Web服务器Tengine安装教程
简介Tengine是由淘宝核心系统部基于Nginx开发的Web服务器,它在Nginx的基础上,针对大访问量网站的需求,添加了很多功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,淘宝商 ...
- jQuery对象和Dom对象的区分以及之间转换
刚开始学习jQuery,可能一时会分不清楚哪些是jQuery对象,哪些是DOM对象.至于DOM对象不多解释,我们接触的太多了,下面重点介绍一下jQuery,以及两者相互间的转换. 一,什么是jQuer ...
- 理解RunLoop
一.什么是RunLoop? 从字面意思上来看,RunLoop就是运行循环,跑圈的意思. 我们都知道,一般来说一个线程执行一次任务之后便会退出,在iOS中,如果主线程只执行一次便退出的话也就意味着程序的 ...
- Makefile隐含规则和用到的默认变量
如果要使用隐含规则生成你需要的目标,你所需要做的就是不要写出这个目标的规则.那么,make会试图去自动推导产生这个目标的规则和命令,如果make可以自动推导生成这个目标的规则和命令,那么这个行为就是隐 ...
- getDrawingRect,getHitRect,getLocalVisibleRect,getGlobalVisibleRect
本文主要大体讲下getHitRect().getDrawingRect().getLocalVisibleRect().getGlobalVisibleRect. getLocationOnScree ...
- event的属性
t获取鼠标相对于浏览器左上角的坐标 <div id="dv" style=" width:300px; height:200px; background-color ...
- hadoop1中partition和combiner作用
---恢复内容开始--- 1.解析Partiton 把map任务的输出的中间结果按照key的范围进行划分成r份,r代表reduce任务的个数.hadoop默认有个类HashPartition实现分区, ...
- Custom template tags and filters
Code layout Custom template tags and filters must live inside a Django app. If they relate to an exi ...