Introduction to Endpoint in WCF: Part 2

In this article, we learn what an endpoint is and how we will implement them in a WCF application.

WCF Endpoints

 
Every service has a unique address and this unique address has the three attributes Address, Binding, and Contract. In other words, we can say an endpoint is a combination of address, binding, and contract. If you are unfamiliar with address, binding and contract then please read my previous article Introduction to WCF: Part 1. In this article, I will give you a short note about these three attributes of endpoints.
  • Address: contains information about where a service can be found.

  • Binding: contains information about how a client can communicate with a service.

  • Contract: It's an agreement between service and client.

    There is one more property in an endpoint, behavior. Behavior is not an attribute of an endpoint.

  • Behavior: A set of behaviors that specify local implementation details of the endpoint.
Let me describe using an example. I am using my previous demo project.
 
In ImyService.cs
  1. namespace myFirstApp  
  2. {  
  3.     [ServiceContract]  
  4.     public interface IMyService  
  5.     {  
  6.         [OperationContract]  
  7.         int AddTwoNo(int intFirstNo, int intSecondNo);  
  8.     }  
  9. }  
In MyService.svc.cs  
  1. namespace myFirstApp    
  2. {    
  3.     public class MyService : IMyService    
  4.     {    
  5.         public int AddTwoNo(int intFirstNo, int intSecondNo)  
  6.         {  
  7.             return intFirstNo + intSecondNo;  
  8.         }  
  9.     }    
  10. }  
Endpoint using web.config
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.   <appSettings>  
  4.     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  
  5.   </appSettings>  
  6.   <system.web>  
  7.     <compilation debug="true" targetFramework="4.5" />  
  8.     <httpRuntime targetFramework="4.5"/>  
  9.   </system.web>  
  10.   <system.serviceModel>  
  11.     <services>  
  12.       <service name="myFirstApp.MyService" behaviorConfiguration="serviceBehaviour">  
  13.     <!--endpoint start here-->  
  14.         <endpoint address="addTwo" binding="basicHttpBinding" contract="myFirstApp.ImyService"></endpoint>  
  15.     <!--endpoint end here-->  
  16.       <host>  
  17.         <baseAddresses>  
  18.           <add baseAddress="http://localhost:36246/"/>  
  19.         </baseAddresses>  
  20.       </host>  
  21.       </service>  
  22.     </services>  
  23.     <behaviors>  
  24.       <serviceBehaviors>  
  25.         <behavior name="serviceBehaviour">  
  26.           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>  
  27.           <serviceDebug includeExceptionDetailInFaults="false"/>  
  28.         </behavior>  
  29.       </serviceBehaviors>  
  30.     </behaviors>  
  31.     <protocolMapping>  
  32.         <add binding="basicHttpsBinding" scheme="https" />  
  33.     </protocolMapping>      
  34.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
  35.   </system.serviceModel>  
  36.   <system.webServer>  
  37.     <modules runAllManagedModulesForAllRequests="true"/>  
  38.     <directoryBrowse enabled="true"/>  
  39.   </system.webServer>  
  40. </configuration>  
Let's update my service reference.
 
update service reference 
 
In default.aspx
  1. <table>  
  2.         <tr><td>First No</td><td><asp:TextBox ID="txtFirst" runat="server"></asp:TextBox></td></tr>  
  3.         <tr><td>Second No</td><td><asp:TextBox ID="txtSec" runat="server"></asp:TextBox></td></tr>  
  4.         <tr><td colspan="2"><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /></td>  
  5.         </tr>  
  6.         <tr><td colspan="2"><asp:Label ID="lblResult" runat="server"></asp:Label></td></tr>  
  7.     </table>  
In default.aspx.cs
  1. using System;  
  2.   
  3. namespace WCFClientApp  
  4. {  
  5.     public partial class _default : System.Web.UI.Page  
  6.     {  
  7.         protected void btnAdd_Click(object sender, EventArgs e)  
  8.         {  
  9.             int intFirstNo = 0, intSecNo = 0, intResult = 0;  
  10.             intFirstNo = Convert.ToInt16(txtFirst.Text);  
  11.             intSecNo = Convert.ToInt16(txtSec.Text);  
  12.             WCFReference.MyServiceClient client = new WCFReference.MyServiceClient();  
  13.             intResult = client.AddTwoNo(intFirstNo, intSecNo);  
  14.             lblResult.Text = "Result is :"+intResult;  
  15.             client.Close();  
  16.         }  
  17.     }  
  18. }  
Run the application and get the expected results.
 
I hope this article is helpful to you.