[OData] Create and expose your own OData flow

OData (Open Data Protocol) is a protocol on top of HTTP, based on REST architecture, that allows remote edition and use for different clients (RIA, Web, Mobile, etc…). So one can use Linq to request data from a client interface.


With WCF RIA Services, it becomes possible to expose your own data easily. When you add a DomainService, you must check the box “Expose OData endpoint” and the selected tables will be exposed. In our example, we will expose the Customers and Orders tables from Northwind database.



 


Checking the box brings two important modifications to our code:


  • In the DomainService, now each parameterless methods have a specific tag :

[Query(IsDefault = true)]
public IQueryable<Customers> GetCustomers()
{
  return this.ObjectContext.Customers;
}


  • In the Web.Config, an “endpoints” is added in the domainServices area :


<system.serviceModel>
  <domainServices>
      <endpoints>
         <add name=”OData”
              type=”System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory,
                    System.ServiceModel.DomainServices.Hosting.OData,
                    Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″
/>
      </endpoints>
  </domainServices>
</system.serviceModel>




Now you can compile, run the application and query in a browser the following URL (which is built like that):


http://localhost:1234/SolutionName-Web-DomainServiceName.svc/OData/


N.B : if the application name and/or the DomainService name contains dots “.”, you must replace them by dashes “-“.


In our example the correct URL is: http://localhost:2321/RIAOData-Web-NorthwindService.svc/OData/


At the end, we can see all the tables we had selected before.



 


If you want to consult the data from a specific table, Customers for example, you can just complete the URL with the same table name:


http://localhost:1234/SolutionName-Web-DomainServiceName.svc/OData/CustomersSet/


The data have been formatted like for ATOM, the browser tries to display it like a RSS feed. So with a right-click in the browser page, you can select the option “View Source” to see the Customers data:



 


If one wants to practice, it can use the OData website where a lot of exposed data can be found. A feed list is available on the OData website.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>