Consuming WCF Data Services on Windows Phone 7

I read a good post recently on Windows Phone 7 Data Json WCF Data Service with IIS 7 Compression by Nick Randolph blog.  I thought I would give it a go minus the IIS compression to see if I could quickly generate a service reference using the VS 2010 Express for Windows Phone April CTP.   I found that currently there were a number of issues with the OData Client Library for Windows Phone 7 Series CTP and adding service references to a WCF Data Service in the CTP of VS – rest assured I am sure they will get sorted by the time things go RTM.  In the mean time these are the steps I used:       

  1. Create your SQL Database, Entity Framework 4.0 model and WCF Data Service project – you can follow up to, but not including,  the IIS compression on Nicks Blog to get your service up and running.
  2. Create a new Windows Phone 7 Application project – File –> New Project –> Silverlight for Windows Phone –> Windows Phone Application
  3. Try to generate a service reference to your  WCF Data Service using right click add service reference.  With the current CTP this will fail against a fails giving  “This service cannot be consumed by the current project. Please check if the project target framework supports this service type.”.  To resolve this you need to use DataSvcutil via the command line to generate your client.
  4. cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
    DataSvcutil.exe /uri:http://localhost/TestDataService/Test.svc/ /DataServiceCollection /Version:2.0 /out:D:\dev\TestSol\TestWCFDataService\Providers\TestClient.cs      

    Hint: drop this into a batch file for later ease of update as your service definition changes.     


  5. Include the generated file, TestClient.cs, into your solution and attempt to bulild.  You will find that the System.Data.Services.Client.dll assembly is not included in your project. On attempting to add it you will also find that it does not exist.  This is because that OData Client Library for Windows Phone 7 is still in CTP.  You can get the client from here
  6. Download and install the OData Client Library for Windows Phone 7 Series CTP .  After extracting and adding a reference to System.Data.Services.Client.dll your solution will build but when you try to run it it will start failing with an exception as follows “A first chance exception of type ‘System.IO.FileLoadException’ occurred in mscorlib.dll”.  Unfortunately apart from adding the service reference it was not quite obvious that the cause was the OData client assembly.  Tim Heur has a post explaining the issue and powershell script to resolve the issue in his post Windows Phone 7 Developer Tools April 2010 Refresh.  Follow the steps provided to fix the assembly and add a reference to the new generated assembly.
  7. Finally your solution should build and run.  Now all you need to do is wire up the data
    • Binding
      <Grid Grid.Row="1" x:Name="ContentGrid" Grid.Column="0">           
                  <ListBox ItemsSource="{Binding Patients}">               
                              <StackPanel Orientation="Horizontal">   
                                      <TextBlock Text="{Binding FirstName}" Margin="5,5,5,5"/>
                                      <TextBlock Text="{Binding LastName}" Margin="5,5,5,5"/>                       
    • Code Behind
        public MainPage()
                  SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
                  this.DataContext = ViewModel;
                  ViewModel.GetPatients(20);//future posts will look at using WCF serverside paging
              private MainPageViewModel _viewModel;
              public MainPageViewModel ViewModel
                      if (_viewModel == null)
                          _viewModel = new MainPageViewModel();
                      return _viewModel;
                  set { _viewModel = value; }


    • using System;
      using System.Data.Services.Client;
      using System.Linq;  
      namespace TestDataService.ViewModels
          public class MainPageViewModel 
              private TestClient _context;  
              public PatientViewModel()
                  _context = new TestClient(new Uri("http://localhost/TestDataService/Test.svc/"));//Externalise url to isolated storage
                  Patients = new DataServiceCollection<Patient>();           
              public void GetPatients(int count)
              public DataServiceCollection<Patient> Patients{ get; set; }

In summary, I cant wait until this goes RTM and becomes just as quick to work with as the serverside component.  I will look at WCF Data Service serverside paging and QueryInterceptors in a coming post.

Windows Phone 7 Application Certification Requirements

I am being dragged away from the computer for the weekend :( but will try to get the OData post I have been working on done when I return. Until then some good reading material for all those who are intending on releasing a Windows Phone 7 application to marketplace – Windows Phone 7 Application Certification Requirements