Art of WCF 2, design and implement service agreements

Source: Internet
Author: User

Servicecontract)

First, I understand several terms:

Message: an independent data unit, including the body and message header.
Endpoint: used to send or receive messages. A WCF Service is exposed to the outside world as an endpoint.
Service: A service is a structure that exposes one or more endpoints, including one or more service operations.

Create a service agreement:

Classes and interfaces can be defined, but interfaces are recommended for modeling. This is because the service agreements defined by interfaces can be easily extended by other service agreements, it also facilitates the implementation of other service protocols for ease of modification and version updates.

Define service agreement: Use servicecontractattribute tag.
Define service operation: Use the operationcontractattribute tag.
Parameters and return values: each operation has a return value and a parameter, even void. They pass object copies between the client and server, rather than object references, and must be serializable.

[Servicecontract (namespace =" Http://Microsoft.ServiceModel.Samples ")] // Service Contract  Public   Interface Icalculator {[operationcontract] // Operation contract  Double Add ( Double N1, Double N2); [operationcontract] // Operation contract  Double Subtract ( Double N1, Double N2); [operationcontract] // Operation contract  Double Multiply ( Double N1, Double N2); [operationcontract] // Operation contract  Double Divide ( Double N1, Double N2 );}

Three Message modes of service operation:

1. The request/reply mode (default mode) supports incoming and return operations. If the client adopts the synchronous call mode, it will be congested until the service is returned.

 
[Operationcontract]StringGetdata (StringPara );

2. In one-way mode, the client does not wait for the Operation to complete or handle the soap error message. Generally, the operation used to return the void is specified as one-way mode.

 
[Operationcontract (isoneway =True)] Hello (StringPara );

3. duplex mode: When communication must be directly with the other party, a callback protocol can be designed to implement the duplex mode. At this time, both parties can independently send information to the other party.

[Servicecontract (namespace = "http: // Microsoft. servicemodel. samples ", Sessionmode = sessionmode. required, callbackcontract =Typeof (Icalculatorduplexcallback)] // The callback protocol must be an object of the specified type.  Public   Interface Icalculatorduplex {[operationcontract (isoneway = True )] Clear (); [operationcontract (isoneway = True )] Add ( Int Para );} Public   Interface Icalculatorduplexcallback {[operationcontract (isoneway = True )] Void Equals (Double Result); [operationcontract (isoneway = True )] Void Prediction ( String Eqn );}

Method for implementing the interface service agreement:

 
Icalculatorduplexcallback callback = operationcontext. Current. getcallbackchannel <icalculatorduplexcallback> ();

In this way, you can obtain a copy of the object in the callback of the other party, and you can conveniently call the method in this interface through callback. Note that the implementation here is in the other party rather than in the local area.
When the client calls the add method of the server, it can call back the related methods in the icalculatorduplexcallback subclass implemented by the client in the add method, so that both parties can call the classes and methods of the other party.

Out and ref parameters: these two parameters cannot be used in unidirectional mode. When using ref parameters, the input parameters must be initialized first. For out, initialization must be completed in the method.

 

<End>

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.