This post discusses about building a service using CLOUD platform that can reach various devices. What is OData and Where it fits in? OData is a specification that makes very easy to exchange and interact with data on the web. So OData is all about connecting up devices to the CLOUD. This post also discuss how to create a OData Service in Visual Studio 2010 and host it on Windows Azure then explains how to consume the service on Windows phone Mango.
What id OData?
A REST based set of patterns for accessing information via services
It is a great protocol for connecting devices to the CLOUD. The REST API’s which you might have developed having the following common requirements
- Querying the data
- Ordering the data
- Paging the data
- Filtering the data
- Even CRUD operations on data
OData provides a common way to do the above operations. If you got your web API and if you use OData then you have got a wide range of options to expose of your data to various client libraries and platforms.
1. Create a new Windows Azure Project in Visual Studio 2010 as shown below
2. Create a Data Access layer to access the data from SQL Azure as shown below
Your Workout Class looks as below
Your data access layer is ready to interact with the database.
The connection string in web.config looks as below, you can local sql database when your developing the application and then can change the connection string to point it to SQL Azure.
3. Now Create OData Service- To do this we can use WCF DataService which is Microsoft implementation of OData protocol in .NET. Add a new item to project and select WCF Data Service from dialogue box.
Your OData Service Class code looks as below
Now you have a OData Service that hooked up to your data model. You can mention what parts of your data that you want to expose in InitializeService method as below
You are going to give read permission on your data and also setting the page size to your return data. Now We have got a working OData Service.
Run your service and you can see the below output
as it supports multiple formats , if you want to view JSON viewer then you can install JSON viewer from here
Support Authentication for OData Service
We allow the authenticated users to insert new workouts in the above service. You need to write some interceptors to achieve this
The ChangeInterceptor invokes every time when you insert or update the data on service. If the user is not authenticated then returning the error message back otherwise we are claiming the credentials. Here it is using ACS with Oauth.
The web.config entry for Oauth protection module looks as below. you can download Oauth protection module from here.
If request comes with Oauth token then this module communicate with ACS and validates the user.
Following Web.Config entries tells where to look at the ACS host and other service url stuff etc
You get the above config values from Windows Azure management portal
Build a client to access the above created OData Service as below, we are consuming this windows phone application. Add a service reference to this project
It simply instantiates the DataServiceCollection and hookup the async handler and then formulate the query. The query is basically what you want to ask the OData Service. Context object in above code is instance generated by the service reference.
When you call LoadAsync then it calls http against that URI. Now run your client application which brings the data from service and shows in windows phone emulator as below
The OData protocol http://Odata.org was created to provide a simple, common way to interact with the data on the Web from any platform or device.
Share this post : |