RIA Services - exposing OData service


Open Data Protocol (OData) is a web protocol which is used for querying and updating data. OData is being used to expose and access information from a variety of sources, including relational databases, file systems, content management systems, and traditional web sites. OData applies web technologies like http, Atom publishing protocol and JSON to provide access to information from different applications and services. It is emerging set of extensions for the Atom protocol.

In RIA Service it is easy to enable Odata service. When you add Domain Service, check the "Expose OData endpoint" checkbox in "Add New Domain Service Class" dialog box (you can go through my article RIA Services - using Entity Framework to see how to create domain service).

1.gif
 
When you enable OData endpoint for domain service you will notice two changes.

First for each query methods you want to expose using OData are marked with default query 

[EnableClientAccess()]
public class StudentDomainService : LinqToEntitiesDomainService<StudentEntities>
{
    [Query(IsDefault = true)]
    public IQueryable<student> GetStudents()
    {
        return this.ObjectContext.students;
    }
    [Query(IsDefault = true)]
    public IQueryable<student_detail> GetStudent_detail()
    {
        return this.ObjectContext.student_detail;
    }
}

And second, the endpoint for OData is added into domainService section of Web.config.

<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>

Now to see the Atom feed hit the below URL (change port number to port number the application is currently running in your system)

http://localhost:52878/RIASample-Web-StudentDomainService.svc/OData/

The URL is combination of namespace and domain service name. When you hit above URL you will get this output.

2.gif

If you can to see student detail set you can specify that in URL.

http://localhost:52878/RIASample-Web-StudentDomainService.svc/OData/student_detailSet/

Output in Firefox view source:

3.gif 

Output in Microsoft PowerPivot for Excel:

4.gif

Microsoft currently supports the Open Data Protocol (OData) in SharePoint Server 2010, Excel 2010 (through SQL Server PowerPivot for Excel), Windows Azure Storage, SQL Server 2008 R2, Visual Studio 2008 SP1.


Similar Articles