Using OData Services in ASP.NET Applications : Page 2

Take advantage of the Open Data Protocol to build RESTful services in ASP.NET.

by Joydip Kanjilal

May 25, 2011

Page 2 of 2

Using OData Services in the ASP.NET MVC Framework

In this section we will discuss how we can create an OData Service and consume it from an ASP.NET MVC application. To get started using Open Data Protocol in .NET applications, you need to first create a WCF Data Service. You can create and use an Entity Data Model using ADO.NET Entity Framework template in Visual Studio 2010.

Here are the steps to create an OData Service:

Create an Entity Data Model or a source of data using LINQ to SQL. You can also use a custom data model, but it should implement IUpdateable and IQueryable interfaces.

Expose the data model as a WCF Data Service.

Host the WCF Data Service.

Consume the WCF Data Service in a client application.

To create a WCF Data Service, click on Project->Add New Item, and then select WCF Data Service from the list of the templates displayed. Note that you would need to add these references to your project:

Now that your data context and data service have been created and configured, you will need to consume the WCF Data Service in the previous step. After the WCF Data Service has been created, you need to add a service reference to the service in your ASP.NET MVC project if you need to access the OData Service from your ASP.NET MVC application. Once done, you can use the OData Client library (this is added as soon as you add a reference to the service) and the service proxy to query data in your controller.

References

Summary

The Open Data Protocol (commonly known as OData) is an open protocol for sharing data and exposing data as a Web friendly data feed. It provides you a uniform way of representing structured data in Atom and JSON formats and a uniform URL for navigation, sorting, filtering and paging data retrieved from a data service. In this article we discuss the Open Data Protocol and how we can work with it in .NET applications.

Joydip Kanjilal has over 10 years of industry experience with C, C++, Java, C#, VB, VC++, ASP.Net, XML, Design Patterns, UML, etc. He currently works as a senior project leader in a reputable multinational company in Hyderabad, India, and has contributed articles on .NET and related technologies to www.aspalliance.com.