WCF Learning (ii): Service Contracts

Source: Internet
Author: User

Introduction of service Contract

I personally understand that a service contract is a set of exposed operations in which the exposed action (OperationContract) can only be defined on the method. For the services we want to expose, we can add an identifier ServiceContract to the interface or class. But we generally define servicecontract on interfaces rather than classes, and there are several benefits:

1. Facilitate the succession of contracts, different types can achieve the same contract, high reusability.

2. The same service can be used to implement multiple contracts.

3. You can modify the service type at any time without having to modify the interface.

The following defines a contract for a service:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.ServiceModel;
Namespace Service
{
[ServiceContract (name= "Service_calucator", namespace= "Henllyee")]
public  Interface Icalucator
{
[operationcontract]
int Add (int x, int y);
}
}    

We first defined an interface name as: Icalucator, and then we want to expose this interface as a service contract with the attribute identifier [ServiceContract], where name can specify an alias for the contract, so that If we encounter the same interface on the client side, we can distinguish it by using name to create an alias. The exposed action is the Add method, which identifies [OperationContract].

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.