Default Endpoint Case Scenario Implementation in WCF 4.0 Hosted on Web App

Today, in this article let's learn one of the most interesting and top most feature of WCF 4.0. This Implementation of Default Endpoint made life easy and simplified process of implementation. This concept is highly attracted by many and many organizations love to use it.

So, let's get ready to start it off.

Question arises: What is an Endpoint?

Let's Break Up and Understand answer for this clearly

  Default Endpoint Zero.png

Figure 1: Describing Endpoint Diagrammatically.

In Simple terms looking at this above figure we can logically say, it is combination of address, binding and contracts.

Question arises: What does Address do?

This Person say: "I am Here to intimate the client to know where the WCF Service is located".

Question arises: What does Binding do?

This Person say: "I am Here to intimate the client to know, how the WCF Service Communicates with Request and Response"

Question arises: What does Contract do?

This Person say: "I am Here to intimate the client to know, what type of operation does this particular service does".

Question arises: What is Default Endpoint?

As, per the above part of this article we are now well grasped grip of what is an endpoint. Similarly, in WCF 4.0 one of the interesting features has been emerged and made life bit easier. It's the default endpoint. It enables to choose the Endpoint based on implemented service by default without user's attention. I mean user does not have to go and manually specify an endpoint for the service, so here in WCF 4.0 it's all automatically done without creating much overhead.

The Complete Code of IService1.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace Arthimetic_Applcation
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        double Add(double a, double b);
        [OperationContract]
        double Sub(double a, double b);
        [OperationContract]
        double Mul(double a, double b);
        [OperationContract]
        double Div(double a, double b);
    }
}

The Complete Code of Service1.svc looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace Arthimetic_Applcation
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public double Add(double a, double b)
        {
            return a + b;
        }
        public double Sub(double a, double b)
        {
            return a - b;
        }
        public double Mul(double a, double b)
        {
            return a * b;
        }
        public double Div(double a, double b)
        {
            return a / b;
        }
    }
}

The Complete Code of Web.Config looks like this:

<?xml version="1.0"?>
<configuration>
  <
system.web>
    <
compilation debug="true" targetFramework="4.0" />
  </system.web>
  <
system.serviceModel>
    <
behaviors>
      <
serviceBehaviors>
        <
behavior>
          <!--
To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <
serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
          <
serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </
serviceBehaviors>
    </
behaviors>
    <
serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <
system.webServer>
    <
modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</
configuration> 

The Complete Code of WebForm1.aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Default_Endpoint.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <center>
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Please Enter First Number" Font-Bold="true"
                            Font-Italic="true" ForeColor="Brown"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Please Enter Second Number" Font-Bold="true"
                            Font-Italic="true" ForeColor="Brown"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Addition" Width="117px" OnClick="Button1_Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="Button2" runat="server" Text="Substraction" Width="117px" OnClick="Button2_Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="Button3" runat="server" Text="Multiplication" Width="117px" OnClick="Button3_Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="Button4" runat="server" Text="Division" Width="117px" OnClick="Button4_Click" />
                    </td>
                </tr>
            </table>
            <table>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Label ID="Label3" runat="server" Visible="false" Font-Bold="true" Font-Italic="true"
                            ForeColor="Red" Font-Size="Large"></asp:Label>
                    </td>
                </tr>
            </table>
        </center>
    </div>
    </form>
</body>
</
html>

The Complete Code of WebForm1.aspx.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Default_Endpoint.ServiceReference1;
namespace Default_Endpoint
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" || TextBox2.Text == "")
                {
                    Label3.Text = "Please Enter Some Data"; Label3.Visible = true;
                }
                else
                {
                    var objClient = new Service1Client();
                    Label3.Text = ("The Addition of two Numbers is " + objClient.Add(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)));
                    Response.Write("</br><center>The Name of Address For This Operation is <b>" + objClient.Endpoint.Address + "</b></br>");
                    Response.Write("</br><center>The Name of Binding For This Operation is <b>" + objClient.Endpoint.Binding.Name + "</b></br>");
                    Response.Write("</br><center>The Name of Contract For This Operation is <b>" + objClient.Endpoint.Contract.Name + "</b></br>");
                    Response.Write("</br><center>The Name of Endpoint For This Operation is <b>" + objClient.Endpoint.Name + "</b></br></br>");
                    Label3.Visible = true;
                    Label3.Visible = true;
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }
            }
            catch (Exception exception)
            {
                exception.Message.ToString();
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" || TextBox2.Text == "")
                {
                    Label3.Text = "Please Enter Some Data";
                    Label3.Visible = true;
                }
                else
                {
                    var objClient = new Service1Client();
                    Label3.Text = ("The Substraction of two Numbers is " + objClient.Sub(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)));
                    Response.Write("</br><center>The Name of Address For This Operation is <b>" + objClient.Endpoint.Address + "</b></br>");
                    Response.Write("</br><center>The Name of Binding For This Operation is <b>" + objClient.Endpoint.Binding.Name + "</b></br>");
                    Response.Write("</br><center>The Name of Contract For This Operation is <b>" + objClient.Endpoint.Contract.Name + "</b></br>");
                    Response.Write("</br><center>The Name of Endpoint For This Operation is <b>" + objClient.Endpoint.Name + "</b></br></br>");
                    Label3.Visible = true;
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }
            }
            catch (Exception exception)
            {
                exception.Message.ToString();
            }
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            try
        {
                if (TextBox1.Text == "" || TextBox2.Text == "")
                {
                    Label3.Text = "Please Enter Some Data";
                    Label3.Visible = true;
                }
                else
                {
                    var objClient = new Service1Client();
                    Label3.Text = ("The Multiplication of two Numbers is " + objClient.Mul(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)));
                    Response.Write("</br><center>The Name of Address For This Operation is <b>" + objClient.Endpoint.Address + "</b></br>");
                    Response.Write("</br><center>The Name of Binding For This Operation is <b>" + objClient.Endpoint.Binding.Name + "</b></br>");
                    Response.Write("</br><center>The Name of Contract For This Operation is <b>" + objClient.Endpoint.Contract.Name + "</b></br>");
                    Response.Write("</br><center>The Name of Endpoint For This Operation is <b>" + objClient.Endpoint.Name + "</b></br></br>");
                    Label3.Visible = true;
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }
            }
            catch (Exception exception)
            {
                exception.Message.ToString();
            }
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" || TextBox2.Text == "")
                {
                    Label3.Text = "Please Enter Some Data"; Label3.Visible = true;
                }
                else
                {
                    var objClient = new Service1Client();
                    Label3.Text = ("The Division of two Numbers is " + objClient.Div(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)));
                    Response.Write("</br><center>The Name of Address For This Operation is <b>" + objClient.Endpoint.Address + "</b></br>");
                    Response.Write("</br><center>The Name of Binding For This Operation is <b>" + objClient.Endpoint.Binding.Name + "</b></br>");
                    Response.Write("</br><center>The Name of Contract For This Operation is <b>" + objClient.Endpoint.Contract.Name + "</b></br>");
                    Response.Write("</br><center>The Name of Endpoint For This Operation is <b>" + objClient.Endpoint.Name + "</b></br></br>");
                    Label3.Visible = true;
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }
            }
            catch (Exception exception)
            {
                exception.Message.ToString();
            }
        }
    }
}

  

The Output of the Application looks like this:

 Default Endpoint First.png

The Output of Addition Operation Application looks like this:

  Default Endpoint Second.png

The Invalid Entry Output Application looks like this:

Default Endpoint Third.png
 

I hope this article is useful for you.


Similar Articles
MVC Corporation
MVC Corporation is consulting and IT services based company.