Using OData Services in ASP.NET Applications

The Open Data Protocol (OData) is a Web protocol that relies on HTTP, Atom Publishing Protocol (AtomPub) and JSON. You can use OData to expose, query and update data from a variety of applications, services and stores. The OData protocol allows you to query data over HTTP protocol and then get the result back in Atom, JSON or XML formats.

The official website for Open Data Protocol states: “The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores.”

This article presents an overview of the Open Data Protocol and discusses how you can work with it in your .NET applications.

Prerequisites

To work with the code examples listed in this article, you need to have Visual Studio 2008 or 2010 installed in your system.

What Is the OData Protocol?

OData is a REST-based protocol that uses HTTP, JSON and ATOM and supports any platform that has support for HTTP, XML or JSON. You can use it to expose data retrieved from relational databases, file systems or data services. OData enables you to perform CRUD operations on top of a data model or a data service. In essence, it is an HTTP-based, platform-independent protocol that supports REST. In OData, data is provided through the usage URIs and common HTTP verbs, like GET, PUT, POST, MERGE and DELETE. Note that WCF Data Services (previously known as ADO.NET Data Services) is the implementation of Open Data Protocol in .NET applications.

MSDN states: “OData defines operations on resources using HTTP verbs (PUT, POST, UPDATE and DELETE), and it identifies those resources using a standard URI syntax. Data is transferred over HTTP using the AtomPub or JSON standards. For AtomPub, the OData protocol defines some conventions on the standard to support the exchange of query and schema information.”

Representation State Transfer (commonly known as REST) is an architectural paradigm that is based on the stateless HTTP protocol and is used for designing applications that can inter-communicate. In REST, Resources are used to represent state and functionality and these resources are in turn represented using user friendly URLs.

The official website of OData Protocol exposes data as an OData Service. Here is the service url: http://services.odata.org/website/odata.svc.

When you open the page in a browser, here is how the XML markup looks:

DefaultODataConsumersODataProducerApplicationsODataProducerLiveServices

Note that a collection of entity sets or feeds is referred to as workspace.

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:

  1. 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.
  2. Expose the data model as a WCF Data Service.
  3. Host the WCF Data Service.
  4. 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:

  • System
  • System.Core
  • System.ServiceModel
  • System.ServiceModel.Web
  • System.Data.Services

Here is how your data context class would look:

public class CustomerDataContext : DbContext{public CustomerDataContext(): base("CustomerEntitiesConnectionString"){}public CustomerDataContext(string connectionString): base(connectionString){}public DbSet Customers { get; set; }}

The next step would be to create a WCF Data Service and specify permissions as appropriate.

public class CustomersDataService : DataService{public static void InitializeService(DataServiceConfiguration config){config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead);config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;}}

To host the WCF Data Service, you can use the following code:

using System;using System.Collections.Generic;using System.Data.Services;using System.Linq;namespace CustomerODataServiceHost{class Program{static void Main(string[] args){string WCFServiceAddress = "http://localhost:1012";Uri[] uriArray = { new Uri(WCFServiceAddress) };Type serviceType = typeof(CustomerDataService); using (var host = new DataServiceHost(serviceType, uriArray)) {host.Open();Console.WriteLine("Service Started...Press any key to stop service");Console.ReadLine();}}}

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.

public ActionResult Index(){var serviceURI = new Uri("http://customer//view");var context = new CustomerServiceReference.CustomerDataService(serviceURI);var query = from c in context.Customerswhere c.Name == "Peter"select c;var result = query.ToList();return View(result);}

References

OData.org

OData-SDK

Slice and Dice OData with the jQuery DataTables Plug-In

Building Rich Internet Apps with the Open Data Protocol

ASP.NET Ajax Library

World’s Simplest OData Service

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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: