Endpoint-Contracts
In WCF, all services expose constracts. The contract is a platform-neutral(平台无关的) and standard way of describing what the service does. WCF defines four types of contracts.
Service contracts
Describe which operations the client can perform on the service.
Data contracts
Define which data types are passed to and from the service. WCF defines implicit (隐式的)contracnts for built-in types such as int and stirng, but we can define explicit data contracts.
Fault contracts
define which errors are raised by the service, and how the service handles and propagates (繁殖,传送)errors to its clients.
Message contracts
Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format you have to comply with. as a WCF developer, we should use message contracts only rarely. :)
Example: defining and implementing a service contract
[ServiceContract]
interface IMyContract
{
[OperationContract]
string Hello();
//will not be part of the contract
string otherHello();
}
public class MyService:IMyContract
{
public string Hello()
{
return "this is a WCF Service";
}
//this method is not exposed as a service.
public string otherHello()
{
return "can not call this method over WCF";
}
}
Names and namespaces
we can and should define a namespace for our contract. if it 's unspecified, the contract namespace defaults to http://tempuri.org. we can use Namespace property of the ServiceContract to provide a namespace.
[ServiceContract(Namespace="myspace")]
By default, the exposed name of the contract will be the name of the interface used. However, you could use an alias for a contract to expose a different name to the clients using the name property of the servicecontract
[servicecontract(Name="IMyContract")]
interface IMyOtherContract
{...}
In similar manner, the name the publicly exposed operation defaults to the method name, but u can use the Name property ot the operationContract to alias it to a different publicly exposed name:
[OperationContract(Name="SomeOperatin")]
string Hello();
Service contracts
Describe which operations the client can perform on the service.
Data contracts
Define which data types are passed to and from the service. WCF defines implicit (隐式的)contracnts for built-in types such as int and stirng, but we can define explicit data contracts.
Fault contracts
define which errors are raised by the service, and how the service handles and propagates (繁殖,传送)errors to its clients.
Message contracts
Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format you have to comply with. as a WCF developer, we should use message contracts only rarely. :)
Example: defining and implementing a service contract
[ServiceContract]
interface IMyContract
{
[OperationContract]
string Hello();
//will not be part of the contract
string otherHello();
}
public class MyService:IMyContract
{
public string Hello()
{
return "this is a WCF Service";
}
//this method is not exposed as a service.
public string otherHello()
{
return "can not call this method over WCF";
}
}
Names and namespaces
we can and should define a namespace for our contract. if it 's unspecified, the contract namespace defaults to http://tempuri.org. we can use Namespace property of the ServiceContract to provide a namespace.
[ServiceContract(Namespace="myspace")]
By default, the exposed name of the contract will be the name of the interface used. However, you could use an alias for a contract to expose a different name to the clients using the name property of the servicecontract
[servicecontract(Name="IMyContract")]
interface IMyOtherContract
{...}
In similar manner, the name the publicly exposed operation defaults to the method name, but u can use the Name property ot the operationContract to alias it to a different publicly exposed name:
[OperationContract(Name="SomeOperatin")]
string Hello();