Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the WCF data contract?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "what is the WCF data contract". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what WCF data contract is.

WCF data contracts (Data Contract) are used to identify user-defined types and serialization. DataContractAttribute and DataMemberAttribute are used to label custom data types so that we can pass complex data volumes in service methods.

Before using it, we need to add a System.Runtime.Serialization.dll reference. From this we can see its basic development mode, that is to use ServiceContract and OperationContract to perform operations, and use DataContract and DataMember as serializable data carriers. Of course, we can also use "Serializable" instead of "DataContract". In fact, WCF data contracts mainly define the format of the data (contracts). DataMember () is the part that tells the serialization engine to serialize. (about serialization, note that any field can be accessed during deserialization, and the scope keyword does not work, which may expose security issues. )

DataContract public struct Number {DataMember public double Num1; DataMember public double Num2; public Number (double num1, double num2) {this.Num1 = num1; this.Num2 = num2;}}

Properties of DataContract

Name / Namespace: custom names and namespaces.

Properties of DataMember

Name: custom name.

IsRequired: indicates that the member must be assigned before being serialized.

DataContractSerializer serialization is actually a process, but this process is mostly implemented automatically by the system. By default, WCF uses the DataContractSerializer engine to serialize the relevant parameters, which is also recommended by WCF. Another option is XmlSerializer, which is the serialization engine used by ASP.NET Web Service. XmlSerializer supports only some of the types supported by DataContractSerializer, but it allows you to use features such as XmlAttributeAttribute to have more control over the serialized generated XML.

Types supported by DataContractSerializer:

All basic types are supported, including common types such as XmlElement and DateTime.

Types that use DataContractAttribute tags are supported.

Supports the use of SerializableAttribute tags or types that implement ISerializable interfaces.

The type that implements the IXmlSerializable interface.

Most collection (including generic) types, including the commonly used Array, List, IList, and so on.

KnownTypes object inheritance is common in OO, but if WCF uses inheritance directly, it is actually divided into two separate classes. At this point, you need to use KnownType to identify it, so that it becomes inherited after the client generates the proxy.

At this point, I believe you have a deeper understanding of "what the WCF data contract is". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report