Working with Consuming a WCF Service in WP7

Introduction:

In this article we are going to see how to consume a WCF Service in a Windows Phone 7 Application Development. For the beginners WCF is a Windows Communication Foundation is a Microsoft framework to build application in Service Oriented Architecture. We can use WCF as a service to send data across the application as a service with the endpoints predefined to communicate. The service endpoints can be of type which is hosted on to an IIS Server and available anytime, or it can also be an application oriented service to provide on demand usage. The data transfer through the messages can be of any particular format that the sender and receiver can able to understand the communication over the protocol.

In Windows Phone 7 development we have scenarios where we need to pass and get the data over the service through some authorized protocols and secured ports where WCF plays a major role. Let us see the step by step process on how to consume a WCF Service in a Windows Phone 7 Application taking into consideration the reader should be familiar with creating a WCF Service and hosting on to IIS. Refer MSDN article to get some idea on how to create and host the WCF Service using the link http://msdn.microsoft.com/en-us/library/ms733766.aspx

Steps:

Open Visual Studio 2010 IDE in administrator mode and create a new Silverlight for Windows Phone 7 Application with a valid project name as shown in the screen below.

image1.jpg

We can see the project is created and the main page is opened with the default controls. Let us consider that we have a WCF application already created and hosted on IIS which can be consumed and pass the value (2 numbers) and we will get the sum of the values passed as the output. So from our Windows phone 7 application we can pass the 2 values and we can make the WCF do the sum and sends the result back which can be viewed in the application.

First let us add some controls which gets the user inputs that should be passed to the WCF Service and to show the results to the end users. Just copy the XAML provided below to get the same user experience with the design as shown in the screen below.


image2.jpg

XAML Code:

<Grid x:Name="LayoutRoot" Background="Transparent">

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="*"/>

</Grid.RowDefinitions>

<!--<span class="hiddenSpellError" pre="">TitlePanel</span> 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="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>

<TextBlock x:Name="PageTitle" Text="WCF Service" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

</StackPanel>

<!--<span class="hiddenSpellError" pre="">ContentPanel</span> - place additional content here-->

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

<TextBlock Height="30" HorizontalAlignment="Left" Margin="30,43,0,0" Name="textBlock1" Text="Value 1" VerticalAlignment="Top" />

<TextBlock Height="30" HorizontalAlignment="Left" Margin="30,117,0,0" Name="textBlock2" Text="Value 2" VerticalAlignment="Top" />

<TextBox Height="72" HorizontalAlignment="Left" Margin="119,19,0,0" Name="textBox1" Text="" VerticalAlignment="Top" Width="313" />

<TextBox Height="72" HorizontalAlignment="Left" Margin="119,97,0,0" Name="textBox2" Text="" VerticalAlignment="Top" Width="313" />

<TextBlock Height="116" HorizontalAlignment="Left" Margin="53,312,0,0" Name="txtResult" Text="" TextWrapping="Wrap" VerticalAlignment="Top" Width="379" />

<Button Content="Consume WCF and Get Result" Height="72" Margin="6,196,0,0" Name="button1" VerticalAlignment="Top" />

</Grid>

</Grid>

Now we can consume the WCF Service by right clicking on the Project and select Add Service reference, we can see a pop up window where we need to input the WCF Service and the details as shown in the screen below.

image3.jpg

Clicking on OK will add the service reference to the project file and we can see the service reference files added with the name provided in the namespace along with some reference files as well. Now we need to put our code logic to consume the service and pass the values as shown in the code below.

image4.jpg

Code Behind:

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;

 

namespace F5debugWp7ConsumeWCF

{

    public partial class MainPage : PhoneApplicationPage

    {

        // Constructor

        public MainPage()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            WCFServiceToSumData.Service1Client client = new WCFServiceToSumData.Service1Client();

             int intvalue1 = Convert.ToInt32(textBox1.Text.ToString());

             int intvalue2 = Convert.ToInt32(textBox2.Text.ToString());

             client.Add2NumbersAsync(intvalue1, intvalue2);

             client.Add2NumbersCompleted += new EventHandler<WCFServiceToSumData.Add2NumbersCompletedEventArgs>
                                                              (client_getStringCompleted);

        }

        private void client_getStringCompleted(object sender, WCFServiceToSumData.Add2NumbersCompletedEventArgs e)

        {

            txtResult.Text = "The Sum of the above 2 number is " + e.Result;

        }

    }

}

Now we are done with our code, just to check if the things are working good we can directly run the application by pressing F5 from the key board or from the tool bar and we can see the expected results once we pass the data as shown in the screens below.

Output Screens:

image5.jpg

Conclusion:

So in this article we have seen how to make use of the WCF service which we created locally or to consume the 3rd party WCF Service and get the desired output as per the requirement in Windows Phone 7 Development environment.

Thanks for reading my article. If you like my blog and if you are interested in getting the latest updates on new articles, kindly follow me through one of these options.

Up Next
    Ebook Download
    View all
    Learn
    View all