Silverlight 4 + RIA Services - Ready for Business: Exposing OData Services

image OData is an emerging set of extensions for the ATOM protocol that makes it easier to share data over the web. To show off OData in RIA Services, let’s continue our series.       We think it is very interesting to expose OData from a DomainService to facilitate data sharing.   For example I might want users to be able to access my data in a rich way in Excel as well as my custom Silverlight client.   I’d like to be able to enable that without writing multiple services or duplicating any business or data access logic. 

This is very easy to enable with RIA Services.  In fact it is just a check box away!    When you create your DomainService simply check the “Expose OData endpoint” and that will expose your DomainService as an OData feed. 

image

If you have already created a DomainService it is easy to enable OData on it as well by doing the two things this wizard does. 

First, it adds an endpoint to the domainServices section of the web.config. 

 

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

 

Second, on each paramaterless query methods you wish to expose via OData, mark the query as being default.  Meaning any time there is an ask for “Plate” it is this query method that is used. 

 

         [Query(IsDefault = true)]
        public IQueryable<Plate> GetPlates()
        {

once these are done, you an hit the service and see an Atom feed.  The format of the URL is the namespace+typename for the domainservice with dots replaced by dashs followed by “.svc/Odata/”.  So if the DomainService class is

MyApp.Web.DishViewDomainService then the URL would be https://localhost:30045/myapp-web-DishViewDomainService.svc/OData/

https://localhost:30045/myapp-web-DishViewDomainService.svc/OData/PlateSet

image

And then drill in with this URL: https://localhost:30045/myapp-web-DishViewDomainService.svc/OData/PlateSet

image

That is really cool that the data is in an open ATOM based format… but what is even better is there is a budding ecosystem of clients that can consume this feed.  One of the more interesting ones is the Excel addin called PowerPivot

Once you have it installed with Excel 2010. select the PowerPivot window

image

image

image

image

image

image

Then you can use the full power of excel so if i want to sort by Number of Updates, with a rating of 4 or higher, with calorie count between 3000 and 4000 then go graph that in some interesting way, you can do the easily..  All with the live data without any custom application code. 

 

What we showed in this walk though is how to expose OData from your DomainService and consume that in Excel.  This is just a down payment on the OData support coming in the future in RIA Services.