WCF design and implementation service agreement (01), wcf agreement

Source: Internet
Author: User

WCF design and implementation service agreement (01), wcf agreement
WCF terminology:
• Message
-A message is an independent data unit. It may consist of several parts, including the message body and message header.
• Service
-A service is a constructor that exposes one or more endpoints. Each endpoints exposes one or more service operations.
• Endpoint
-An endpoint is a structure used to send or receive messages (or perform these two operations. An endpoint includes a location (Address) that defines the destination to which a message can be sent, and a communication mechanism specification (binding) that describes how a message should be sent) the definition (Service Agreement) of a group of messages that can be sent or received (or both) at this location also describes the messages that can be sent.

-The WCF Service is made public to the outside world as a collection of endpoints.

Note:

The WCF Service is a set of methods provided by the server for the client. These methods can be in a class or in different classes. Endpoints refer to these classes, that is, the portals of a series of methods.



Create a service agreement:

• Service Agreements can be defined for classes or interfaces
• We recommend that you use interfaces because they can be directly modeled on service agreements.
• A service agreement interface has all the advantages of a hosted interface:
-The service agreement interface can expand any number of other service agreement interfaces.
-A class can implement any number of service agreements by implementing the service agreement interface.
-You can modify the implementation of a service agreement by modifying the interface, so that the service agreement remains unchanged.
-You can determine the service version by implementing the old interface and new interface. The old client connects to the original version, while the new client can connect to the new version.


• Define service agreements
-Use ServiceContractAttribute tag on a class or interface


• Define service operations
-Use the OperationContractAttribute to mark a method.


• Parameters and return values
-Each operation has a return value and a parameter, even if they are void. You can use the local method to pass references to an object from one object to another. However, unlike the local method, service operations do not pass references to the object, they pass only copies of objects.
-This is important because every type used in parameters or return values must be serializable. In other words, objects of this type must be converted to byte streams, it can also be converted from byte to object.
-By default, primitive types are serializable, and many types in. NET Framework are serializable.


Create a message mode for service agreement service operations

1. Request/reply Mode
-In the request/reply mode, the request sender (client application) will receive a response related to the request. This is the default mode, because it supports both input operations (one or more parameters are passed to this operation ), the return operation is also supported (this operation returns one or more output values to the caller)

[OperationContract]string Hello(string greeting);
-Note: unless other Basic message modes are specified, even if the service operation returns void (Nothing in Visual Basic), it is also a request/reply message exchange.
-The result of the operation is: unless the client calls the operation asynchronously, the client stops processing until it receives the returned message, even if the message is normally empty.
Disadvantages
-If the operation takes a long time, the client performance and response capabilities will be reduced.
• Advantages
-A soap error may be returned in the Response Message, indicating that some service-related errors may occur during communication or processing.

2. unidirectional mode

-If the client of the WCF Service application does not have to wait for the Operation to complete and does not handle SOAP errors, this operation can specify the unidirectional message mode.
-One-way operations are operations that the client calls and continues to process after WCF writes messages to the network. This usually means that, unless the data sent in the outbound message is extremely large, the client will continue running almost immediately (unless an error occurs when sending the data ). This type of message exchange mode supports event-like behavior from the client to the service application.
-To specify one-way message exchange for operations that return void, set the IsOneWay attribute to true. The default value is false.

[OperationContract(IsOneWay=true)]void Hello(string greeting);
This method is the same as the previous request/response example. Setting the IsOneWay attribute to true means that even though the method is the same, the service operation will not send a response message, the client will return immediately when the outbound message arrives at the channel layer.


3. duplex mode

-The duplex mode is characterized by either one-way message sending or request/reply message sending,Both the service and client can send messages to each other independently.. This bidirectional communication mode is useful for services that must communicate directly with the client or provide an asynchronous experience to any party that exchanges messages, including event-like behavior.
-Because there is an additional mechanism for communication with the client, the bidirectional mode is slightly more complex than the request/reply or unidirectional mode.
-To design a duplex protocol, you must also design a callback protocol and assign the type of the callback protocol to the CallbackContract attribute (property) of the ServiceContractAttribute attribute that marks the service agreement ).
-To implement the duplex mode, you must create the second interface, which contains the method declaration called by the client.

[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples",SessionMode=SessionMode.Required,CallbackContract=typeof(ICalculatorDuplexCallback))]public interface ICalculatorDuplex{[OperationContract(IsOneWay = true)]void Clear();} public interface ICalculatorDuplexCallback{ [OperationContract(IsOneWay = true)]void Equals(double result);[OperationContract(IsOneWay = true)]void Equation(string eqn);}

Note:

• Service-oriented applications (such as Windows Communication Foundation (WCF) Applications) are designed to interoperate with the maximum number of client applications on the Microsoft platform and non-Microsoft platform.
• To maximize interoperability, we recommend that you use the DataContractAttribute and DataMemberAttribute attributes to tag your type to create a data contract.
• Data agreements are part of service agreements used to describe the data exchanged for your service operations.

• Data agreements are optional style agreements: No types or data members are serialized unless you explicitly apply the data agreement Properties
• Data protocols are unrelated to the access scope of hosted code. Private Data members can be serialized and sent to other locations for public access.
• WCF processes the definition of the basic SOAP message used to enable the operation function, and serializes the data type to the message body and deserializes the message body. Once the data type is serialized, you do not need to consider the basic message exchange infrastructure during design operations.
• Other serialization mechanisms can be used. Standard ISerializable, SerializableAttribute, and IXmlSerializable mechanisms can be used to serialize data types to basic SOAP messages that can be taken from one application to another.


Out and Ref Parameters

• In most cases, you can use the in parameters (ByVal in Visual Basic), out, And ref parameters (ByRef in Visual Basic ). Because both the out and ref parameters indicate that the data is returned from the operation, the Operation signature similar to the following will specify the request/reply operation, even if the operation signature returns the void

[ServiceContract]public interface IMyContract{[OperationContract]void PopulateData(ref CustomDataType data);}
• The modified object can be returned only when the out or ref parameter is used to require the operation to have a basic response message. If the operation is a one-way operation, the InvalidOerationExcetion exception will be thrown during the operation.


Note:

This article is based on: Xu Changlong and I have been learning the WCF series from scratch (2): designing and implementing a service agreement (Level 200) Tutorial


Copyright Disclaimer: Author: jiankunking Source: http://blog.csdn.net/jiankunking this article copyright to the author and CSDN a total, welcome to reprint, but without the author's consent must retain this statement, and in the Article Page clearly given the original connection.

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.