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.