ServiceContract Attribute And Its Parameters in WCF

ServiceContract attribute

[ServiceContract] attribute is used to define a Service contract. This attribute is placed on an interface or a class , which you want to identify as a Service contract. The information specified by the parameters of ServiceContract attribute and its interface is being mapped with the <portType> element in WSDL (Web Service Description Language). 

  1. [ServiceContract]    
  2. public interface interfaceName    
  3.     
  4. {    
  5.          ……………………..    
  6.    
  7. }   

[ServiceContract] attribute parameters

[ServiceContract] attribute contains parameters, which specify additional information about ServiceContract attribute. The name of these parameters and their functions are given below.

CallbackContract

In case of two way (duplex) communication CallbackContract parameter gets or sets the callback contract for the client and in case of one way communication, it represents the outgoing calls from the Service to the client. The default value is null reference of this parameter.

Example 

  1. [ServiceContract(CallbackContract = typeof(callbackContractName))]  
  2.   
  3. public interface interfaceName  
  4.   
  5. {  
  6.   
  7.          ....................................  
  8.   
  9. }   
Name

This parameter gets or sets the name for <portType> element in WSDL. WSDL (Web Service Description Language) is an XML base format used for the Web Services, which defines the Service location (addresses or endpoints) and its methods. <portType> element in WSDL contains information about the operations, which can be performed and the messages that are involved. Its default value is the name of an interface to which it is applied.

Example 

  1. [ServiceContract(Name=”interfaceName”)]  
  2.   
  3. public interface interfaceName  
  4.   
  5. {  
  6.   
  7.          .................................  
  8.   
  9. }  

ConfigurationName

This parameter gets or sets the name, which is used to find the Service element in the configuration file. Its default value is the name of the class, which implements the Service contract.

Example

  1. [ServiceContract(ConfigurationName = “service”)]  
  2.   
  3. public interface IDemo  
  4.   
  5. {  
  6.   
  7.          ................................  
  8.   
  9. }   

The Application configuration file would look, as shown below.

  1. <configuration>  
  2.        <system.servicemodel>  
  3.           <services>  
  4.   
  5.              <service name = “Demo>  
  6.               ...................................
  7.              </service>  
  8.   
  9.           </services>  
  10.   
  11.        </system.servicemodel>  
  12.   
  13. </configuration>   

Namespace

This parameter gets or sets <portType> element namespace in WSDL. Its default value is "http://tempuri.org".

Example 

  1. [ServiceContract(Namespace=”http://www.Demo.com”)]  
  2.   
  3. public interface interfaceName  
  4.   
  5. {  
  6.   
  7.          ..................................  
  8.   
  9. }   
ProtectionLevel

This parameter is used for protection level bindings, which includes an encryption and digital signature. The value of this parameter can be set by following ProtectionLevel enumeration values.

ProtectionLevel Enumeration valuesBrief Descriptions
EncryptAndSign Encrypt and sign data to ensure the integrity and confidentiality
NoneOnly simple authentication
SignNo encryption only ensures integrity

The default value of this parameter is EncryptAndSign, if binding supports security and None, if bindings do not support security.

Example

  1. [ServiceContract(ProtectionLevel = System.Net.Security.ProtectionLevel.None)]  
  2.   
  3. public interface interfaceName  
  4.   
  5. {  
  6.   
  7.          .............................  
  8.   
  9. }   
SessionMode

This parameter specifies the supports of session for ServiceContract. The value of this parameter can be set by following SessionMode enumeration values given below.

SessionMode Enumeration values

Brief Descriptions
AllowedSpecifies session support for contract
NotAllowedSpecifies that contract does not support session
RequiredSpecifies that contract requires session all the time

Example

  1. [ServiceContract(SessionMode = SessionMode.Required)]  
  2.   
  3. public interface interfaceName  
  4.   
  5. {  
  6.   
  7.          ............................  
  8.