何时使用 DataContract 和 DataMember 属性?
我对 WCF 中的 DataContract 属性感到非常困惑。据我所知,它用于序列化用户定义的类型,例如类。我写了一个类,像这样在客户端公开。
[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; }
}
它工作正常,但是当我删除 DataContract
和 DataMember
时,它也可以正常工作。我不明白为什么它可以正常工作。谁能告诉我DataContract
的实际用途是什么?
我的服务合同是这样的
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
Contact XmlData(string id);
}
I am very confused about the DataContract
attribute in WCF. As per my knowledge it is used for serializating user defined type like classes. I wrote one class which is exposed at client side like this.
[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
and DataMember
it also works properly. I can't understand why it is working properly. 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
由于许多程序员对
[DataContract]
和[DataMember]
属性感到不知所措,因此在 .NET 3.5 SP1 中,Microsoft 使数据协定序列化程序能够处理所有类 - 即使没有这些属性中的任何一个 - 很像旧的 XML 序列化程序。因此,从 .NET 3.5 SP1 开始,您不再必须添加数据协定或数据成员属性 - 如果您不这样做,那么数据协定序列化程序将序列化您的类上的所有公共属性,只需就像 XML 序列化器一样。
但是:如果不添加这些属性,您将失去许多有用的功能:
[DataContract]
,您就无法为数据定义 XML 命名空间,>[DataMember]
,如果没有[DataMember]
,则无法序列化非公共属性或字段Order=
),并且 如果没有[DataMember]
,DCS 将按字母顺序序列化所有属性,Name=
)[DataMember]
,如果没有[DataMember]
,您就无法定义诸如IsRequired=
之类的内容或其他有用的属性因此对于“quick'n'dirty”解决方案,保留
[DataContract]
和[DataMember]
属性将起作用 - 但将它们放在数据类中仍然是一个好主意- 只是为了更明确地说明您正在做什么,并让您自己能够访问所有那些没有它们就无法获得的附加功能...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:
[DataContract]
, you cannot define an XML namespace for your data to live in[DataMember]
, you cannot serialize non-public properties or fields[DataMember]
, you cannot define an order of serialization (Order=
) and the DCS will serialize all properties alphabetically[DataMember]
, you cannot define a different name for your property (Name=
)[DataMember]
, you cannot define things likeIsRequired=
or other useful attributes[DataMember]
, you cannot leave out certain public properties - all public properties will be serialized by the DCSSo 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...就WCF而言,我们可以通过消息与服务器和客户端进行通信。为了传输消息,从安全角度来看,我们需要以序列化格式制作数据/消息。
为了序列化数据,我们使用 [datacontract] 和 [datamember] 属性。
在您的情况下,如果您使用
datacontract
,则WCF使用DataContractSerializer
,否则WCF使用XmlSerializer
,这是默认的序列化技术。让我详细解释一下:
基本上WCF支持3种类型的序列化:
XmlSerializer :- 默认顺序与类相同
DataContractSerializer/NetDataContractSerializer :- 默认顺序按字母顺序
< strong>XmlSerializer :- XML 架构是广泛的
DataContractSerializer/NetDataContractSerializer :- XML 架构受到约束
XmlSerializer :- 无法进行版本控制
DataContractSerializer/NetDataContractSerializer< /strong> :- 可以进行版本控制
XmlSerializer :- 与 ASMX 兼容
DataContractSerializer/NetDataContractSerializer :- 与 .NET Remoting 兼容
XmlSerializer :- 属性 中不需要
XmlSerializer DataContractSerializer/NetDataContractSerializer
:- 此序列化中需要的属性,因此您使用的属性取决于您的要求...
In terms of WCF, we can communicate with the server and client through messages. For transferring messages, and from a security prospective, we need to make a data/message in a serialized format.
For serializing data we use [datacontract] and [datamember] attributes.
In your case if you are using
datacontract
WCF usesDataContractSerializer
else WCF usesXmlSerializer
which is the default serialization technique.Let me explain in detail:
basically WCF supports 3 types of serialization:
XmlSerializer :- Default order is Same as class
DataContractSerializer/NetDataContractSerializer :- Default order is Alphabetical
XmlSerializer :- XML Schema is Extensive
DataContractSerializer/NetDataContractSerializer :- XML Schema is Constrained
XmlSerializer :- Versioning support not possible
DataContractSerializer/NetDataContractSerializer :- Versioning support is possible
XmlSerializer :- Compatibility with ASMX
DataContractSerializer/NetDataContractSerializer :- Compatibility with .NET Remoting
XmlSerializer :- Attribute not required in XmlSerializer
DataContractSerializer/NetDataContractSerializer :- Attribute required in this serializing
so what you use depends on your requirements...
数据契约是服务和客户端之间的正式协议,抽象地描述了要交换的数据。也就是说,为了进行通信,客户端和服务不必共享相同的类型,只需共享相同的数据契约。数据协定针对每个参数或返回类型精确定义了要交换的数据序列化(转换为 XML)。
Windows Communication Foundation (WCF) 默认情况下使用称为数据契约序列化器的序列化引擎来序列化和反序列化数据(将其与 XML 相互转换)。所有 .NET Framework 基元类型(例如整数和字符串)以及某些被视为基元的类型(例如 DateTime 和 XmlElement)都可以在无需其他准备的情况下进行序列化,并且被视为具有默认数据协定。许多 .NET Framework 类型也具有现有的数据协定。
您可以在此处找到完整文章。
A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged.
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). All .NET Framework primitive types, such as integers and strings, as well as certain types treated as primitives, such as DateTime and XmlElement, can be serialized with no other preparation and are considered as having default data contracts. Many .NET Framework types also have existing data contracts.
You can find the full article here.
数据契约是服务和客户端之间的正式协议,抽象地描述了要交换的数据。
数据契约可以是显式的或隐式的。简单类型(例如 int、string 等)具有隐式数据契约。用户定义的对象是显式或复杂类型,您必须使用 [DataContract] 和 [DataMember] 属性为其定义数据契约。
数据契约可以定义如下:
它描述了传入和传出服务操作的数据的外部格式
它定义了结构和服务消息中交换的数据类型
我们需要包含对项目的 System.Runtime.Serialization 引用。该程序集包含 DataContract 和 DataMember 属性。
A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged.
Data contract can be explicit or implicit. Simple type such as int, string etc has an implicit data contract. User defined object are explicit or Complex type, for which you have to define a Data contract using [DataContract] and [DataMember] attribute.
A data contract can be defined as follows:
It describes the external format of data passed to and from service operations
It defines the structure and types of data exchanged in service messages
We need to include System.Runtime.Serialization reference to the project. This assembly holds the DataContract and DataMember attribute.
数据契约:它指定您的实体类已准备好进行序列化过程。
数据成员:它指定特定字段是数据协定的一部分并且可以序列化。
Data contract: It specifies that your entity class is ready for Serialization process.
Data members: It specifies that the particular field is part of the data contract and it can be serialized.
另外,当你从 http 请求调用时,它会正常工作,但是当你尝试从 net.tcp 调用时,你会得到所有这些东西
Also when you call from http request it will work properly but when your try to call from net.tcp that time you get all this kind stuff
序列化数据时不需要添加 DataMember 属性。当未添加 DataMember 属性时,旧的 XMLSerializer 会序列化数据。添加 DataMember 提供了有用的属性,例如 order、name、isrequired,否则无法使用这些属性。
DataMember attribute is not mandatory to add to serialize data. When DataMember attribute is not added, old XMLSerializer serializes the data. Adding a DataMember provides useful properties like order, name, isrequired which cannot be used otherwise.
数据将在服务内传输和处理,并且存储值,因此在 WCF 术语中,它们被称为“数据契约”。
其中班级的每个成员;即,数据契约被称为“数据成员”,它们也将用属性来装饰。
来源
The data is to be transferred and processed within service and they store the values, so in the WCF terminology they are called “Data Contract”.
Where each member of the Class; i.e., The data contract is called “Data Member” and they are also to be decorated with the Attributes.
Source