SIGN UP MEMBER LOGIN:    
ARTICLE

Arithmetic Operation Using WCF Service Hosted on Windows Phone with Silverlight

Posted by Vijay Prativadi Articles | Windows Phone in C# January 22, 2012
Today, in this article let’s play around by performing arithmetic operation on windows phone using Silverlight with base as WCF Service.
Reader Level:

Today, in this article let's play around by performing arithmetic operation on windows phone using Silverlight with base as WCF Service.

So, we are here only to do practical implementation rather than much studying theory concepts.

So, I think we all are now good to go and implement this interesting concept.

Let's get started it off now!!!

Let's Now Create New WCF Service Application in .NET 4.0 and carryon with further operations like this:

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

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

          </baseAddresses>
        </
host>
      </
service>
    </
services>
  </
system.serviceModel>

  <system.webServer>
    <
modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

Now we're all done with WCF Part, now it's time for us to go and create new Windows Phone Application and we will add it to existing solution folder.

The Complete Code of MainPage.xaml looks like this

<phone:PhoneApplicationPage
    x:Class="ArthimeticPhoneApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Vijay's Arthimetic using WCF" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Windows 7 Phone" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="234*" />
                <ColumnDefinition Width="222*" />
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="Textblock1" FontSize="25" Text="Please Enter First Number" Margin="-1,18,1,0" Grid.ColumnSpan="2" />
            <TextBox Margin="0,0,6,541" HorizontalAlignment="Right" Width="115" Name="textbox1" Grid.Column="1" />
            <TextBlock  x:Name="Textblock2" FontSize="25" Text="Please Enter Second Number " Margin="6,84,127,-72" Grid.ColumnSpan="2" />
            <TextBox Height="72" HorizontalAlignment="Left" Margin="101,72,0,0" Name="textBox2"  VerticalAlignment="Top" Width="115" Grid.Column="1" />
            <Button Content="Addition" Margin="35,140,-22,381" Grid.Column="1" Click="Button1_Click" Name="Button1" />
            <Button Content="Substraction" Margin="35,211,-21,321" Grid.Column="1" Name="Button2" Click="Button2_Click" />
       <Button Content="Multiplication" Margin="30,273,-22,257" Grid.Column="1" Name="Button3" Click="Button3_Click" />
<Button Content="Division" Height="72" VerticalAlignment="Top" Margin="35,339,-22,0" Grid.Column="1" Name="Button4" Click="Button4_Click" />
           <TextBlock x:Name="Textblock3" Margin="12,417,6,96" Grid.ColumnSpan="2" FontSize="26" FontWeight="Bold" HorizontalAlignment="Center" Visibility="Collapsed" />
        </Grid>
    </Grid>

    <!--Sample code showing usage of ApplicationBar-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>

The Complete Code of MainPage.xaml.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ArthimeticPhoneApp.ServiceReference1;
using System.ServiceModel;
namespace ArthimeticPhoneApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

        }

        static void AddCall(object sender, AddCompletedEventArgs e)
        {
            MessageBox.Show("The Addition Result is: " + e.Result);
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {

            if (textbox1.Text == "" || textBox2.Text == "")
            {
                Textblock3.Text = "Please Enter Some Values";
                Textblock3.Visibility = Visibility.Visible;

            }
            else
            {
                Textblock3.Text = "";
                Service1Client obj = new Service1Client();
                obj.AddCompleted += new EventHandler<AddCompletedEventArgs>(AddCall);
                obj.AddAsync(Convert.ToDouble(textbox1.Text), Convert.ToDouble(textBox2.Text));
                textbox1.Text = "";
                textBox2.Text = "";

            }

        }

        static void SubCall(object sender, SubCompletedEventArgs e)
        {
            MessageBox.Show("The Substraction Result is: " + e.Result);
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            if (textbox1.Text == "" || textBox2.Text == "")
            {
                Textblock3.Text = "Please Enter Some Values";
                Textblock3.Visibility = Visibility.Visible;

            }
            else
            {
                Textblock3.Text = "";
                Service1Client obj = new Service1Client();
                obj.SubCompleted += new EventHandler<SubCompletedEventArgs>(SubCall);
                obj.SubAsync(Convert.ToDouble(textbox1.Text), Convert.ToDouble(textBox2.Text));
                textbox1.Text = "";
                textBox2.Text = "";

            }

        }
        static void MulCall(object sender, MulCompletedEventArgs e)
        {
            MessageBox.Show("The Multiplication Result is: " + e.Result);
        }

        private void Button3_Click(object sender, RoutedEventArgs e)
        {
 
            if (textbox1.Text == "" || textBox2.Text == "")
            {
                Textblock3.Text = "Please Enter Some Values";
                Textblock3.Visibility = Visibility.Visible;

            }
            else
            {
                Textblock3.Text = "";
                Service1Client obj = new Service1Client();
                obj.MulCompleted += new EventHandler<MulCompletedEventArgs>(MulCall);
                obj.MulAsync(Convert.ToDouble(textbox1.Text), Convert.ToDouble(textBox2.Text));
                textbox1.Text = "";
                textBox2.Text = "";

            }
        }

        static void DivCall(object sender, DivCompletedEventArgs e)
        {
            MessageBox.Show("The Division Result is: " + e.Result);
        }

        private void Button4_Click(object sender, RoutedEventArgs e)
        {

            if (textbox1.Text == "" || textBox2.Text == "")
            {
                Textblock3.Text = "Please Enter Some Values";
                Textblock3.Visibility = Visibility.Visible;

            }
            else
            {
                Textblock3.Text = "";
                Service1Client obj = new Service1Client();
                obj.DivCompleted += new EventHandler<DivCompletedEventArgs>(DivCall);
                obj.DivAsync(Convert.ToDouble(textbox1.Text), Convert.ToDouble(textBox2.Text));
                textbox1.Text = "";
                textBox2.Text = "";
            }
        }

    }
}

The Output of the Application looks like this:

WindowsPhone1.png
 

WindowsPhone1.1.png

The Addition Operation Output of the Application looks like this:

WindowsPhone2.png
 

The Invalid Output Operation Application looks like this:

WindowsPhone3.png
 

I hope this article is useful for you.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks Monika...I Really Appreciate Your Feedback.

Posted by Vijay Prativadi Jan 23, 2012

Thanks Sonakshi...I Really Appreciate Your Feedback.

Posted by Vijay Prativadi Jan 23, 2012

Hi Vijai. Very nicely presented article.

Posted by Monika Arora Jan 22, 2012

Such a vast topic it is........very well explained... Vijay

Posted by Sonakshi Singh Jan 22, 2012
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor