locked
How to consume WCF Data Service in Windows Phone 7? RRS feed

  • Question

  • As the topic says, I've been trying to consume WCF data service from Windows Phone 7, but I don't understand whats with all the DataServiceContext, DataServiceCollection, etc. Some suggestions for guides on this would be greatly appreciated.
    Friday, November 19, 2010 1:50 PM

Answers

  • You need to do the following:

    1. Define the WCF Data Service (or use the sample Northwind service for testing purposes). For more information, see http://msdn.microsoft.com/en-us/library/cc838239(v=VS.95).aspx.
    2. Use the DataSvcUtil.exe tool included with the Odata Windows Phone Client to generated the client code to access the data service from your phone application, add this generated code page to your project, and add a reference to this version of System.Data.Services.Client.dll to your phone project.
    3. Create a new DataServiceCollection<T> instance based on a DataServiceContext that points to the data service.
    4. Register for the LoadCompleted event (this is where you handle the callback from the async request).
    5. Call the LoadAsync() method, which does take a Uri, to access a feed-specific URI in the data service.

    There is also paging to consider, but the basic steps are very close to the Silverlight 4 behaviors, except that the LoadAsync() method only takes a URI (because the Windows Phone client doesn't support LINQ queries at this time). For more information, see http://msdn.microsoft.com/en-us/library/ee681614(v=VS.95).aspx.

    In my experience, Silverlight does tend to eat exceptions and you have to do more work to get them. You can find more info here: http://msdn.microsoft.com/en-us/library/cc189070(VS.95).aspx. Sadly, Fiddler doesn't seem to work with the phone emulator, so that's one very useful troubleshooting tool that cannot be used in this case.

    Glenn Gailey 


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Saturday, November 27, 2010 1:36 AM

All replies

  • First question: are you using the phone library that we just released - http://blogs.msdn.com/b/astoriateam/archive/2010/10/28/data-services-client-for-win-phone-7-now-available.aspx.

    If yes, what issues are you running into? Since the phone platform does not support IQueryable, this release only supports executing via an uri, and helping with serialization/materialization of the requests. We also support tombstoning the application. YOu should be able to find more information in the blog above.

    Please feel free to ask specific questions if you have.

    Hope this helps

    Thanks

    Pratik


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, November 19, 2010 7:32 PM
    Moderator
  • Yep I'm using that library. This is my first time using WCF data service, so some terminologies are rather vague to me. Basically I'm just trying to load data from my SQL Server 2008 to a listbox in the application. From what I understand, you have to:

    1. Create an Entity (as generated by the ADO.NET Entity Model) that takes in an argument of the Uri of the service.

    2. Create a DataServiceCollection<> of the table that I want to pull data from

    3. Make a Linq query and pass it as an argument to the LoadAsync method of the service collection.

    4. Bind the ItemsSource of the control to the DataServiceCollection object.

     

    Unfortunately there is also the asynchronous issue to consider. My previous experiences lies with mostly Windows Forms, so I'm not sure how it plays a role in this. When I ran my application on the emulator, it simply returned to the app selection page after a few seconds without throwing any exception.

    Also, does the new LoadAync method takes in an Uri for the Phone version of the service as well?

    Saturday, November 20, 2010 6:03 AM
  • You need to do the following:

    1. Define the WCF Data Service (or use the sample Northwind service for testing purposes). For more information, see http://msdn.microsoft.com/en-us/library/cc838239(v=VS.95).aspx.
    2. Use the DataSvcUtil.exe tool included with the Odata Windows Phone Client to generated the client code to access the data service from your phone application, add this generated code page to your project, and add a reference to this version of System.Data.Services.Client.dll to your phone project.
    3. Create a new DataServiceCollection<T> instance based on a DataServiceContext that points to the data service.
    4. Register for the LoadCompleted event (this is where you handle the callback from the async request).
    5. Call the LoadAsync() method, which does take a Uri, to access a feed-specific URI in the data service.

    There is also paging to consider, but the basic steps are very close to the Silverlight 4 behaviors, except that the LoadAsync() method only takes a URI (because the Windows Phone client doesn't support LINQ queries at this time). For more information, see http://msdn.microsoft.com/en-us/library/ee681614(v=VS.95).aspx.

    In my experience, Silverlight does tend to eat exceptions and you have to do more work to get them. You can find more info here: http://msdn.microsoft.com/en-us/library/cc189070(VS.95).aspx. Sadly, Fiddler doesn't seem to work with the phone emulator, so that's one very useful troubleshooting tool that cannot be used in this case.

    Glenn Gailey 


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Saturday, November 27, 2010 1:36 AM