System.ServiceModel is the Core Library of WCF service. WCF provides many attributes as part of its library which makes a normal component as WCF enabled component (actually attribute is a Class).
WCF recommends interface based programming, that means it exposes its method through interface. If we are not using the interfaces while developing the service, still it will work but you will miss some of the WCF features. By using Interfaces we can get Rule-Based development(by defining Contracts/rules, also useful in Polymorphism behaviour), Exchange information in a proper way.
ServiceContract, OperationContract are the examples of attributes provided by the WCF library System.ServiceModel.dll.
ServiceContract
Using ServiceContract attribute we can mention C# interface/Class as WCF enabled. Without using the ServiceContract attribute, that interface/Class is a normal type which can be consumed within the CLR environment and not available for client which is calling from outside.
Eg:
using System.ServiceModel;
namespace WcfService1
{
[ServiceContract]
public interface IEmployee
{
}
}
As shown above we are applying the ServiceContract attribute for interface, even we can apply the ServiceContract attribute for Class also. By applying the ServiceContract attribute, we are making the normal interface/class as WCF enabled interface/class and any one can consume the service even from outside of the CLR environment.
OperationContract:
All methods which should be accessible through WCF runtime should be added with the OperationContract attribute. That means if you want to expose any of your methods to the client through WCF service you have to apply the OperationContract attribute. By applying the OperationContract attribute WCF can add all bindings and behaviours required for Client.
Generally according to SOA(Service Oriented Architecture), methods are only accessible points in a Service. In WCF also Client can call only methods which are added by the OperationContract attribute and which are in the ServiceContract attribute applied interface/class.
For example you have a ServiceContract attribute applied class/interface which has 10 methods, but you want to expose only 5 methods for Client, for that just add the OperationContract attribute for only those 5 methods which you want to expose to client.
Eg:
using System.ServiceModel;
namespace WcfService1
{
[ServiceContract]
public interface IEmployee
{
[OperationContract]
string GetEmpName(int Id);
[OperationContract]
string GetEmpRole(int Id);
string GetEmpSal(int Id);
}
}
As shown above, we have ServiceContract attributed added Interface IEmployee which has three methods. First two methods GetEmpName(), GetEmpRole() methods are added with OperationContract attribute, so Client can call these methods. We are restricting the last method GetEmpSal() access from client by not applying the OperationContract attribute, so client can not able to call that method.