Jerry Nixon @Work: Mango Sample: Consume Odata

Jerry Nixon on Windows

Monday, November 7, 2011

Mango Sample: Consume Odata

imageOdata is a standard method to expose data through a WCF service. Typical services have GetThis() & GetThat() methods, Odata services expose Queriable interfaces that allow consumers to define data queries on the fly.

Odata delivers data as standard, XML Atom. But reference an Odata service in a Windows Phone project, and that XML is abstracted away – you get simple objects instead. Yeah!

Note: This post is about consuming Odata. Perhaps I will create an Odata service post later. One interesting point: consuming Odata in a Windows Phone project is identical to the techniques in ANY Silverlight application.


Go to and register for a free account. Once registered, you should see the screen below. Your Customer ID is important to access Marketplace data.


Not only will you have a Customer ID – this is your login username. But you will also get an Account Key – this is your login password to access Marketplace data.


Hey! What is this Azure Data Marketplace?

The Azure Data Marketplace is where people with data can come sell it – letting the Azure infrastructure promote and deliver the data for them. Some of this data is free. And since it is exposed as Odata, it’s a great source for us to use in this sample.

Specifically, we will use the City Crime statistics.

Extraction of offense, arrest, and clearance data as well as law enforcement staffing information from the FBI's Uniform Crime Reporting (UCR) Program.


Step 2 Add a Service Reference:

For this demo we will use the Federal data on city crime statistics. There’s nothing tricky to adding a service reference. Just add the service like normal and Visual Studio will detect that it is an Odata feed and create the proxy correctly.

Here’s the service reference URL to Crime Data (call it CityCrimeService):

Step 3 Create a Context to the Feed

The context will connect to the source, supply your credentials, and allow you to query the data as you like. Odata really is this neat and simple.


Get real code here.

Step 4 Query for Data

It’s not likely that you know the structure of the Crime data. If this were a real scenario, you would just reference the documentation on the Azure Data Marketplace for this. Let me help, the data is basically structured like this:


Querying this data is as simple as using Linq to Objects. Let’s pretend that what we want is only the crime in State “Colorado”, City “Denver”, Year “All”.

Remember that these calls, like all service calls in Silverlight, are asynchronous. That means you execute the query, and must then handle the completed event.

Here’s the syntax to do the query:


Get real code here.


Hopefully, you have everything you need to start querying an Odata service. Odata sources allow you to create the query as you need the data – this improves payload and performance. Hopefully, you will also investigate the Azure Data Marketplace.

Remember this one Thing

Whenever you execute an asynchronous method and handle the completed event, the completed event code is no longer on the UI thread. In Silverlight you use the Dispatcher to return to the UI thread. Otherwise you get a cross thread exception.