Using Multiple Endpoints in WCF Hosted on Web App

Today, in this article let's see how to create a multiple endpoints using WCF Service and later we will try to host it on to a web application.

Question Arises: What is an Endpoint?

The WCF Service can be utilized by any client using Endpoint. The Endpoint includes 3 main things: Address, Binding and Contract. Endpoint uses these three main things to communicate across the clients and consume the service.

Question Arises: What are Address, Bindings and Contract in WCF ?

  • Address: Basically specifies URL link for which client can consume the WCF service.
  • Binding: Describes type of protocol is used to communicate with client
  • Contract: Specifies process of utilizing interface names and consume the service.

So, we are good to go and we don't get hold of much about theory part. We will try to implement this concept so that we can get more clear and real time idea. But this time we will try to use multiple endpoints and let's see how the implementation scenario carries on.

Firstly create some service contract and operation contract.

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 Endpoints
{
    // 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 Endpoints
{
    // 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 name="Application">
          <!-- 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" />
    <services>
      <
service name="Endpoints.Service1" behaviorConfiguration="Application">
        <endpoint name="addBinding" address="/MyAddBinding" binding="basicHttpBinding" contract="Endpoints.IService1"></endpoint>
        <endpoint name="subBinding" address="/MySubBinding" binding="basicHttpBinding" contract="Endpoints.IService1"></endpoint>
        <endpoint name="mulBinding" address="/MyMulBinding" binding="wsHttpBinding" contract="Endpoints.IService1"></endpoint>
        <endpoint name="divBinding" address="/MyDivBinding" binding="wsHttpBinding" contract="Endpoints.IService1"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <
baseAddresses>
            <
add baseAddress="http://localhost:50236/Service1.svc"/>
          </baseAddresses>
        </
host>
      </
service>
    </
services>
  </
system.serviceModel>
  <
system.webServer>
    <
modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</
configuration>

The service links looks like this:

http://localhost:50236/Service1.svc


Later we will try to add new web Application and we will integrate it into our existing solution file.

The Complete Code of WebForm1.aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="EndpointvsEndpoints.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>
            <br />
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Please Enter First Number" Font-Bold="true"
                            ForeColor="Brown" Font-Italic="true"></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"
                            ForeColor="Brown" Font-Italic="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Addition" BackColor="Coral" Width="184px"
                            OnClick="Button1Click" />
                    </td>
                    <td>
                        <asp:Button ID="Button2" runat="server" Text="Substraction" BackColor="Coral" Width="166px"
                            OnClick="Button2Click" />
                    </td>
                    <td>
                        <asp:Button ID="Button3" runat="server" Text="Multiplication" BackColor="Coral" Width="166px"
                            OnClick="Button3Click" />
                    </td>
                    <td>
                        <asp:Button ID="Button4" runat="server" Text="Division" BackColor="Coral" Width="166px"
                            OnClick="Button4Click" />
                    </td>
                </tr>
            </table>
            <table>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Label ID="Label3" runat="server" Visible="false" ForeColor="Brown" Font-Bold="true"
                            Font-Size="Larger"></asp:Label>
                    </td>
                </tr>
            </table>
        </center>
    </div>
    </form
>
</body>
</
html>

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

using System;
using EndpointvsEndpoints.ServiceReference1;

namespace EndpointvsEndpoints
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void PageLoad(object sender, EventArgs e)
        {

        }

        protected void Button1Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" && TextBox2.Text == "")
                {
                    Label3.Text = "Addition Operation Was Not Successfully. Empty Data Provided";
                    Label3.Visible = true;
                }
                else
                {
                    var add = new Service1Client("addBinding", "http://localhost:50236/Service1.svc/MyAddBinding");
                    Label3.Text = "The Addition of Two Numbers Result is: <b>" + (add.Add(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text))).ToString() + "</b>";
                    Label3.Visible = true;

                }

            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

        protected void Button2Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" && TextBox2.Text == "")
                {
                    Label3.Text = "Substraction Operation Was Not Successfully. Empty Data Provided";
                    Label3.Visible = true;
                }
                else
                {
                    var add = new Service1Client("subBinding", "http://localhost:50236/Service1.svc/MySubBinding");
                    Label3.Text = "The Substraction of Two Numbers Result is: <b>" + (add.Sub(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text))) + "</b>";
                    Label3.Visible = true;

                }

            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

        protected void Button3Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" && TextBox2.Text == "")
                {
                    Label3.Text = "Multiplication Operation Was Not Successfully. Empty Data Provided";
                    Label3.Visible = true;
                }
                else
                {
                    var add = new Service1Client("mulBinding", "http://localhost:50236/Service1.svc/MyMulBinding");
                    Label3.Text = "The Multiplication of Two Numbers Result is: <b>" + (add.Mul(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text))) + "</b>";
                    Label3.Visible = true;

                }

            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }

        protected void Button4Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" && TextBox2.Text == "")
                {
                    Label3.Text = "Division Operation Was Not Successfully. Empty Data Provided";
                    Label3.Visible = true;
                }
                else
                {
                    var add = new Service1Client("addBinding", "http://localhost:50236/Service1.svc/MyAddBinding");
                    Label3.Text = "The Division of Two Numbers Result is: <b>" + (add.Div(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text))).ToString() + "</b>";
                    Label3.Visible = true;

                }

            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
    }
}


The Output of the Application looks like this:

EndPointFirst.png

The Addition Operation Output of the Application looks like this:
EndPointSecond.png

The Friendly Error Message Output for nothing entered looks like this:

EndPointThird.png
I hope this article is useful for you.


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