Let's Play Around With Channel Factory in WCF Service Hosted on Web App


Let's Play Around With Channel Factory in WCF Service Hosted on Web App

Today, in this article let's play around with one of the most interesting topic in WCF and we will try to implement this concept such that we can get much better idea on idea to handle the real time case scenarios.

Question Arises: What is a Channel?

It is the person which says:  "I am Responsible for Sending requests from client to WCF service and getting back the response from service to client. It plays a vital role in communication with stack of channels using Endpoints"

Question Arises: What is a Channel Factory?

It is process where more than one channel can utilized for particular operation and get the expected results from WCF Service. In simple, it is a type of factory, where the entire channels are located and you can use them as many as you want for performing a specific operation WCF operation.

So, I think we all are now good to go and implement this wonderful concept effectively on web hosting.

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>
    <
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="ChannelWeb.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 Channel Factory Using WCF Service"
                            Font-Bold="true" ForeColor="Brown"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Please Enter First Number" Font-Bold="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server" Width="117px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label3" runat="server" Text="Please Enter Second Number" Font-Bold="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server" Width="117px"></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>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Label ID="Label5" 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 ChannelWeb.ServiceReference1;
using System.Web.UI.WebControls;
using System.ServiceModel;

namespace ChannelWeb
{
    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 = "Please Enter Some Values";
                    Label5.Text = "";
                    Label4.Visible = true;
                    Label5.Visible = true;
                }
                else
                {
                    var objBinding = new BasicHttpBinding();
                    var objEndpoint = new EndpointAddress("http://localhost:52424/Service1.svc");
                    var objChannelFactory = new ChannelFactory<IService1>(objBinding, objEndpoint);

                    //Now Let's Get Ready By Creating First Channel For Addition
                    IService1 objService1 = objChannelFactory.CreateChannel();
                    Label4.Text = "The Addition of&nbsp <b>" + TextBox1.Text + "</b>&nbsp and &nbsp<b>" + TextBox2.Text + "</b>&nbsp from First Channel is &nbsp<b>" + objService1.Add(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)) + "</b>";
                    Label4.Visible = true;
                    ((IClientChannel)objService1).Close();

                    // Now Let's Get Ready By Creating Second Channel For Addition
                    IService1 objService2 = objChannelFactory.CreateChannel();
                    Label5.Text = "The Addition of&nbsp <b>" + TextBox1.Text + "</b>&nbsp and &nbsp<b>" + TextBox2.Text + "</b>&nbsp from
Second Channel is &nbsp<b>"
+ objService2.Add(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text));
                    Label5.Visible = true;
                    ((IClientChannel)objService1).Close();
                    objChannelFactory.Close();
 
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();

            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" || TextBox2.Text == "")
                {
                    Label4.Text = "Please Enter Some Values";
                    Label5.Text = "";
                    Label4.Visible = true;
                    Label5.Visible = true;
                }
                else
                {
                    var objBinding = new BasicHttpBinding();
                    var objEndpoint = new EndpointAddress("http://localhost:52424/Service1.svc");
                    var objChannelFactory = new ChannelFactory<IService1>(objBinding, objEndpoint);

                    //Now Let's Get Ready By Creating First Channel For Substraction
                    IService1 objService1 = objChannelFactory.CreateChannel();
                    Label4.Text = "The Substraction of&nbsp <b>" + TextBox1.Text + "</b>&nbsp and &nbsp<b>" + TextBox2.Text + "</b>&nbsp from First Channel is &nbsp<b>" + objService1.Sub(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)) + "</b>";
                    Label4.Visible = true;
                    ((IClientChannel)objService1).Close();

                    // Now Let's Get Ready By Creating Second Channel For Substraction
                    IService1 objService2 = objChannelFactory.CreateChannel();
                    Label5.Text = "The Substraction of&nbsp <b>" + TextBox1.Text + "</b>&nbsp and &nbsp<b>" + TextBox2.Text + "</b>&nbsp from Second Channel is &nbsp<b>" + objService2.Sub(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text));
                    Label5.Visible = true;
                    ((IClientChannel)objService1).Close();
                    objChannelFactory.Close();
 
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();

            }

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" || TextBox2.Text == "")
                {
                    Label4.Text = "Please Enter Some Values";
                    Label5.Text = "";
                    Label4.Visible = true;
                    Label5.Visible = true;
                }
                else
                {
                    var objBinding = new BasicHttpBinding();
                    var objEndpoint = new EndpointAddress("http://localhost:52424/Service1.svc");
                    var objChannelFactory = new ChannelFactory<IService1>(objBinding, objEndpoint);

                    //Now Let's Get Ready By Creating First Channel For Multiplication
                    IService1 objService1 = objChannelFactory.CreateChannel();
                    Label4.Text = "The Multiplication of&nbsp <b>" + TextBox1.Text + "</b>&nbsp and &nbsp<b>" + TextBox2.Text + "</b>&nbsp
from First Channel is &nbsp<b>"
+ objService1.Mul(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)) + "</b>";
                    Label4.Visible = true;
                    ((IClientChannel)objService1).Close();

                    // Now Let's Get Ready By Creating Second Channel For Multiplication
                    IService1 objService2 = objChannelFactory.CreateChannel();
                    Label5.Text = "The Multiplication of&nbsp <b>" + TextBox1.Text + "</b>&nbsp and &nbsp<b>" + TextBox2.Text + "</b>&nbsp from Second Channel is &nbsp<b>" + objService2.Mul(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text));
                    Label5.Visible = true;
                    ((IClientChannel)objService1).Close();
                    objChannelFactory.Close();

                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();

            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (TextBox1.Text == "" || TextBox2.Text == "")
                {
                    Label4.Text = "Please Enter Some Values";
                    Label5.Text = "";
                    Label4.Visible = true;
                    Label5.Visible = true;
                }
                else
                {
                    var objBinding = new BasicHttpBinding();
                    var objEndpoint = new EndpointAddress("http://localhost:52424/Service1.svc");
                    var objChannelFactory = new ChannelFactory<IService1>(objBinding, objEndpoint);

                    //Now Let's Get Ready By Creating First Channel For Division
                    IService1 objService1 = objChannelFactory.CreateChannel();
                    Label4.Text = "The Division of&nbsp <b>" + TextBox1.Text + "</b>&nbsp and &nbsp<b>" + TextBox2.Text + "</b>&nbsp from First Channel is &nbsp<b>" + objService1.Div(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text)) + "</b>";
                    Label4.Visible = true;
                    ((IClientChannel)objService1).Close();

                    // Now Let's Get Ready By Creating Second Channel For Division
                    IService1 objService2 = objChannelFactory.CreateChannel();
                    Label5.Text = "The Division of&nbsp <b>" + TextBox1.Text + "</b>&nbsp and &nbsp<b>" + TextBox2.Text + "</b>&nbsp from Second Channel is &nbsp<b>" + objService2.Div(Convert.ToDouble(TextBox1.Text), Convert.ToDouble(TextBox2.Text));
                    Label5.Visible = true;
                    ((IClientChannel)objService1).Close();
                    objChannelFactory.Close();

                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
 
            }
 
        }
    }
}

The Output of the Application looks like this:

Channel Factory First.png
 


 

The Output of the Application for Addition Operation looks like this:

Channel Factory Second.png
 

The Invalid Data Entered Output looks like this:

Channel Factory Third.png
 

I hope this article is useful for you...I look forward for your comments and feedback.

Up Next
    Ebook Download
    View all
    Learn
    View all
    MVC Corporation is consulting and IT services based company.