Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Develop and Consume OData Web Services in WCF

DZone's Guide to

Develop and Consume OData Web Services in WCF

·
Free Resource

What is OData?

OData is an Open Data Protocol used to access data from various sources like Relational databases, file systems, etc. and exposes it to the consumers that can query or update data.   OData is based upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from variety of data sources. Consumers can query the data using HTTP protocol and get the results in AtomPub and JSON formats. These are the formats through which the service can deliver the requested resources or collection of entities.

OData is released under the Open Specification Promise http://www.microsoft.com/openspecifications/en/us/programs/osp/default.aspx and anyone can freely interoperate with OData implementations.

Developing WCF Service using OData Protocol

Microsoft has first introduced OData support in Visual Studio 2008 SP1 in terms of ADO.Net Data Services (subsequently known as WCF Data Services). Developers can create services on the fly and exposes data using Entity Framework or LINQ 2 SQL ORM tools.

Let’s create a simple WCF Data Service and expose data using Entity Framework.

Start Visual Studio 2010 and create a new WCF Service Application project.

1

Right click the WCF Service Application project and click on the “Add New Item”, then select WCF Data Service.

2

If you open the WCF Data Service class file you can see the Class will be derived from the generic DataService class. This is the main entry point for developing WCF Data services. We have to specify the data source class name. In order to proceed further I will add entity framework model which creates the entities model on the fly.

Right click on the Service Project and Add “ADO.Net Entity Data Model” from the dialog.

3

When you add ADO.Net Entity Model it will ask you to select the data source, go through the wizard by selecting the data source and finish a dialog.

Now open the .edmx file click on properties.

4

Here’s the entity container named as “DDMSEntities”. Just specify the name DDMSEntities in your WCF Data Service base class like below

       public class WcfDataService1 : DataService<DDMSEntities>

You can now add the SetEntitySetAccessRule for each entity you want to expose. Default configuration is to deny access to resources. When a service is initialized the access rights must be enabled.

Sample code snippet below      

public static void InitializeService(DataServiceConfiguration config)

        {

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

            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

            config.SetEntitySetAccessRule ("Levels", EntitySetRights.AllRead);

            config.SetEntitySetAccessRule ("NodeTypes", EntitySetRights.All);

        }

In Above code, I have added two tables i.e. Levels and NodeTypes and given permissions as AllRead.

There are many permission levels you can set as follows.

Below are the enums of EntitySetRights with their description

-          All – Authorize to create, read, update and delete data.

-          AllRead- Authorize to read data.

-          AllWrite- Authorize to write data.

-          None- Denies all rights to access data.

-          ReadMultiple – Authorize to read sets of data.

-          ReadSingle – Authorize to read single data of items.

-          WriteAppend- Authorize to create new data items in the data sets.

-          WriteDelete –Authorize to delete data items from the data sets.

-          WriteMerge – Authorize to merge data.

-          WriteReplace – Authorize to replace data.

7.    Now let’s build a service and test it. Once you hit the web service in a browser, you will see the page as below

7

The above feed representing the data as per Atom Syndication format. There are two entity collections namely “Levels” and “NodeTypes”.

-     You can query levels by navigating to

http://localhost:32555/WcfDataService1.svc/Levels

-     Also, apply filter using filter property as follows

     http://localhost:32555/WcfDataService1.svc/Levels?filter= LevelTree gt 4

o    “gt” is used for greater than where as “lt” is used for less than.

        Consuming WCF Data Service in Web Application

1.    Create a new Web Application project in Visual Studio

2.    Add a service reference by right clicking on Add Service Reference and select the newly service created.

7

3.       Add a button to call the service on its click event.

4.       On the  click event you can consume the service like below

  Services.DMSEntities.DDMSEntities entities = new Services.DMSEntities.DDMSEntities(new Uri("http://localhost:32555/WcfDataService1.svc"));

  var lst = entities.Levels.FirstOrDefault();

This will return the first item in the collection. You can execute all CRUD operations depending on the rule set in the Service for that entity.

Hope this helps, happy coding!

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}