Hi,
What is the difference between Service Contracts and Operation Contracts in WCF? Explain with some example.
Thanks.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Pravin MorePosted Sep 21, 2011, 12:46 AM
basic about ServiceContract and OperationContract is.....
ServiceContract:-
service contract is at Class ans Interface Level.You can Define Classes and Interfaces in [ServiceContract] like below....
public namespace WCF_Service
{
[ServiceContract]
public class Sevice Class
{
}
}
Operation Contract:-
Operation contract is at Operation Level(Function).You can Define Methods in [OperationContract] like below.... and Opeation Contract can have [MessageContract] also.....
public namespace WCF_Service
{
[ServiceContract]
public class Sevice Class
{
[OperationContract]
public getData()
{
//Some Code
}
}
}
this is basuc about service and operation contract...hope it will help you.
Thanx,
Pravin.
Posted Sep 21, 2011, 12:39 AM
Where as,Operation Contract means it is the name of the attribute which is applied to a method inside the interface of a wcf service which is similar to the [WebMethod] in WebService.
this means, ServiceContract attribute is similar to the [WebServcie] attribute in the WebService and [OpeartionContract] is similar to the [WebMethod] in WebService.
Example :
[ServiceContract()]
public interface IFullname
{
[OperationContract()]
String Add(string FirstName, string Lastname);
}
Once we define Service contract in the interface, we can create implement class for this interface.
public class Fullname : IFullname
{
public String Add(string FirstName, string Lastname)
{
return FirstName + Lastname;
}
}