A Brief Introduction to the WCF contract (service contract data contract message Contract)

Source: Internet
Author: User
ArticleDirectory
    • Service Contract
    • Data contract
    • Message contract

This blog post briefly describes the types, functions, and some simpleCodeExample. In WCF, contracts are classified into service contracts, data contracts, and message contracts. The following is a brief introduction to these types of contracts.

Service Contract

The service contract describes the types (interfaces or classes) exposed to the outside, the operations supported by the Service, the message exchange mode used, and the message format. Each WCF Service must implement at least one service contract. To use a service contract, you must reference the namespace.System. servicemodel. Three features commonly used in service contracts:

Servicecontractattribute

This feature is defined on a class or interface to describe a set of related operations. Servicecontractattribute has the following attributes that can be set:

Name: defines a name for the service contract, instead of the type name of the interface or class, which is used when the client adds a service reference.

Namespace: namespace, http://tempuri.org by default.

Callbackcontract: defines another service contract as a callback contract so that the client can receive asynchronous messages from the server.

Protectionlevel: controls whether the protection mode sent to messages in the contract needs to be signed and encrypted. This attribute is of the system. net. Security. protectionlevel Enumeration type.

Sessionmode: determines whether a session is supported by the endpoint of the public service contract.

Configurationname: service configuration name.

