Tuesday, June 17, 2014

Step by step how to create a WCF OData REST application in Asp.Net MVC


In this tutorial we'll learn how to create a WCF OData REST application in Asp.Net MVC, with support for HTTP GET requests, in 5 minutes
The OData (Open Data) protocol is a REST  protocol which supports all CRUD operations (Create - Read - Update - Delete) against resources exposed as data services endpoints. For this tutorial, we'll  add an  OData REST  application that will handle ONLY HTTP GET requests. The complete, and larger, HTTP POST - HTTP PUT & HTTP DELETE tutorial can be found HERE.
We'll start with a working MVC application, which will use an SQL database, containing the following entities and relationships :

In order to create the Data Service, we add a folder (to hold the service) to the project :


Next, we add a New Item :



At WEB tab , search for WCF items , and select WCF Data Service :

The automatic C# code we receive at the .cs file of the Data Service is as follows :

We adapt it to our application, by adding the relevant Data Source , in our case, the Entity Model :



Then, we will add an ODATA  Data Service to expose the data as read only collections. The OData metadata is exposed in a specification called CSDL (Conceptual Schema Definition Language), which is different from the WSDL (Web Services Description Language) of the SOAP protocol. The former can be fetched using the $metadata operator. Now, we give READ ONLY permission to retrieve only the "Blog" entities, as follows :

Notice that we write Blogs in PLURAL, because that's the name of the collection.
Now we're done, but what's the URL to get the collection? We just chain the name of the server, the folder ("ODATA" in our case) , the Data Service name ("BlogData Service.svc") , and the name of the collection ("Blogs") :

As you can see :



Take into account : 
OData is case-sensitive !!!!!!! If you write "BLOGS", you don't get the collection :



But if it's written properly, we get all entries :



Now you can select some specific entity :


Also, you can get all METADATA from the Service :


Take a look at MSDN to see all ODATA QUERY OPTIONS that you can send in the requests :

(source : "Open Data Protocol by Example" - by Chris Sells)
(source : "Open Data Protocol by Example" - by Chris Sells)

For instance, you can use /$count to get the number of entities in a collection :


You can get the VALUE of some field :






Also, you can use $indexof( field , "  "  ) to search text :

The operators are specified as follows :

(source : "Open Data Protocol by Example" - by Chris Sells)

Now, because MVC performs route mapping , it will try to map the ODATA REST request to one of the controllers at the application. It couldn't find a controller, so will return a 404 NOT FOUND error code.
Therefore , let's say MVC not to map any request destinated to our WCF OData REST service, adding the following code to the RouteConfig.cs file :

routes.IgnoreRoute("ServiceFolder/ServiceName.svc/{*pathInfo}");



There is another issue while creating an OData WCF Data Service. If you take a look at the references of your MVC Visual Studio project, that are related to ODATA, you'll possibly see something like this : 

Microsoft.Data.Edm.dll    5.2.0.0
Microsoft.Data.OData.dll 5.2.0.0
System.Spatial.dll            5.2.0.0

But the Microsoft.Data.Services appears being of version 5.0.0.0 : that's not good : the version of the assemblies should essentially be the same. Elsewhere, there could be methods missing and whatever other problems of lack of coordination between them.
So open the Package Manager Console and run the following expression, to load the SAME ASSEMBLY VERSION :

> Install-Package Microsoft.Data.Services -Version 5.2.0

This will install the required DLL and next remove the former version of the assembly.

You can also require in the web.config to load the new version of the assembly, but it is not strictly necessary to do so:

<runtime>   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">     <dependentAssembly>       <assemblyIdentity name="Microsoft.Data.Services"                     publicKeyToken="31bf3856ad364e35" />             <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.2.0.0" />     </dependentAssembly>    </assemblyBinding> </runtime>

Also, there is a definition  of the version of the   Microsoft.Data.Services  assembly to be loaded:




Change the version to 5.2.0.0 , to load the proper assembly.

That's all!! 
In this tutorial we've learned how to create a WCF OData REST application in Asp.Net MVC, with support for HTTP GET requests, in 5 minutes . 

Happy programming.....
       By Carmel Shvartzman
כתב: כרמל שוורצמן





No comments:

Post a Comment