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].