[Servicecontract(Name ="Helloworld", Namespace =Http://oec2003.cnblogs.com"]Public interfaceIhelloworldservice{}
Operationcontractattribute

The method marked with operationcontractattribute is a service operation. Simply using this feature, a method can be added to the operation queue of the service contract and called by the client. This feature also has some attributes for setting the message exchange mode. As follows:

Name: defines an operation name instead of a method name.

Action: The action title of the Operation message.

Replyaction: the title of the action that responds to the operation message.

Isoneway: sets whether the operation is unidirectional and no reply. If the operation is unidirectional, replyaction is not supported.

Protectionlevel: allows you to control whether a specific operation message is protected. The protectionlevel attribute in the operation will overwrite the protectionlevel in the service contract. This attribute is of the system. net. Security. protectionlevel Enumeration type.

Isinitiating: whether an operation can be used to initialize a session.

Isterminating: Indicates whether to abort a session.

Asyncpattern: defines service operations as Asynchronous implementation modes.

[Servicecontract(Namespace =Http://oec2003.cnblogs.com", Name ="Oec2003", Configurationname ="Ihelloworldservice")]Public interfaceIhelloworldservice{[Operationcontract(Name ="Oec2003sayhello", Action =Http://oec2003.cnblogs.com/IHelloWorldService/Hello", Replyaction =Http://oec2003.cnblogs.com/IHelloWorldService/HelloReply")]StringSayhello ();}
Messageparameterattribute

Messageparameterattribute can be used to control the name of a parameter or return value. This attribute is invalid for parameters marked with the messagecontractattribute feature. This feature has only one name attribute. See the following code:

[Servicecontract(Namespace =Http://oec2003.cnblogs.com", Name ="Oec2003", Configurationname ="Ihelloworldservice")]Public interfaceIhelloworldservice{[Operationcontract] [Return:Messageparameter(Name ="Responsestring")]StringSayhello ([Messageparameter(Name ="String")]StringMEG );}

 

Data contract

To use a data contract, you must reference the system. runtime. serialization namespace. You can use datacontractattribute to create a data contract on the type. The members of the type are marked with datamember. The Code is as follows:

[Datacontract]Public classUser{[Datamember]Public intAge {Get;Set;}[Datamember]Public StringName {Get;Set;}[Datamember]Public StringEmail {Get;Set;}}
Datacontractattribute

The datacontractattribute attribute is defined on top of the type, including class, structure, enumeration, but not interfaces. The datacontractattribute feature cannot be inherited. That is, the class that inherits its own datacontractattribute feature tag is not a data contract. It must be marked with datacontractattribute to become a data contract. Datacontractattribute has three attributes: isreference, name, and namespace:

Isreference: bool type, indicating whether to maintain the existing reference structure of the object during serialization.

Name: Name.

Namespace: namespace.

[Datacontract(Isreference =True, Name ="Myuser", Namespace =Http://oec2003.cnblogs.com")]Public classUser{[Datamember]Public intAge {Get;Set;}[Datamember]Public StringName {Get;Set;}[Datamember]Public StringEmail {Get;Set;}}
Datamemberattribute

Only data members marked with datamemberattribute can become data members of the Data contract. This is similar to operationcontractattribute in the service contract. The datamemberattribute feature has the following four attributes:

Emitdefavalue value: Indicates whether to serialize a data member to the final XML when its value is equal to the default value. The default value is true, indicating that the default value will participate in serialization.

Isrequired: bool type, indicating whether the attribute member is a required member. The default value is false.

Name: the alias of the data member.

Order: the position where the corresponding data members appear in the final serialized XML. By default, the data members are listed alphabetically.

[ Datacontract (Isreference = True , Name = "Myuser" , Namespace = Http://oec2003.cnblogs.com" )] Public class  User {[ Datamember (Emitdefaultvalue =True , Isrequired = True , Name = "Oec2003_age" , Order = 1)] Public int Age { Get ; Set ;}[ Datamember (Emitdefaultvalue = True , Isrequired = True , Name = "Oec2003_name" , Order = 2)] Public String Name { Get ;Set ;}[ Datamember (Emitdefaultvalue = True , Isrequired = False , Name = "Oec2003_email" , Order = 3)] Public String Email { Get ; Set ;}

 

Message contract

the system. servicemodel namespace must be referenced when a message contract is used. The message contract and data contract are both defined on the data type. Different from the data contract, the message contract focuses more on the representation of data members in the SOAP message. Messagecontractattribute is required to define a message contract. messageheaderattribute and messagebodymemberattribute are also involved. messagecontractattribute is marked on the type, and messageheaderattribute and messagebodymemberattribute are marked on the data members.

[Messagecontract]Public classMessagetest{[Messageheader]Public intAge {Get;Set;}[Messageheader]Public StringName {Get;Set;}[Messagebodymember]Public StringEmail {Get;Set;}}
Messagecontractattribute

You can use the messagecontractattribute tag on a type to make it a message contract. The messagecontractattribute feature contains the following attributes:

Iswrapped: whether to add an additional root node to a defined principal member (one or more.

Wrappername: name of the root node.

Wrappernamespace: namespace of the root node.

Protectionlevel: indicates the protection level. In WCF, The system. net. Security. protectionlevel enumeration is used to define the protection level of a message. There are generally three optional protection levels: None, sign, and encryptandsign.

 
[Messagecontract(Iswrapped =False, Wrappername ="Mymessage", Wrappernamespace =Http://oec2003.com")]Public classMessagetest{// Omitted}
Messageheaderattribute

 

The data member marked with messageheaderattribute will appear in the header of the SOAP message. This feature includes the following attributes:

ACTOR: a URI value that indicates the target node that processes the header.

Mustunderstand: bool type, indicating whether the actor-defined node must understand and process the node.

Name: Name.

Namespace: namespace.

Protectionlevel: indicates the protection level.

Relay: indicates whether the header needs to be transmitted to the next soap node.

 
[Messagecontract]Public classMessagetest{[Messageheader(Actor =Http://oec2003.com/Age", Mustunderstand =True, Name ="Myage", Namespace =Http://oec2003.com", Relay =True)]Public intAge {Get;Set;}[Messageheader]Public StringName {Get;Set;}}
Messagebodymemberattribute

The data member marked with messageheaderattribute will appear in the main part of the SOAP message. This feature includes the following attributes:

Order: The order attribute is used to control the position of a Member in the soap body section. By default, the members are listed alphabetically.

Name: Name.

Namespace: namespace.

Protectionlevel: indicates the protection level.

 
[Messagecontract(Iswrapped =False, Wrappername ="Mymessage", Wrappernamespace =Http://oec2003.com")]Public classMessagetest{[Messagebodymember(Order = 1)]Public StringEmail {Get;Set;}}

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.