何时使用 DataContract 和 DataMember 属性?

发布于 2024-10-14 14:51:44 字数 674 浏览 3 评论 0原文

我对 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; }
}

它工作正常,但是当我删除 DataContractDataMember 时,它也可以正常工作。我不明白为什么它可以正常工作。谁能告诉我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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

拥抱我好吗 2024-10-21 14:51:44

由于许多程序员对 [DataContract][DataMember] 属性感到不知所措,因此在 .NET 3.5 SP1 中,Microsoft 使数据协定序列化程序能够处理所有类 - 即使没有这些属性中的任何一个 - 很像旧的 XML 序列化程序。

因此,从 .NET 3.5 SP1 开始,您不再必须添加数据协定或数据成员属性 - 如果您不这样做,那么数据协定序列化程序将序列化您的类上的所有公共属性,只需就像 XML 序列化器一样。

但是:如果不添加这些属性,您将失去许多有用的功能:

  • 如果没有 [DataContract],您就无法为数据定义 XML 命名空间,
  • 而没有 >[DataMember],如果没有 [DataMember],则无法序列化非公共属性或字段
  • ,无法定义序列化顺序 (Order=),并且 如果没有 [DataMember] ,DCS 将按字母顺序序列化所有属性,
  • ,您无法为属性定义不同的名称 (Name=)
  • 如果没有 [DataMember] ,如果没有 [DataMember],您就无法定义诸如 IsRequired= 之类的内容或其他有用的属性
  • ,您不能遗漏某些公共属性 - 所有公共属性都将由 DCS 序列化,

因此对于“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:

  • 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-10-21 14:51:44

就WCF而言,我们可以通过消息与服务器和客户端进行通信。为了传输消息,从安全角度来看,我们需要以序列化格式制作数据/消息。

为了序列化数据,我们使用 [datacontract] 和 [datamember] 属性。
在您的情况下,如果您使用datacontract,则WCF使用DataContractSerializer,否则WCF使用XmlSerializer,这是默认的序列化技术。

让我详细解释一下:

基本上WCF支持3种类型的序列化:

  1. XmlSerializer
  2. DataContractSerializer
  3. NetDataContractSerializer

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 uses DataContractSerializer else WCF uses XmlSerializer which is the default serialization technique.

Let me explain in detail:

basically WCF supports 3 types of serialization:

  1. XmlSerializer
  2. DataContractSerializer
  3. NetDataContractSerializer

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

回梦 2024-10-21 14:51:44

数据契约是服务和客户端之间的正式协议,抽象地描述了要交换的数据。也就是说,为了进行通信,客户端和服务不必共享相同的类型,只需共享相同的数据契约。数据协定针对每个参数或返回类型精确定义了要交换的数据序列化(转换为 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.

还如梦归 2024-10-21 14:51:44

数据契约是服务和客户端之间的正式协议,抽象地描述了要交换的数据。

数据契约可以是显式的或隐式的。简单类型(例如 int、string 等)具有隐式数据契约。用户定义的对象是显式或复杂类型,您必须使用 [DataContract] 和 [DataMember] 属性为其定义数据契约。

数据契约可以定义如下:

  • 它描述了传入和传出服务操作的数据的外部格式

  • 它定义了结构和服务消息中交换的数据类型

  • 它将 CLR 类型映射到 XML 模式。
  • 它定义了数据类型如何序列化和反序列化。通过序列化,您可以将对象转换为可以通过网络传输的字节序列。通过反序列化,您可以根据从调用应用程序收到的字节序列重新组装对象。
  • 它是一个版本控制系统,允许您管理对结构化数据的更改。

我们需要包含对项目的 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

  • It maps a CLR type to an XML Schema
  • It defines how data types are serialized and deserialized. Through serialization, you convert an object into a sequence of bytes that can be transmitted over a network. Through deserialization, you reassemble an object from a sequence of bytes that you receive from a calling application.
  • It is a versioning system that allows you to manage changes to structured data

We need to include System.Runtime.Serialization reference to the project. This assembly holds the DataContract and DataMember attribute.

甜是你 2024-10-21 14:51:44
  1. 数据契约:它指定您的实体类已准备好进行序列化过程。

  2. 数据成员:它指定特定字段是数据协定的一部分并且可以序列化。

  1. Data contract: It specifies that your entity class is ready for Serialization process.

  2. Data members: It specifies that the particular field is part of the data contract and it can be serialized.

因为看清所以看轻 2024-10-21 14:51:44

另外,当你从 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

柠檬心 2024-10-21 14:51:44

序列化数据时不需要添加 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.

ゃ懵逼小萝莉 2024-10-21 14:51:44

数据将在服务内传输和处理,并且存储值,因此在 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.

enter image description here

Source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文