Introduction To WCF Endpoints

Background
 
WCF provides communication for client applications using Endpoints. Endpoints provide the configuration required for the communication and create the complete WCF service application.
If you are a beginner and want to learn about WCF then refer to the following articles of mine:
 
So let us learn about the WCF Endpoints. 
 
Endpoints

Endpoints are a combination of ABC, that is Address, Binding and Contracts. Let us look at the following diagram:

 
 

These Endpoints are used to configure the communication channel between the client application and the WCF service. This configuration is done in the Web.config file.

The Endpoints will look such as follows:

  1. <endpoint address="http://localhost:3901/Service1.svc"
  2.            binding="basicHttpBinding"  
  3.           contract="ServiceReference.IService1" bindingConfiguration="BasicHttpBinding_IService1"  
  4.               name="BasicHttpBinding_IService1" /> 
As discused, Endpoints consist of the following:
  1. Address
  2. Binding
  3. contracts 
Now let us learn about each Endpoint.
 
Addresses

An address is the URL that defines where the WCF service is hosted. Suppose I have hosted a WCF Service Application in my local IIS then the address will look such as follows.

 
Binding

Bindings are nothing but the transport protocols that are used for the communication with the client application. WCF provides a wide range of transport protocols that can be used as in the requirements.

A binding has the following characteristics:

  1. Transport: Defines the base protocol to be used, like HTTP, Named Pipes, TCP and MSMQ are some of the protocols.
  2. Encoding (Optional): Three types of encodings are available, they are:
  1. Text Encoding
  2. Binary Encoding
  3. Message Transmission Optimization Mechanism (MTOM)
       3. Protocol (Optional): Defines the information to be used in the binding such as Security, transaction or reliable messaging capability.
 
 

The following are some of the bindings that are widely used, depending on the requirements of the application:

  1. BasicHttpBinding: This is the default WCF binding and this basic protocol is similar to HTTP without security.
  2. WebHttpBinding: This binding is used when the service is communicating with a client application using WCF REST such as get or post.
  3. WSHttpBinding: This is a communication protocol with security similar to the HTTPS protocol.
  4. WSDualHttpBinding: Supports duplex contracts and transactions.
  5. WSFederationHttpBinding: Binding with federated security and transactions support.
  6. MsmqIntegrationBinding: Communication directly with MSMQ applications along with transaction support.
  7. NetMsmqBinding: Communication among WCF applications using queuing and transaction support.
  8. NetNamedPipeBinding: Communication among WCF applications on the same computer that support duplex contracts and transactions.
  9. NetPeerTcpBinding: Communication among computers across peer-to-peer services that  support duplex contracts.
  10. NetTcpBinding: Communication among WCF applications across computers, supports duplex contracts and transactions.

The preceding are the most commonly used bindings, however many other bindings are also supported by WCF. 

For example:

  1. <bindings>  
  2.           <BasicHttpBinding>  
  3.             <binding name="BasicHttpBinding_IService1" />  
  4.           </basicHttpBinding>  
  5.  </bindings> 
Contracts
 
Contracts are nothing but a collection of attributes that give special meanings to the methods and classes. Contracts are a collection of the following.
  1. Service contract
  2. Operation contract 
  3. Data contract
  4. Message contract
  5. Fault contract 

Now let us learn about each contract.

Service contract

Service contracts are nothing but definition of the interface for the service, those interfaces are considered to be a Service contract of the WCF service marked with the Service contract attribute.

It can be defined as follows:

  1. [ServiceContract]  
  2.  public interface IService1  
  3.  {  
  4.   
  5.      // TODO: Add your service operations here  
  6.  } 
The Service contract attribute has the following properties: 
  1. CallbackContract: Gets or sets the type of callback contract when the contract is a duplex contract.
  2. ConfigurationName: Gets or sets the name used to locate the service in an application configuration file.
  3. HasProtectionLevel: Gets a value that indicates whether the member has a protection level assigned.
  4. Name: Gets or sets the name for the <portType> element in the Web Services Description Language .
  5. Namespace: Gets or sets the namespace of the <portType> element in the Web Services Description Language.
  6. ProtectionLevel: Specifies whether the binding for the contract must support the value of the ProtectionLevel property.
  7. SessionMode: Gets or sets whether sessions are allowed, not allowed or required.
  8. TypeId: When implemented in a derived class, gets a unique identifier for this attribute.

