
Exposing OData endpoints from WCF RIA Service
WCF RIA Service is one of the great extension components based on the standard WCF service. WCF RIA Service is designed for building data access services (for n-tier solutions), which will not only expose data sets to clients but also encapsulate most of the business/application logics at service layer. With the latest WCF RIA Service version, we can make a WCF RIA Service expose data through various kinds of endpoints such as SOAP, OData, and JSON.
In this recipe, we will show you how to open an OData endpoint from an existing WCF RIA Service.
Getting ready
To play with WCF RIA Service, we need to install Visual Studio 2010 Service Pack 1, which includes the runtime and development tools for WCF RIA Service V1 SP1.
Visual Studio 2010 Service Pack 1 is available at http://support.microsoft.com/kb/983509.
The source code for this recipe can be found in the \ch01\ODataRIAServiceSln\
directory.
How to do it...
- Create a new ASP.NET Empty Web Application.
- Create the ADO.NET Entity Framework data model from the sample database.
The following screenshot shows the class diagram of the data model created from the Northwind sample database (four tables are included):
- Create a new WCF RIA Service by using the Domain Service Class item template in Visual Studio (see the following screenshot).
- Specify the service options (especially the one for enabling an OData endpoint) in the Add New Domain Service Class dialog (see the following screenshot).
The following are all the options we need to set for a new WCF RIA Service:
- Domain Service Class name: This is the type name of our RIA service class.
- Available DataContext/ObjectContext classes: This is the data model class we will use for providing the underlying data objects. Make sure we have saved all items in the project so that the ADO.NET Entity Framework data model class will appear in the drop-down list.
- Enable client access and Expose OData endpoint options: As the name explains, these two options will enable the RIA service to be accessed from client applications and also add an additional endpoint on it so as to expose data entities in an OData compatible format.
- Create a
.svc
file as the service access endpoint for the WCF RIA Service.In the
.svc
file, we need to specify theServiceHostFactory
andService
types through the@ServiceHost
directive (see the following code snippet).<%@ ServiceHost Language="C#" Debug="true" Service="ODataRIAService.NWDomainService" Factory="System.ServiceModel.DomainServices.Hosting.DomainServiceHostFactory, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
As shown in the previous
@ServiceHost
directive, we need to supply the full name (including namespace and assembly name) of theServiceHostFactory
type in theFactory
attribute.Tip
If you use the WCF service item template to create a new
.svc
file, Visual Studio will generate theServiceContract
andService
implementation code files automatically. To prevent this, you can create a Text or XML file instead and manually change the file extension to.svc
(and adjust the file content correspondingly). - Launch the WCF RIA Service and access its OData endpoint by adding the odata/ suffix to the URL.
By adding the odata/ suffix to the URL over the base service address, we can reach the OData endpoint exposed by the WCF RIA Service. The default output of the OData endpoint is just the same as a standard WCF Data Service (see the following screenshot).

How it works...
When creating the sample WCF RIA Service, we enable the OData endpoint on it by selecting the Expose OData endpoint option in the Add New Domain Service Class dialog. Actually, we can find the magic behind the dialog within the web.config
file (see the following configuration fragment).

The dialog adds a domainServices/endpoints/add
element in the<system.serviceModel>
section. This element tells the runtime to add a new endpoint for each WCF RIA Service and this endpoint will generate an OData format response (by using the System.ServiceModel.DomainServices.Hosting.ODataEndpointFactory
type).
Likewise, if you have some existing WCF RIA Services, which were created without the OData endpoints enabled, we can simply make them OData enabled by adding the previous configuration settings manually in the web.config
file.
See also
- Building an OData service via WCF Data Service and ADO.NET Entity Framework recipe