Consuming basic REST service in Windows 7 mobile application


In this article I am going to explain how to consume a REST service in windows 7 phone app.

We will achieve this is two steps

  1. Create and Host REST based WCF service
  2. Consume service in W7 phone application.

Create and host REST service

There are many article, I have written discussing REST service. All can be finding here. But again, I am showing it here for benefit of your readers.

What I am going to do is to create a very simple REST service. Service will have one operation contract. This will take one string as input and return greeting message appending the string.

  1. Open visual studio and from File menu select new project. Go to WCF tab and select WCF service application.
  2. Open Web.Config file and inside System.Servicemodel delete any existing endpoint settings.
  3. Right click on your .svc file and select view markup.

Add as a Factory class to the markup of the service System.ServiceModel.Activation.WebServiceHostFactory. after adding markup should look like below

  1. Define the contract. There is one operation contract. This will take a string as input parameter and return a string.


    In WebGet attribute all settings are default for XML return and response. URI for the operation will be

    http://hostserver:port/Service1.svc/GetData/[parameter] so if you want to pass your name as parameter then URL would be http://hostserver:port/Service1.svc/GetData/DhananjayKumar

  2. Run the WCF service or right click on .svc file and select view in browser. Once you will run , you will see the below output

    In address bar of browser you can see the URL and XML output would be as below

  3. We are going to host the service for our sample in Cassini server.

    For reference complete code for Web.Config , Contract and Service definition is as below

    Contract (IService1.cs)

  4.     1 using System;

        2 using System.Collections.Generic;

        3 using System.Linq;

        4 using System.Runtime.Serialization;

        5 using System.ServiceModel;

        6 using System.ServiceModel.Web;

        7 using System.Text;

        8 namespace RESTtoConsumeinWindwPhone

        9 {  

       10     [ServiceContract]

       11     public interface IService1

       12     {

       13         [OperationContract]

       14         [WebGet(UriTemplate=“GetData/{a}”,

       15             RequestFormat=WebMessageFormat.Xml  ,

       16             ResponseFormat=WebMessageFormat.Xml,

       17             BodyStyle=WebMessageBodyStyle.Bare)]

       18         String GetData(string a);



       21     } 

       22 }



Service Definition (Service1.cs)

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Runtime.Serialization;

    5 using System.ServiceModel;

    6 using System.ServiceModel.Web;

    7 using System.Text;

    8 namespace RESTtoConsumeinWindwPhone

    9 {  

   10     public class Service1 : IService1

   11     {

   12         public String GetData(string a)

   13         {

   14             return “Hello “ + a + “From REST Service “;

   15         }     

   16     }

   17 }



<?xml version=1.0?>
<compilation debug=truetargetFramework=4.0 /> </system.web> <system.serviceModel><behaviors><serviceBehaviors><behavior>
<serviceMetadata httpGetEnabled=true/>

<serviceDebug includeExceptionDetailInFaults=false/>




<serviceHostingEnvironment multipleSiteBindingsEnabled=true />



<modules runAllManagedModulesForAllRequests=true/>



Consuming REST service in Window 7 Phone application

Very first create Windows Phone Application. From Silverlight for Windows Phone tab select Windows Phone Application project type.

  1. In content grid place a textbox and a button.
  2. User will eneter text and on clcik event we will pass this text to service and display retrun string into a message box. I am not designing the page very much it’s a very simple page.








mc:Ignorable=”d” d:DesignWidth=”480″ d:DesignHeight=”800″

FontFamily=”{StaticResource PhoneFontFamilyNormal}”

FontSize=”{StaticResource PhoneFontSizeNormal}”

Foreground=”{StaticResource PhoneForegroundBrush}”>
<Grid x:Name=”LayoutRoot” Background=”{StaticResource PhoneBackgroundBrush}”>

<RowDefinition Height=”Auto”/>

<RowDefinition Height=”*”/>


<Grid x:Name=”TitleGrid” Grid.Row=”0″>

<TextBlock Text=”MY APPLICATION” x:Name=”textBlockPageTitle” Style=”{StaticResource PhoneTextPageTitle1Style}”/>

<TextBlock Text=”page title” x:Name=”textBlockListTitle” Style=”{StaticResource PhoneTextPageTitle2Style}”/>


<Grid x:Name=”ContentGrid” Grid.Row=”1″>

<Button x:Name=”myButton” Height=”75″ Content=”Call REST service” Margin=”99,197,124,381″ />

<TextBox x:Name=”myTxtBox” Height=”31″ HorizontalAlignment=”Left” Margin=”20,96,0,0″
Text=” “ VerticalAlignment=”Top” Width=”434″ FontFamily=”Verdana” Foreground=”#FFCB4B4B” FontSize=”32″ />

<TextBox x:Name=”myTxtBoxDisplay” Height=”31″ HorizontalAlignment=”Left” Text=” “ VerticalAlignment=”Top” Width=”434″ FontFamily=”Verdana” Foreground=”#FFCB4B4B” FontSize=”20″ Margin=”20,319,0,0″ />



Now I will be consuming REST service on click event of button.

  1. Using WebClient class will make the asynchronous calls.
  2. On returning of the response as stream using DataContractSerliazer , I will de serialize the returned stream.

  3. While constructing the URL to be called append input parameter. I am taking input parameter here from textbox.


    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Net;

    5 using System.Windows;

    6 using System.Windows.Controls;

    7 using System.Windows.Documents;

    8 using System.Windows.Input;

    9 using System.Windows.Media;

   10 using System.Windows.Media.Animation;

   11 using System.Windows.Shapes;

   12 using Microsoft.Phone.Controls;

   13 using System.Xml;

   14 using System.Runtime.Serialization;



   17 namespace RESTServiceConsuming

   18 {

   19     public partial class MainPage : PhoneApplicationPage

   20     {

   21         public MainPage()

   22         {

   23             InitializeComponent();

   24             SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;


   26             myButton.Click += new RoutedEventHandler(myButton_Click);

   27         }


   29         void myButton_Click(object sender, RoutedEventArgs e)

   30         {

   31             WebClient proxy = new WebClient();

   32             string strUri = http://localhost:50841/Service1.svc/GetData/&#8221;+myTxtBox.Text;

   33             proxy.OpenReadCompleted+=new OpenReadCompletedEventHandler(proxy_OpenReadCompleted);

   34             proxy.OpenReadAsync(new Uri(strUri));

   35         }


   37         void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)

   38         {         


   40             DataContractSerializer ser = new DataContractSerializer(typeof(string));

   41             String str = ser.ReadObject(e.Result).ToString();

   42             myTxtBoxDisplay.Text = str;          



   45         }



   48     }

   49 }

Press F5 to get the result

So, in this article I explained about how to consume a REST service in Win7 mobile app. In later articles I shall explain image and streaming through REST service in Win7 mobile app. Thanks for reading. I hope it was useful. Happy Coding.

5 responses to “Consuming basic REST service in Windows 7 mobile application”

  1. thank you!:-)

  2. hi i have one windows mobile appliction with
    two labels username and password and two text boxes accordinglsy.
    and one login button.
    and i have a rest service http://localhost/ravi/service.svc/login
    to verify username and password
    if i click Login button
    how to call that service and verify whether user is authenticated or not.pls reply me fast.thanks

  3. Hi, thanks for the tutorial. Does the code for the WP7 work with any REST WebService? thx again

  4. […] Consuming basic REST service in Windows 7 mobile application […]

  5. This is a great tutorial. But instead of WCF I want to use PHP. Can you suggest a great tutorial like this one but that uses PHP? Thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at