WCF from theory to Practice (3): The Black contract of Pawnshop No. eighth

Source: Internet
Author: User

The starting point of this article:

By reading this article, we can solve the following problems

What is a contract?

How many kinds of contracts are there? , what are they used for?

How do I define a contract?

is the contract independent of the platform?

What is the difference between a contract and a technology that is similar in the past?

This article is suitable for readers

WCF beginners can have no SOA or other distributed technology experience

What is a contract?

Any distributed application, it is able to pass messages to each other, are in advance of the data exchange rules, this rule is the exchange of data between the two sides (such as server and client) can understand each other's basis, WCF as a distributed development technology, also has such a feature. The rules made in WCF are called contracts (Contract), which is the message standard for WCF and an integral part of any WCF program.

How many kinds of contracts are there? , what are they used for?

In WCF, the contracts are grouped into four categories, each of which is:

Service contract for defining service operations: Services Contract

This level of contract also includes two kinds: ServiceContract and OperationContract

ServiceContract is used on a class or struct to instruct WCF that such a class or struct can be invoked remotely, and OperationContract is used on a method in a class to indicate that the WCF method can be invoked remotely.

Data Contract for custom structure: Contract

There are two types of data contracts: DataContract and datamember.datacontract are used on classes or structs to instruct WCF that such a class or structure can be serialized and transmitted, while datamember can only be used in the properties of a class or struct Or field, indicates that WCF this property or field can be serialized for transmission.

Exception contract for custom error exception: Fault Contract

Faultcontract is used to customize error exception handling, by default, when the service side throws an exception, the client can receive the description of the exception information, but these descriptions are often uniform format, sometimes more difficult to get useful information from, at this time, we can customize the format of the exception message, Passing messages We care about to the client in error messages requires adding a class to the method that customizes an error message, then adding faultcontract to the function to handle the exception, and indicating that the exception information is returned as a custom format.

Message contract to Control message format: Contract

Simply put, it can customize message formats, including message headers, message bodies, and whether to encrypt and sign message content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.