Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using OData Services in ASP.NET Applications : Page 2

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


advertisement

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.Customers where 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.



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.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap