Rookie eyes WCF (2)-Service contract

Source: Internet
Author: User

in the previous article , we briefly explained WCF and made a simple example, so we will continue to parse it today--a preliminary discussion of the service contract.

When it comes to the agreement we are certainly not unfamiliar, as long as the work will be signed a variety of agreements, such as the signing of the contract, we are one of them, according to the contract signed by the two parties to work, it grants you what authority, you have what authority, you can not do the job outside the work. Would you like to consider the agreement on this contract, like the service agreement we're talking about here? Yes, the service contract is really nothing. The individuals and companies that have just signed the cooperation agreement are called "clients" and "Servers" respectively.

The service contract tells the client, which methods you can invoke, what data types you can use, and the public-private interface that makes you call, depends entirely on how the server-side defines the service contract. Just come down to experience some of the meaning of the Protocol through examples.

First, instance one

For example, the following agreement:

<span style= "FONT-SIZE:18PX;" >[servicecontract] public Interfaceiservicea {     [operationcontract]     int addint (int a, int b);      [OperationContract]     Double adddouble (double A, double b);      Float Addfloat (float A, float b);  Implement the contract: public Classmyservice:iservicea {public     int addint (int a, int b)     {         return a + b;     }      Public double adddouble (double A, doubleb)     {         return a + b;     }      public float addfloat (float A, FLOATB)     {         return a + B;     }} </span>

You can guess, in the code generated in the client, which methods you can see, which methods will not be seen? In the above service contract, we define three methods, of which the first two are with the operation contract OperationContractAttribute, and the Addfloat method is not labeled as contract. So what happens to the generated client code?

is there a method missing? Yes, it's just a little addfloat, remember? In our code above, the Addfloat method is not labeled as an operation contract.

Second, case two optimization comparison

Now let's add an operation contract for Addfloat, as shown below.

Public Interfaceiservicea {     [operationcontract]     int addint (int a, int b);      [OperationContract]     Double adddouble (double A, double b);      [OperationContract]     

At this point , see how many methods you can see on the client. Whether the discovery method can be displayed.

Iii. Summary

Through the above example we will find that if the operation contract OperationContract is defined, then this method can be called by the client, and vice versa. This reminds us that the service agreement must be set in combination with the needs of the project to maximize its value.

Rookie eyes WCF (2)-Service contract

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.