Arithmetic Operations Hosting on Cloud With Windows Azure

Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in Windows Azure.

Question: What is hosting arithmetic operations using Azure?

In simple terms "It enables creation of a new web form arithmetic application and serves to host it across cloud based environments using Windows Azure".

 

Step 1: Open visual studio 2010 and create an "ASP.NET Web Forms Application", as in:

 

asp.net-web-application.jpg

Step 2: The complete code of IArithmetic.cs looks like this:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace AzureApp

{

    public interface IArthmetic

    {

        double Add(double a, double b);

        double Sub(double a, double b);

        double Mul(double a, double b);

        double Div(double a, double b);

    }

}

 

Step 3: The complete code of Arithmetic.cs looks like this:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

namespace AzureApp

{

    public class Arthmetic : IArthmetic

    {

        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;

        }

    }

}

 

Step 4: The complete code of Default.aspx looks like this:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AzureApp._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">

    <center>

        <div>

            <table>

                <tr>

                    <td colspan="2">

                        <asp:Label ID="Label1" runat="server" Text="Arthmetic Operations - Azure App" Font-Bold="true"

                            Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label3" runat="server" Text="Please Enter First Number" Font-Size="Large"

                            Font-Names="Verdana" Font-Italic="true"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="TextBox2" runat="server" Width="120px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label2" runat="server" Text="Please Enter Second Number" Font-Size="Large"

                            Font-Names="Verdana" Font-Italic="true"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="TextBox1" runat="server" Width="120px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Button ID="Button1" runat="server" Text="Addition" Font-Names="Verdana" Width="213px"

                            BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Button ID="Button2" runat="server" Text="Substraction" Font-Names="Verdana"

                            Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button2_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Button ID="Button3" runat="server" Text="Multiplication" Font-Names="Verdana"

                            Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button3_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Button ID="Button4" runat="server" Text="Division" Font-Names="Verdana" Width="213px"

                            BackColor="Orange" Font-Bold="True" OnClick="Button4_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" align="center">

                        <asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>

                    </td>

                </tr>

            </table>

        </div>

    </center>

    </form>

</body>

</html>

 

Step 5: The complete code of Default.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;namespace AzureApp

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            this.TextBox2.Focus();

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))

            {

                Label5.Text = "Please Enter Some Values";

                Label5.ForeColor = System.Drawing.Color.Red;

            }

            else

            {

                Label5.Text = string.Format("Addition of {0} and {1} is <b>{2}</b>", TextBox1.Text, TextBox2.Text, objArthmetic.Add(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)).ToString());

                TextBox1.Text = string.Empty;TextBox2.Text = string.Empty;

            }

        }

        protected void Button2_Click(object sender, EventArgs e)

        {

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text)){Label5.Text = "Please Enter Some Values";

                Label5.ForeColor = System.Drawing.Color.Red;

            }

            else{Label5.Text = string.Format("Substraction of {0} and {1} is <b>{2}</b>", TextBox1.Text, TextBox2.Text, objArthmetic.Sub(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)).ToString());

                TextBox1.Text = string.Empty;TextBox2.Text = string.Empty;

            }

        }

        protected void Button3_Click(object sender, EventArgs e)

        {

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))

            {

                Label5.Text = "Please Enter Some Values";

                Label5.ForeColor = System.Drawing.Color.Red;

            }

            else

            {

                Label5.Text = string.Format("Multiplication of {0} and {1} is <b>{2}</b>", TextBox1.Text, TextBox2.Text, objArthmetic.Mul(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)).ToString());

                TextBox1.Text = string.Empty;TextBox2.Text = string.Empty;

            }

        }

        protected void Button4_Click(object sender, EventArgs e)

        {

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text)){Label5.Text = "Please Enter Some Values";

                Label5.ForeColor = System.Drawing.Color.Red;

            }

            else

            {

                Label5.Text = string.Format("Division of {0} and {1} is <b>{2}</b>", TextBox1.Text, TextBox2.Text, objArthmetic.Div(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)).ToString());

                TextBox1.Text = string.Empty;TextBox2.Text = string.Empty;

            }

        }

        #region Instance MembersArthmetic objArthmetic = new Arthmetic();

        #endregion

    }

}

 

Step 6: Now right-click on the project and create a Windows Azure Cloud Service Project, as in:

 

add-Windows-Azure-Cloud-Service-Project.jpg
 

 

Step 7: Configuration file output of the application looks like this:

 

Configuration-file-output.jpg
 

Step 8: Click on publish for the Azure project and dialogue box appears as shown below:

 

publish-windows-azure-application.jpg
 

 

Step 9: Download the credentials by clicking on sign in to download credentials:

 

windows-azure-platform.jpg
 

Step 10: Import the downloaded file and click the "Finish" button:

 

windows-azure-publish-sign-in.jpg
 

Step 11: Give a name to the application and select hosting center:

 

create-windows-azure-services.jpg
 

Step 12: Manage the publish settings and click the "Next" button, as in:

 

publish-windows-azure-application.jpg
 

Step 13: Verify the publish summary and click the "Finish" button, as in:

 

windows-azure-publish-summary.jpg
 

 

Step 14: Verify the Activity log for the status and when completed navigate to the website URL; see:

 

Output10.png
 

 

Step 15: The output of the application hosted in cloud looks like this:

 

arthmetic-operations-windows-azure.jpg
 

 

Step 16: The data entering output of the application hosted in cloud looks like this:

 

app-arthmetic-operations-windows-azure.jpg
 

 

Step 17: The addition operation output of the application hosted in cloud looks like this:

 

azure-apparthmetic-operations.jpg

Step 18: The complete code of ServiceConfiguration.Cloud.cscfg looks like this:
 

<?xml version="1.0" encoding="utf-8" ?>

<serviceconfiguration servicename="AzureApp.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"

   ="" osfamily="1" osversion="*" schemaversion="2012-05.1.7">

  <Role name="AzureApp">

    <Instances count="1" />

    <ConfigurationSettings>

      <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=azurearthmeticapp;AccountKey=oOw4qekrunuBcpB8oS87k2feSexp48WPLl2T66/fzukPSFSowFqqetUafZBiSlk+e6O1JygX/h8ShJn1b5ZSOA==" />

    </ConfigurationSettings>

  </Role>

</serviceconfiguration>
 

Step 19: The complete code of ServiceConfiguration.Local.cscfg looks like this:
 

<?xml version="1.0" encoding="utf-8" ?>

<serviceconfiguration servicename="AzureApp.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"

   ="" osfamily="1" osversion="*" schemaversion="2012-05.1.7">

  <Role name="AzureApp">

    <Instances count="1" />

    <ConfigurationSettings>

      <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />

    </ConfigurationSettings>

  </Role>

</serviceconfiguration>
 

Step 20: The complete code of ServiceDefinition.csdef looks like this:

 

<?xml version="1.0" encoding="utf-8" ?>

<servicedefinition name="AzureApp.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"

    schemaversion="2012-05.1.7">

  <WebRole name="AzureApp" vmsize="Small">

    <Sites>

      <Site name="Web">

        <Bindings>

          <Binding name="Endpoint1" endpointName="Endpoint1" />

        </Bindings>

      </Site>

    </Sites>

    <Endpoints>

      <InputEndpoint name="Endpoint1" protocol="http" port="80" />

    </Endpoints>

    <Imports>

      <Import moduleName="Diagnostics" />

    </Imports>

  </WebRole>

</servicedefinition> 

I hope this article is useful for you.

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