ServiceContractAttribute in WCF

ServiceContractAttribute in WCF 

 
A Service Contract defines the operations that a service will perform when executed. It is a collective mechanism by which we can specify the consumer's requirements. It tells the outside world about its messages, its location and protocols which it can use for communication. It takes the form of a .Net interface or class.
 
Now we discuss the ServiceContractAttribute Class. It indicates the class and interface that defines a service contract in an application. It is in the System.ServiceModel namespace.
 
Here we take an example of a Service Contract by which we can specify it.
  1. using System.ServiceModel;  
  2.   
  3. [ServiceContract]  
  4. public interface IService  
  5. {  
  6.     [OperationContract]  
  7.     double Plus(double x1, double x2);  
  8. }  
  9.  public class CalculatorService : IService   
  10. {  
  11.     public double Plus(double x1, double x2)  
  12.     {  
  13.         return x1 + x2;   
  14.     }  
  15. }  
The ServiceContract Attribute can be declared without any parameters, but it can also take the named parameters.

Named Parameters of ServiceContract Attribute


(a) CallbackContract: It associates another service as a Callback contract. It is when the contract is in the duplex Format.
  1. using System.ServiceModel;  
  2. [ServiceContract(CallbackContract = typeof(IMyReply))]  
  3. public interface ICalculator  
  4. {  
  5.     [OperationContract(IsOneWay = true)]  
  6.     double Add(double n1, double n2);  
  7. }  
  8. public interface IMyReply  
  9. {  
  10.     [OperationContract(IsOneWay = true)]  
  11.     void Reply(string responseofplus);  
  12. }  
Here IMyReply implements a one-way callback method; it enables the service to call the client without waiting for a reply.
 
(b) Name: It specifies the Name of the ServiceContractAtrribute.
 
[ServiceContract(Name="MyServiceContract" ,CallbackContract = typeof(IMyReply))]
 
(c) NameSpace: It specifies the Namespace property of the ServiceContractAtrribute.
 
[ServiceContract(Name = "MyServiceContract", Namespace = "http://Microsoft.WCF.Documentation")]
 
(d) ConfigurationName: It specifies the name which is used to locate the service element in the application configuration file. The default name is the name of the service implementation class.
 
(e) HasProtectionLevel: It specifies that the member has a protection level or not.
 
(f) ProtectionLevel: It specifies the constraints on messages of all operations in the contract; whether they are protected on the wire or whether they are signed or protected.
 
(g) SessionMode: This property is used to set the way of exchanging the messages between two or more Endpoints. It specifies whether the sessions are enabled by the Endpoint.

[ServiceContract(Name = "MyServiceContract", Namespace = "http://Microsoft.WCF.Documentation", SessionMode = SessionMode.Required)]


Similar Articles