My initial experiences with Windows Phone 7 development were a mixed bag. One of the things that I found to be a big letdown was the restrictions on the APIs and libraries available to the developer. That said, I do like Windows Phone 7 development because it allows me to use my existing .NET and C# skills, and keep me within the Visual Studio 2010 environment that has been very comfortable to me over the years. So despite my initially poor experience in getting starting with Windows Phone 7, I was willing to take a few more stabs at it.
One of the apps I wanted to make was a simple application to show the local crime rates. The government has this data on Data.gov, but it was only available as a data extract, and I really did not feel like building a Web service around a data set, so I shelved the idea. But then I discovered that the "Dallas" project had finally been wrapped up, and the Azure Marketplace DataMarket was live. Unfortunately, there are only a small number of data sets available on it right now, but one of them just happened to be the data set I wanted, and it was available for free. Talk about good luck! I quickly made a new Windows Phone 7 application, and tried to add the reference, only to be stopped in my tracks with this error: "This service cannot be consumed by the current project. Please check if the project target framework supports this service type."
It turns out, Windows Phone 7 launched without the ability to access WCF Data Services. I am not sure who made this decision, seeing as Windows Phone 7 is a great match for Azure Marketplace DataMarket, it's fairly dependent on Web services to do anything useful, and Microsoft is trying to push WCF Data Services. My initial research found only a CTP from March 2010 to provide this information. I asked around and found out that code to do just this was made announced at PDC recently and was available for free on CodePlex.
Something to keep in mind is that Windows Phone 7 applications must be responsive when performing processing and must support cancellation of "long running" processes. In my experience with the application certification process, I had an app rejected for not supporting cancellation even though it would take at most three seconds for processing. So now I am very cautious about making sure that my applications support cancellation.
Using the Open Data Protocol (OData) library is a snap. Here's what I did to be able to use an OData service from my Windows Phone 7 application:
- Download the file ODataClient_BinariesAndCodeGenToolForWinPhone.zip.
- Unzip it.
- In Windows Explorer, go to the Properties page for each of the DLLs, and click the Unblock button.
- In my Windows Phone 7 application in Visual Studio 2010, add a reference to the file System.Data.Services.Client.dll that I unzipped.
- Open a command prompt, and navigate to the directory of the unzipped files.
- Run the command: DavaSvcUtil.exe /uri:UrlToService /out:PathToCSharpFile (in my case, I used https://api.datamarket.azure.com/Data.ashx/data.gov/Crimes for the URL and .\DataGovCrime.cs for my output file). This creates a strongly typed proxy class to the data service.
- I copied this file into my Visual Studio solution's directory, and then added it to the solution.
- I created my code around cancellation and execution. Because I am not doing anything terribly complicated, and because the OData component already supports asynchronous processing, I took a backdoor hack approach to this for simplicity. I just have booleans indicating a "Running" and "Cancelled" state. If the event handler for the service request completion sees that the request is cancelled, it does nothing.
There was one big problem: The OData Client Library does not support authentication, at least not at a readily accessible level. Fortunately, there are several workarounds.
- The first option is what was recommended at PDC: construct the URL to query the data manually, and use the WebClient object to download the XML data and then parse it manually (using LINQ to XML, for example). This gives you ultimate control and lets you do any kind of authentication you might want. However, though, you are giving up things like strongly typed proxy classes, unless you feel like writing the code for that yourself (have fun).
- The second alternative, suggested by user sumantbhardvaj in the discussion for the OData Client Library, is to hook into the SendingRequest event and add the authentication. You can find his sample code on the CodePlex site. I personally have not tried this, so I cannot vouch for the result, but it seems like a very reasonable approach to me.
- Another alternative that has been suggested to me is to use the Hammock library instead.
For simple datasets, the WebClient method is probably the easiest way to get it done quickly and without having to learn anything new.
While it is unfortunate that the out-of-the-box experience with working with OData is not what it should be, there are enough options out there that you do not have to be left in the cold.
More about Windows Phone 7 on TechRepublic
- My first Windows Phone 7 app development project
- The Windows Phone 7 App Hub experience
- Optimize data access for mobile phone apps
- Windows Phone 7 through a developer's eyes
- Samsung Focus, a Windows Phone 7 powerhouse
- TechRepublic's Windows Phone 7 focus page
J.JaDisclosure of Justin's industry affiliations: Justin James has a contract with Spiceworks to write product buying guides; he has a contract with OpenAmplify, which is owned by Hapax, to write a series of blogs, tutorials, and articles; and he has a contract with OutSystems to write articles, sample code, etc.
Justin James is an OutSystems MVP, architect, and developer with expertise in SaaS applications and enterprise applications.