Telerik blogs

Read the other posts in this series:

In the previous blog posts listed above, I showed how Telerik’s new LINQ implementation works with WCF RIA Services. I showed how to build your own Domain Service, build custom query methods, and make a metadata class. In this post I will show how to expose your Domain Service as an OData feed.

The Open Data Protocol (OData) is a Web protocol for querying and updating data in a RESTful fashion. You create OData feeds when you want to set up feeds for 3rd parties to consume, typically without your knowledge. For example Twitter has a RESTful feed of all its public tweets and many applications will consume that feed.

You may use WCF RIA Services to create your application, however, you may want to expose parts of your application as a feed for others to consume. This is real easy to do. Let’s see how.

I will continue using the same project from the first three parts of this blog series. In the server (ASP.net) project you have to do three things. First set a reference to System.ServiceModel.DomainServices.Hosting.OData.

Next we have to configure an OData endpoint. You do this by adding the following to your web.config under the system.serviceModel node:

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

 
Lastly, you have to tell RIA Services what methods of your DomainService you want to expose. The methods you expose have to be an IQueryable and parameterless (which means the query methods in Part II are ineligible) and decorated with the IsDefult=true attribute. I will expose our GetCustomers() method from Part I as shown here by adding the attribute to the method:

 1: //enable
OData
 2: [Query(IsDefault = true)]
 3: public IQueryable<Customer>
GetCustomers()
 4: {
 5:  return this.DataContext.Customers
 6:  .Where(c
=> c.Country == "Germany")
 7:  .OrderBy(c
=> c.CustomerID);
 8: }

 
Now you can run your project and view the OData feed from a browser. The format of the URL is the namespace+typename for the DomainService with dots replaced by hyphens followed by “.svc/odata/”. (Note, I have found that this is case sensitive and requires the terminating /.)

So for example, our Namespace is SilverlightApplication6.Web and our Domain Service is DomainService1, so our url would be http://servername/SilverlightApplication6-Web-DomainService1.svc/odata/

My URL is the following and the results are shown below:

http://localhost:1055/SilverlightApplication6-Web-DomainService1.svc/odata/

clip_image002

Now let’s explore the OData feed. Being a RESTful service you will access the feed and each resource via HTTP. The resource in this case will be the names of your Entities. What is great is that the OData feed respects the business rules of your RIA Service (since it is using the same DomainService), so you don’t have to worry about data leakage, nor duplicate any work replicating your business rules. Let’s drill down into the CustomerSet:

http://localhost:1055/SilverlightApplication6-Web-DomainService1.svc/odata/CustomerSet

clip_image004

That is it. You can then consume the feed from an iPhone app, .NET application, Excel PowerPivot, or any other application that supports HTTP and XML (which is pretty much anything.)

Enjoy!


About the Author

Steve Forte

 sits on the board of several start-ups including Triton Works. Stephen is also the Microsoft Regional Director for the NY Metro region and speaks regularly at industry conferences around the world. He has written several books on application and database development including Programming SQL Server 2008 (MS Press).

Comments

Comments are disabled in preview mode.