Simple Arithmetic Operation using WCF Service Hosted on WebApp


Today, in this article we will try to perform simple arithmetic operation importing some created service into our web application. Once the application is fully developed we can request values from our web form which retrieve the output by performing an expected operation to the values specified by the service.

The Complete Code for created 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 Simple_WCF
{
    // 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]
        int Add(int x, int y);

        [OperationContract]
        int Sub(int x, int y);

        [OperationContract]
        int Mul(int x, int y);

        [OperationContract]
        int Div(int x, int y);
    }
}


The Complete Code for created Service1.svc looks like this: It implements IService1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Simple_WCF
{
    // 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 int Add(int x, int y)
        {
            return x + y;
        }

        public int Sub(int x, int y)
        {
            return x - y;
        }

        public int Mul(int x, int y)
        {
            return x * y;
        }

        public int Div(int x, int y)
        {
            return x / y;
        }
    }
}


The Complete Code for Created Web.Config looks like this:

We are creating a simple endpoint with some service related information:

<?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 ="Simple_WCF.Service1" behaviorConfiguration="Application">
        <endpoint address="/MyAddress" binding="basicHttpBinding" contract="Simple_WCF.IService1">

        </endpoint>
        <
endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">

        </endpoint>
        <
host>
          <
baseAddresses >
            <
add baseAddress="http://localhost:51861/Service1.svc"/>

          </baseAddresses>
        </
host>
      </
service>
    </
services>
  </
system.serviceModel>
  <
system.webServer>
    <
modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

Next Step to go and add a new web application to our existing solution file. So, now it's time for us to specific service reference for newly added web application. So the Service reference link is:

http://localhost:51861/Service1.svc

After this next step is to go and code in webform1.aspx file with some simple controls:

The Complete Code of Default.aspx looks like this:

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

<!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="Label2" runat="server" Text="Please Enter First Number" Font-Bold="true"
                            Font-Italic="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Please Enter Second Number" Font-Bold="true"
                            Font-Italic="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                </tr>
            </table>
        </center>
        <center>
            <table>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Add Two Numbers" OnClick="Button1Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="Button2" runat="server" Text="Sub Two Numbers" OnClick="Button2Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="Button3" runat="server" Text="Mul Two Numbers" OnClick="Button3Click" />
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="Button4" runat="server" Text="Div Two Numbers" OnClick="Button4Click"
                            Width="160px" />
                    </td>
                </tr>
            </table>
        </center>
        <center>
            <asp:Label ID="Label3" runat="server" Text="" Visible="false" Font-Bold="true" ForeColor="Red"
                Font-Size="Larger"></asp:Label>
        </center>
    </div>
    </form
>
</body>
</
html>

Code Toolbox Requirements:

  • 3 Labels, 2 Textboxes, 4 Buttons.

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

Here we the calling the service client with created methods.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SimpleWCFWEB.ServiceReference1;
namespace SimpleWCFWEB
{
    public partial class Default : System.Web.UI.Page
    {
        protected void PageLoad(object sender, EventArgs e)
        {

        }

        protected void Button1Click(object sender, EventArgs e)
        {
            var ab = new Service1Client();
            Label3.Text = string.Format("Result of Two Numbers is : {0}", (ab.Add(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text))).ToString());
            Label3.Visible = true;
            TextBox1.Text = "";
            TextBox2.Text = "";

        }

        protected void Button2Click(object sender, EventArgs e)
        {
            var ab = new Service1Client();
            Label3.Text = string.Format("Result of Two Numbers is : {0}", (ab.Sub(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text))).ToString());
            Label3.Visible = true;
            TextBox1.Text = "";
            TextBox2.Text = "";
        }

        protected void Button3Click(object sender, EventArgs e)
        {
            var ab = new Service1Client();
            Label3.Text = string.Format("Result of Two Numbers is : {0}", (ab.Mul(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text))).ToString());
            Label3.Visible = true;
            TextBox1.Text = "";
            TextBox2.Text = "";
        }

        protected void Button4Click(object sender, EventArgs e)
        {
            var ab = new Service1Client();
            Label3.Text = string.Format("Result of Two Numbers is : {0}", (ab.Div(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text))).ToString());
            Label3.Visible = true;
            TextBox1.Text = "";
            TextBox2.Text = "";
        }
    }
}


The Output of the Application looks like this:

1.png

The Output of addition operation looks like this:

2.png

The Output of subtraction operation looks like this:

3.png

The Output of multiplication operation looks like this:

4.png
The Output of division operation looks like this:

5.png

I hope this article is useful for you.


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