Protocol Mapping in WCF 4.0 Hosted on Web App

Today, in this article let's dig out an interesting and wonderful concept. This concept is made life better and delivers high in throughput. 

Question arises: What is Protocol Mapping?

In Simple Terms: It is set of configured protocols which intimate to use which type of binding based on particular address. It groups set of protocols that are responsible for transport protocols.

So, we all are good to go and implement this concept.

Now, let's get it started off

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 WcfService1
{
    // 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 WcfService1
{
    // 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>
    <
protocolMapping>
      <
add scheme="http" binding="basicHttpBinding" bindingConfiguration=""/>

    </protocolMapping>
    <
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> 

Now it's time for us to go and add protocols with unique URL and system user name. So the complete steps for doing this is described as below:

Start->Accessories->Run

When the Run window opens type: netsh

The command prompt will open, so now it's time for us to go and add protocols for user. So in the command prompt type:

netsh>http add urlacl url=http: //localhost:8000/Service user=Vijay-PC

Press Enter Now. The http will be successfully added and display a success message on prompt.

The Complete Code of WebForm1.aspx looks like this:

<%@ Page="" Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Protocol_Mapping.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>
      <
style type="text/css"> .div { font-family:Cambria; } </style>
    </head>
    <
body>
      <
form id="form1" runat="server">
        <div class="div">
          <center>
            <
table>
              <
tr>
                <
td></td>
                <
td>
                  <
asp:Label ID="Label1" runat="server" Text="Vijay's Protocol Mapping"></asp:Label>
                </td>
              </
tr>
              <
tr>
                <
td>
                  <
asp:Label ID="Label2" runat="server" Text="Please Enter First Number"></asp:Label>
                </td>
                <
td>
                  <
asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
              </
tr>
              <
tr>
                <
td>
                  <
asp:Label ID="Label3" runat="server" Text="Please Enter Second Number"></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="Label4" runat="server" Visible="false" ForeColor="Red"></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 Protocol_Mapping.ServiceReference1;
using WcfService1;
using System.ServiceModel;

namespace Protocol_Mapping
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Focus();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" || TextBox2.Text == "")
                {
                    Label4.Text = "Addition Operation Unsuccessful";
                    Label4.Visible = true;
                }
                else
                {
                    ServiceHost objHost = new ServiceHost(typeof(Service1),
                        new Uri("http://localhost:8000/Service"));
                    objHost.Open();
                    foreach (var objEnd in objHost.Description.Endpoints)
                    {
                        Service1Client objClient = new Service1Client();
                        double val = Convert.ToDouble(TextBox1.Text);
                        double val1 = Convert.ToDouble(TextBox2.Text);
                        double result = objClient.Add(Convert.ToDouble(val), Convert.ToDouble(val1));
                        Label4.Text = "The Addition of &nbsp<b>" + val + "</b>&nbsp and &nbsp<b>" + val1 + "</b>&nbsp is &nbsp<b>" + result + "</b></br> The Host Status is: &nbsp<b>" + objHost.State + "</b>&nbsp </br> The Endpoint Name is: &nbsp<b>" + objEnd.Name + "</b> </br> The Address is: &nbsp<b>" + objEnd.Address.ToString() + "</b></br> The Binding Name is: &nbsp<b>" + objEnd.Binding.Name + "</b> </br> The Contract Name is: &nbsp<b>" + objEnd.Contract.Name + "</b>";
                        Label4.Visible = true;
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                    }
                    objHost.Close();
                }
            }
            catch (FormatException ex)
            {
                ex.Message.ToString();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" || TextBox2.Text == "")
                {
                    Label4.Text = "Substraction Operation Unsuccessful";
                    Label4.Visible = true;
                }
                else
                {
                    ServiceHost objHost = new ServiceHost(typeof(Service1),
                        new Uri("http://localhost:8000/Service"));
                    objHost.Open();
                    foreach (var objEnd in objHost.Description.Endpoints)
                    {
                        Service1Client objClient = new Service1Client();
                        double val = Convert.ToDouble(TextBox1.Text);
                        double val1 = Convert.ToDouble(TextBox2.Text);
                        double result = objClient.Sub(Convert.ToDouble(val), Convert.ToDouble(val1));
                        Label4.Text = "The Substraction of &nbsp<b>" + val + "</b>&nbsp and &nbsp<b>" + val1 + "</b>&nbsp is &nbsp<b>" + result + "</b></br> The Host Status is: &nbsp<b>" + objHost.State + "</b>&nbsp </br> The Endpoint Name is: &nbsp<b>" + objEnd.Name + "</b> </br> The Address is: &nbsp<b>" + objEnd.Address.ToString() + "</b></br> The Binding Name is: &nbsp<b>" + objEnd.Binding.Name + "</b> </br> The Contract Name is: &nbsp<b>" + objEnd.Contract.Name + "</b>";
                        Label4.Visible = true;
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                    }
                    objHost.Close();
                }
            }
            catch (FormatException ex)
            {
                ex.Message.ToString();
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" || TextBox2.Text == "")
                {
                    Label4.Text = "Multiplication Operation Unsuccessful";
                    Label4.Visible = true;
                }
                else
                {
                    ServiceHost objHost = new ServiceHost(typeof(Service1),
                        new Uri("http://localhost:8000/Service"));
                    objHost.Open();
                    foreach (var objEnd in objHost.Description.Endpoints)
                    {
                        Service1Client objClient = new Service1Client();
                        double val = Convert.ToDouble(TextBox1.Text);
                        double val1 = Convert.ToDouble(TextBox2.Text);
                        double result = objClient.Mul(Convert.ToDouble(val), Convert.ToDouble(val1));
                        Label4.Text = "The Multiplication of &nbsp<b>" + val + "</b>&nbsp and &nbsp<b>" + val1 + "</b>&nbsp is &nbsp<b>" + result + "</b></br> The Host Status is: &nbsp<b>" + objHost.State + "</b>&nbsp </br> The Endpoint Name is: &nbsp<b>" + objEnd.Name + "</b> </br> The Address is: &nbsp<b>" + objEnd.Address.ToString() + "</b></br> The Binding Name is: &nbsp<b>" + objEnd.Binding.Name + "</b> </br> The Contract Name is: &nbsp<b>" + objEnd.Contract.Name + "</b>";
                        Label4.Visible = true;
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                    }
                    objHost.Close();
                }
            }
            catch (FormatException ex)
            {
                ex.Message.ToString();
            }

        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" || TextBox2.Text == "")
                {
                    Label4.Text = "Division Operation Unsuccessful";
                    Label4.Visible = true;
                }
                else
                {
                    ServiceHost objHost = new ServiceHost(typeof(Service1),
                        new Uri("http://localhost:8000/Service"));
                    objHost.Open();
                    foreach (var objEnd in objHost.Description.Endpoints)
                    {
                        Service1Client objClient = new Service1Client();
                        double val = Convert.ToDouble(TextBox1.Text);
                        double val1 = Convert.ToDouble(TextBox2.Text);
                        double result = objClient.Div(Convert.ToDouble(val), Convert.ToDouble(val1));
                        Label4.Text = "The Division of &nbsp<b>" + val + "</b>&nbsp and &nbsp<b>" + val1 + "</b>&nbsp is &nbsp<b>" + result + "</b></br> The Host Status is: &nbsp<b>" + objHost.State + "</b>&nbsp </br> The Endpoint Name is: &nbsp<b>" + objEnd.Name + "</b> </br> The Address is: &nbsp<b>" + objEnd.Address.ToString() + "</b></br> The Binding Name is: &nbsp<b>" + objEnd.Binding.Name + "</b> </br> The Contract Name is: &nbsp<b>" + objEnd.Contract.Name + "</b>";
                        Label4.Visible = true;
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                    }
                    objHost.Close();
                }
            }
            catch (FormatException ex)
            {
                ex.Message.ToString();
            }
        }
    }
}


Now, we haven't yet done, when you run the application it displays an "addressacessdeniedException".  This exception is thrown because it's one of the new security feature implemented in since release from Vista Operating System. It stops the user and whereby it can be handled by running the visual studio as in Administrator Mode.  The reason behind it is protocols require administrator access rights and privileges. The Screenshot displayed below shows how to run visual studio in administrator mode. The Screenshot follows:

ProtocolMapping5.png
 

The Exception screenshot is given as below:

ProtocolMapping1.png
 

The Output of the Application looks like this:

ProtocolMapping2.png
 

The Addition Operation Output Application looks like this:

ProtocolMapping4.png
 

The Invalid Data Entered Output Application looks like this:

  ProtocolMapping3.png

I hope this article is useful for you.


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