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 ...
随机推荐
- 【SQL】Update中使用表别名、如何用表中一列值替换另一列的所有值
Update中使用表别名 select中的表别名: select * from TableA as ta update中的表别名: update ta from TableA as ta 如何用表中一 ...
- WindowListener中的windowClosed方法不执行的问题。
1.在正常情况下windowClosed方法不执行: 2.调用dispose方法,windowClosed方法会执行.例如:在windowClosing方法中执行dispose方法,windowClo ...
- MySQL分区表例子——List分区
列表分区(List分区) 这里假设表中有一个sale_item_type 字段,数据类型为INT 型 当sale_item_type 为1,3,5的时候,作为一个分区 当sale_item_type ...
- c++ primer复习(二)
1 悬垂else来自于else语句搭配的if语句的二义性 2 理解switch代码: switch(c) { case 'a': a++; case 'b': b++; default: x++; } ...
- 对称密码-分组密码-AES
AES产生背景: DES的安全性和应用前景受到挑战,因此需要设计一个高保密性能的.算法公开的.全球免费使用的分组密码算法,用于保护敏感信息,并希望以此新算法取代DES算法,称为新一代数据加密标准,取名 ...
- Spark-Cassandra-Connector 插入数据函数saveToCassandra
在spark-shell中将数据保存到cassandra: var data = normalfill.map(line => line.split("\u0005")) d ...
- ubuntu ll命令
用过 Redhat 的朋友应该很熟悉 ll 这个命令,就相当于 ls -l,但在 Ubuntu 中就不行了.严格来说 ll 不是一个命令,只是命令的别名而已.很多 Linux 用户都使用 bash s ...
- wamp配置
# #localhost # <VirtualHost *:> DocumentRoot "D:/software/wamp/www" ServerName local ...
- linux根目录详解
ubuntu 文件说明:http://tech.ccidnet.com/art/302/20080118/1347213_1.html/ 根目录 | |-boot/ 启动文件.所有与系统启动有关的 ...
- sql的连接查询方式
1 SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据. Join 和 Key 有时为了得到完整的结果,我们需要从两个或更多的表中获取结果.我们就需要执行 join. 数据库中 ...