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

Posted by: Brad Abrams on 03/16/2010

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 http://localhost:30045/myapp-web-DishViewDomainService.svc/OData/

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

image

And then drill in with this URL: http://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. 


About Brad Abrams

Brad Abrams

Brad Abrams was a founding member of both the Common Language Runtime, and .NET Framework teams at Microsoft Corporation where he is currently the Group Program Manager for the UI Framework and Services team which is responsible for delivering the developer platform that spans both clients and web based applications as well as the common services that are available to all applications. Specific technologies owned by this team include ASP.NET, Atlas, and Windows Forms.

Brad has been designing parts of the .NET Framework since 1998 when he started his framework design career building the BCL (Base Class Library) that ship as a core part of the .NET Framework. Brad was also the lead editor on the Common Language Specification (CLS), the .NET Framework Design Guidelines and the libraries in the ECMA\ISO CLI Standard. Brad has been deeply involved with the WinFX and Windows Vista efforts from their beginning

Brad co-authored Programming in the .NET Environment, and was editor on .NET Framework Standard Library Annotated Reference Vol1 and Vol2 and the Framework Design Guidelines

More About Brad »

NFJS, the Magazine

December Issue Now Available
  • BDD and REST

    by Brian Sletten
  • Mocks and Stubs in Groovy Tests

    by Kenneth Kousen
  • Algorithms for Better Text Search Results

    by John Griffin
  • Knowns and Unknowns of Scrum and Agile

    by Brian Tarbox
Learn More »