Consuming WCF Data Service (i.e. OData Service) for Windows Phone

To add an OData service (i.e. WCF Data Service) reference with VS 2013, it is very straightforward:

Right-click Service References > Add Service References... > enter the OData service address and namespace > Click OK.

VS2013 will add all necessary references (e.g. Microsoft.Data.Edm. Portable, Microsoft.Data.OData. Portable, Microsoft.Data.Services.Client.Portable).  If the references have old ones (e.g. Microsoft.Data.OData.WP80),  they need to be deleted.

The following is the old way:

There are many online materials on Consuming WCF Data Service for Windows Phone 7 the old way such as Shawn Wildermuth's excellent short video on this topic. The old way was a bit awkward because the tool - DataSvcUtil.exe -  was not integrated into VS, so one would have to run a long command line to generate the client proxy class.

Fortunately, with the release of Windows Phone 7.1 SDK, using WCF Data Service in a Windows Phone 7 app is finally like using it in other platforms - involving a few button clicks in Visual Studio to set it up, then a few lines of code to get the data.  OData Client Tools for Windows Phone Apps may need to be installed. Andy Pennell has a walk-through on how to consume OData in a Windows Phone ap. One can use Nuget to add Microsoft.Data.OData.WindowsPhone and Microsoft.Data.Services.Client.WindowsPhone to a project. OData service (i.e. WCF Data Service) can be created by following the instructions of MSDN.

Of course, making WCF data service consumption secure is still a challenge.  Microsoft has not made it as easy as many wish.

One method to deal with the security issue is as following:

  1. On the service side, overide OnStartProcessingRequest to retrieve client credential infomration from args.OperationContext.RequestHeaders, then use Membership.ValidateUser() to validate it.
  2. On the client sitde,  add a handler to DataServiceContext.SendingRequest, and use e.RequestHeaders to add user and password in the handler.

One badly needed feature for WP is generating task-based operations for WCF references.  The way to get around it is wrapping the call-back style async methods into awaitable method.  There is a sample.  There is blog post specific about this: Extending the OData Async Extensions to DataServiceCollection<T> Methods. One can shun this platform dependent approach by using standard OData query and a regular HTTP client (e.g. HttpClient) to download the results in JSON format, then use a preferred way (e.g. JSON.NET) parse them.   

Unfortunately, if a library project has installed WCF Data Services Client for OData v1-3 NuGet package to consume WCF services, all projects using that library much include this package regardless of whether it a project uses anything related to WCF.   This is not the case for other NuGet packages.  Otherwise, a project would get error "Error 496 Type universe cannot resolve assembly: Microsoft.Data.Services.Client.Portable..."

Microsoft made an unbelievable decision to drop the support for service references for Windows Phone 8.1 turning a significant advantage of WP over other competititors into a significant disadvantage.