TechBubbles Microsoft Technology BLOG

Creating an OData service on the .NET Framework

Open Data Protocol is a web protocol for accessing information via services. It is built upon technologies such as XML and JSON. The main component for building the OData service on .NET Framework is WCF Data Service. OData service can support different data source models including ADO.NET Entity Framework. LINQ to SQL etc. This post outlines the steps to creating an OData service on the .NET Framework through WCF Data service and ADO.NET Entity Framework data model.

1. Open Visual Studio 2012 and create new project by selecting ASP.NET Empty web application template in dialogue box.


2.  Add new item from project menu and select ADO.NET Entity Data Model from dialogue box


Choose the database objects in the wizard and say finish.

3. Create a new OData service using WCF Data service template which can be found under web category


4. Open the WcfDataService1.svc.cs file and change the default service type with the entity framework model class as shown below

   1: using System;

   2: using System.Collections.Generic;

   3: using System.Data.Services;

   4: using System.Data.Services.Common;

   5: using System.Linq;

   6: using System.ServiceModel.Web;

   7: using System.Web;

   8: namespace ODataWebApplication

   9:  {

  10:      public class WcfDataService1 : DataService<ODataWebApplication.AdventureWorks2012Entities>

  11:      {

  12:         // This method is called only once to initialize service-wide policies.

  13:          public static void InitializeService(DataServiceConfiguration config)

  14:         {

  15:             // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.

  16:             // Examples:

  17:              config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);

  18:              config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);

  19:              config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;

  20:         }

  21:     }

  22: }

5. Now start running the service by selecting .svc file in solution explorer and choose the view in browser then result will be as below


6. WCF Data service encapsulates the details of implementing an OData service. What all you need to do is generate data source and associate with the WCF Data Service. WCF Data Service is WCF Service implemented over REST Http endpoint. Most cases we no need to worry about configuration details.

7. You can also expose OData endpoints using WCF RIA service. WCF RIA service is based on standard WCF service and is designed for building data access services and encapsulates the business-logic at service layer. You can expose the data through end-points such as SOAP, OData and JSON.

8. Create a new WCF RIA service by selecting Domain Service Class item template in visual studio new item dialogue box


9. Specify the service options like enabling an OData endpoint.


10. Create a .svc file as the service access endpoint for the WCF RIA Service

Update the servicehost directive with ServiceHostFactory and Service types.

11. Now launch the WCF RIA service and access its OData endpoint by adding the odata suffix to the URL.

12. There is an entry in the web.Config file when you click ok which enables you enter odata in url

   1: <domainServices>

   2:       <endpoints>

   3:         <add name="OData" type="System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory, 

   4:          System.ServiceModel.DomainServices.Hosting.OData, Version=, Culture=neutral, 

   5:          PublicKeyToken=31bf3856ad364e35" />

   6:       </endpoints>

   7:     </domainServices>

Share this post :

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

Add Comment

TechBubbles Microsoft Technology BLOG

Follow me


Tag Cloud