· Service Contract: It defines what a client can do with the service.
· Data Contract: It defines which data types can be transferred in the service. ( by default you can pass basic types like int, string however for custom types like a custom class you need to define the class as a Data Contract to be able to pass object of that class in the service)
· Fault Contract: It defines what are the errors raised by service.
· Message Contract: It is an extra functionality allows you to send message in a customized way (It means that you need to send a message in a specific way because let’s say there is an existing system that just understands messages in a specific way. As a user of WCF you should not usually use this as you do not need it except you are working with very specific system that needs specific messaging system)
Service Contract:
To allow a WCF Client to understand what they can do with the service you need to use Service Contract which is an attribute you need to add to the class. Also for each method that you wish WCF client to access you need add OperationContract attribute take a look at this sample
[ServiceContract]
Class MyService
{
[OperationContract]
public string MyOperation()
{
return “hello World”;
}
}
As you see in above example we have ServiceContract for the class and we have OperationContract for the methods. Some Points:
· You can apply ServiceContract to a class or interface
· You have to apply OperatoinContract for methods otherwise it will not be accessible to clients
· You can only apply OperationContract to methods
· It is highly recommended first you better create an interface and apply the servicecontract then create a class that implements the interface (the reason will be discussed further)
· Methods accessibility is not important to WCF (public, private or internal) since they are CLR concepts not WCF
· Always the default constructor will be called (avoid parameterized constructors)
· You can define a Namespace for the ServiceContract (and it is suggested to do so) to avoid collision
Please see the same sample by using interface and namespace
[ServiceContract(Namespace=”mycompany.com”)]
interface IMyService
{
[OperationContract]
string MyOperation();
}
Class MyService:IMyService
{
public string MyOperation()
{
return “hello World”;
}
}