Key Points of Service contract

  • Any WCF Service can have more than one Service contract.
  • It must declare at least one Service contract in a service.
  • The Service contract can be declared using the [ServiceContract] attribute.
  • It allows defining an Operation contract under it to expose the service outside the world.
  • It maps the interface and methods of your service to a platform-independent description.
  • It describes message exchange patterns that the service can have with another party. Some service operations might be one-way, others might require a request-reply pattern.

Operation contract

An Operation contract defines the method that is exposed to the client to exchange the information between the client and the server. An Operation contract describes what functionality is to be given to the client, such as addition, substraction and so on. The methods that are marked with the Operation contract attribute is termed an Operation contract.
It can be defined as:
  1. [OperationContract]  
  2.  string GetData(int value); 
An Operation contract has the same properties as a Service contract as follows:
  1. CallbackContract: Gets or sets the type of callback contract when the contract is a duplex contract.
  2. ConfigurationName: Gets or sets the name used to locate the service in an application configuration file.
  3. HasProtectionLevel: Gets a value that indicates whether the member has a protection level assigned.
  4. Name: Gets or sets the name for the <portType> element in the Web Services Description Language.
  5. Namespace: Gets or sets the namespace of the <portType> element in the Web Services Description Language.
  6. ProtectionLevel: Specifies whether the binding for the contract must support the value of the ProtectionLevel property.
  7. SessionMode: Gets or sets whether sessions are allowed, not allowed or required.
  8. TypeId: When implemented in a derived class, gets a unique identifier for this attribute.

Message contract

 
A Message contract provides an alternate way of creating and formatting the SOAP messages. By default WCF communicates with a client application using a SOAP message format but we can create a custom message format using the Message contract attribute. Message contracts are also defined in a manner similar to other contracts.
 
For example:
  1. [MessageContract]  
  2.        public class Employee  
  3.        {  
  4.            [MessageHeader]  
  5.            public string Name;  
  6.            [MessageBodyMember]  
  7.            public decimal salary;  
  8.          
  9.        } 

Data contract

A Data contract is similar to get and set properties of our normal programing but it defines how data types are serialized and desterilized. Using serialization, you convert an object into a sequence of bytes that can be transmitted over a network. Using deserialization, you reassemble an object from a sequence of bytes that you receive from a calling application.
 
Key points 
  • A Data contract is included in the System.Runtime.Serialization namespace.
  • A class marked with the Data contract attribute is termed a Data contract.
  • Members and Data contracts are marked as a Data Member.

For example:

  1. [DataContract]  
  2.   public class Student  
  3.   {  
  4.       private string _Name;  
  5.   
  6.       private string _City;  
  7.   
  8.   
  9.       [DataMember]  
  10.       public string Name  
  11.       {  
  12.           get { return _Name; }  
  13.           set { _Name = value; }  
  14.       } 
Fault contract

It is used to handle the exception and know the cause of the error that occured in the WCF service. By default when we throw an exception from a service, it will not reach the client side due to the lack of interoperability so for that WCF provides the option to handle and convey the error message to the client from the service using a SOAP Fault contract.
 
For example:
  1.  [ServiceContract]  
  2.     public interface IGetDetailsService  
  3.     {  
  4.         [OperationContract]  
  5.         [FaultContract(typeof(Student))]  
  6.         Student GetDetails(string Name);  
  7.     }  
  8.   
  9.     [DataContract]  
  10.     public class Student  
  11.     {  
  12.         private string _Name;  
  13.   
  14.         private string _City;  
  15.   
  16.         [DataMember]  
  17.         public string Name  
  18.         {  
  19.             get { return _Name; }  
  20.             set { _Name = value; }  
  21.         }  
  22.   
  23.         [DataMember]  
  24.         public string City  
  25.         {  
  26.             get { return _City; }  
  27.             set { _City = value; }  
  28.          }  
  29. }
Summary
 
From the receding examples and explanations we have learned about the WCF endpoints. In my next article we will learn in detail about each endpoint. I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all