WCF Service Role Using Azure on Client

Introduction

 

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

Question: What is WCF service role using Azure on client?
 

In simple terms "It enables use of a WCF service role with Azure and deployment of it across client context accessibility".

 

Step 1: Open Visual Studio 2010 and select Windows Azure Cloud Service application; see:

 

select-Windows-Azure-Cloud-Service.jpg
 

 

Step 2: Select WCF service web role and click on the ok - button.

 

Select-WCF-service-web-role.jpg
 

 

Step 3: 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 WCFServiceWebRole1

{

    // 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);

    }

}

 

Step 4: The complete code of Service1.svc.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 WCFServiceWebRole1

{

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

        }

    }

}

 

Step 5: The complete code of web.config looks like this:

 

<?xml version="1.0"?>

<configuration>

  <configSections></configSections>

  <!--  To collect diagnostic traces, uncomment the section below or merge with existing system.diagnostics section.To persist the traces to storage, update the DiagnosticsConnectionString setting with your storage credentials.To avoid performance degradation, remember to disable tracing on production deployments.<system.diagnostics>     <sharedListeners><add name="AzureLocalStorage" type="WCFServiceWebRole1.AzureLocalStorageTraceListener, WCFServiceWebRole1"/></sharedListeners><sources><source name="System.ServiceModel" switchValue="Verbose, ActivityTracing"><listeners><add name="AzureLocalStorage"/></listeners></source><source name="System.ServiceModel.MessageLogging" switchValue="Verbose"><listeners><add name="AzureLocalStorage"/></listeners></source></sources> </system.diagnostics> -->

  <system.diagnostics>

    <trace>

      <listeners>

        <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"name="AzureDiagnostics">

          <filter type="" />

        </add>

      </listeners>

    </trace>

  </system.diagnostics>

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

    <!--To browse web app root directory during debugging, set the value below to true.Set to false before deployment to avoid disclosing web app folder information.-->

    <directoryBrowse enabled="true"/>

  </system.webServer>

</configuration>

 

Step 6: Adding new client application to the solution:

 

asp.net-web-application.jpg
 

Step 7: Add new service reference to the project. In the context the service reference link looks like this:

 

http://127.0.0.1:81/Service1.svc

 

Step 8: The complete code of webform1.aspx looks like this:

 

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

    <center>

        <div>

            <table>

                <tr>

                    <td colspan="2">

                        <asp:Label ID="Label1" runat="server" Text="WCF Service Role using Azure on Client"

                            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 9: 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 AzureClientWCFApp.ServiceReference1;namespace AzureClientWCFApp

{

    public partial class WebForm1 : 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("The Addition Result of {0} and {1} is <b>{2}</b>", TextBox1.Text, TextBox2.Text, objClient.Add(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)));

                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("The Substraction Result of {0} and {1} is <b>{2}</b>", TextBox1.Text, TextBox2.Text, objClient.Sub(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)));

                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("The Multiplication Result of {0} and {1} is <b>{2}</b>", TextBox1.Text, TextBox2.Text, objClient.Mul(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)));

        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("The Division Result of {0} and {1} is <b>{2}</b>", TextBox1.Text, TextBox2.Text, objClient.Div(double.Parse(TextBox1.Text), double.Parse(TextBox2.Text)));

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

}

}

#region Instance VariablesService1Client objClient = new Service1Client();

#endregion

}

}
 

Step 10: The output of the application looks like this:

 

wcf-service-role-using-azure.jpg
 

 

Step 11: Addition operation output of the application looks like this:

client-wcf-service-role-using-azure.jpg

I hope this article is useful for you.


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