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 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...

时间: 2024-11-06 22:48:43

When to use DataContract and DataMember attributes?的相关文章

WCF DataContract and DataMember

这篇来谈一下WCF中重要的两个成员,WCF DataContract and DataMember,如果我们想在客户端调用服务,就需要标注我们的对象或者对象中的属性为契约对象或者契约成员,这样的话,WCF就会将我们的传输的对象转化为XML的格式进行传输.下面还是来做一个小例子,通过在客户端输入用户ID,来从服务端查询用户的信息:在服务端根据WEB端传输的用户ID去库中进行查询. 1.数据库建立 create table tblEmployee ( id int, name nvarchar(50

WCF Windows Service Using TopShelf and ServiceModelEx z

http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are two excellent .NET libraries that help us to build enterprise solutions using the Windows Communication Foundation (WCF) – TopShelf and ServiceModelEx

C# DataContract DataMember

Windows Communication Foundation (WCF) uses a serialization engine called the Data Contract Serializer by default to serialize and deserialize data (convert it to and from XML). A data contract is a formal agreement between a service and a client tha

.net中对象序列化技术浅谈

.net中对象序列化技术浅谈 2009-03-11 阅读2756评论2 序列化是将对象状态转换为可保持或传输的格式的过程.与序列化相对的是反序列化,它将流转换为对象.这两个过程结合起来,可以轻松地存储和传输数 据.例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象.反之,反序列化根据流重新构造对象.此外还可以将对象序列化后保存到本地,再次运行的时候可以从本地文件 中“恢复”对象到序列化之前的状态.在.net中有提供了几种序列化的方式:二进制序列化

WebApi接口 - 响应输出xml和json

格式化数据这东西,主要看需要的运用场景,今天和大家分享的是webapi格式化数据,这里面的例子主要是输出json和xml的格式数据,测试用例很接近实际常用情况:希望大家喜欢,也希望各位多多扫码支持和点赞谢谢: . 自定义一个Action,响应输出集合数据 . api返回json数据的两种方式 . json时间格式处理方式 . 让api接口支持返回json和xml数据 下面一步一个脚印的来分享: . 自定义一个Action,响应输出集合数据 首先,我们新建一个webapi项目,新建好以后我们能够找

WCF搭建

WCF搭建 前言:前面三篇分享了下DDD里面的两个主要特性:聚合和仓储.领域层的搭建基本完成,当然还涉及到领域事件和领域服务的部分,后面再项目搭建的过程中慢慢引入,博主的思路是先将整个架构走通,然后一步一步来添加相关元素,使架构慢慢变得丰满.这篇打算分享下应用层的搭建.根据DDD的设计原则,应用层不包含任何领域逻辑,它主要的作用是协调任务,或者叫调度任务,维护应用程序状态.根据博主的理解,应用层是用来隔离领域层的,假设没有应用层,那么我们的界面层可以直接调用领域层的逻辑,也就是说可以直接访问领域

DatacontractSerializer序列化

DatacontractSerializer在命名空间System.Runtime.Serialization下.它能够序列化DataContract.DataMember标记的类.  一.序列化规则: 1.没有显式声明DataMember特性的成员不会参与序列化. 2.所有元素序列化后都是XmlElement形式(不同于XmlSerializer的XmlAttribute可以将元素序列化到元素属性中). 3.序列化后的元素顺序:父类元素在前,子类元素在后.同一类型中的数据成员按字母顺序排序.

使用ServiceStack构建Web服务

提到构建WebService服务,大家肯定第一个想到的是使用WCF,因为简单快捷嘛.首先要说明的是,本人对WCF不太了解,但是想快速建立一个WebService,于是看到了MSDN上的这一篇文章 Building Cross-Platform Web Services with ServiceStack,所以这里简要介绍一下如何使用ServiceStack快速建立一个WebService服务. 当然,在开始之前,首先要说明一下ServiceStack是个什么东西. 在国内用ServiceStac

用jQuery的Ajax调用WCF服务编程心得

这两天在写基于WCF服务的后台框架,过程中遇到了一些挫折,经过努力全部解决了,在此分享给大家,使用的工具是Visual Studio 2013. 该后台需要支持通过json来传递和接收数据. 首先,说说搭建过程. 第一步:创建WCF服务应用程序项目WCF. 第二步,创建服务使用的数据类 using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Sch