Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 28 – 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.       [more]

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.

clip_image002

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.

clip_image004

XAML Code:

01

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

02

<Grid.RowDefinitions>

03

<RowDefinition Height="Auto"/>

04

<RowDefinition Height="*"/>

05

</Grid.RowDefinitions>

06

 

07

<!--<span class="hiddenSpellError" pre="">TitlePanel</span> contains the name of the application and page title-->

08

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

09

<TextBlock x:Name="ApplicationTitle" Text="F5DEBUG WP7 TUTORIALS" Style="{StaticResource PhoneTextNormalStyle}"/>

10

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

11

</StackPanel>

12

 

13

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

14

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

15

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

16

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

17

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

18

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

19

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

20

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

21

</Grid>

22

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

clip_image006

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.

clip_image008

Code Behind:

01

using System;

02

using System.Collections.Generic;

03

using System.Linq;

04

using System.Net;

05

using System.Windows;

06

using System.Windows.Controls;

07

using System.Windows.Documents;

08

using System.Windows.Input;

09

using System.Windows.Media;

10

using System.Windows.Media.Animation;

11

using System.Windows.Shapes;

12

using Microsoft.Phone.Controls;

13

 

14

namespace F5debugWp7ConsumeWCF

15

{

16

public partial class MainPage : PhoneApplicationPage

17

{

18

// Constructor

19

public MainPage()

20

{

21

InitializeComponent();

22

}

23

 

24

private void button1_Click(object sender, RoutedEventArgs e)

25

{

26

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

27

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

28

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

29

 

30

client.Add2NumbersAsync(intvalue1, intvalue2);

31

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

32

 
       

33

}

34

 

35

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

36

{

37

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

38

 

39

}

40

}

41

}

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:

clip_image010

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.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Comments (1) -

Service development
Service development
8/4/2012 3:51:51 AM #

We can use WCF as a service to send data across the application as a service with the endpoints predefined to communicate.

http://venusappsolutions.com/

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter