Book Image

OData Programming Cookbook for .NET Developers

By : Juntao Cheng
Book Image

OData Programming Cookbook for .NET Developers

By: Juntao Cheng

Overview of this book

Odata (Open Data Protocol) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData enables data access among a variety of applications, services, and stores by adopting existing Web technologies such as HTTP, XML, and JSON. This book deals with common OData programming cases over the Microsoft .NET Framework platform and eases the learning curve for a .NET developer to start incorporating OData in data service development.This book provides a collection of recipes that help .NET developers to get familiar with OData programming in a quick and efficient manner. The recipes cover most OData features from the former ADO.NET Data Service to the current WCF Data Service platform. In addition, all the sample cases here are based on real-world scenarios and issues that .NET developers might come across when programming with OData in application development.This book will be your handy guide with basic to advanced walkthroughs of common OData programming cases for the Microsoft .NET Framework platform. You will learn quick solutions to necessary tasks to integrate the power of OData at both server-side and client-side.This book will help you master the use of OData with .NET Framework by taking you through hands-on and practical recipes. It starts by talking about the common means for building OData services and consuming OData services in client applications. Then, some more specific topics like hosting, configuration and security are discussed. The book also covers many popular and interesting topics such as integrating OData in web applications, and developing data-driven mobile applications with OData. Moreover, you can also find quite a few recipes discussing real-world OData producers and new features in latest and future versions.Within "OData Programming Cookbook for .NET Developers", all the recipes are selected based on real-world scenarios that you will commonly come across. Each recipe covers a specific topic, going from the description of the problem, through a conceptual solution, to a solution containing sample code. By following these recipes, you can acquire how to program with OData in a simple, effective, and easy manner.
Table of Contents (15 chapters)
OData Programming Cookbook for .NET Developers
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

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

  1. 1. Create a new ASP.NET Empty Web Application.

  2. 2. 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):

  3. 3. Create a new WCF RIA Service by using the Domain Service Class item template in Visual Studio (see the following screenshot).

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

  5. 5. Create a .svc file as the service access endpoint for the WCF RIA Service.

    In the .svc file, we need to specify the ServiceHostFactory and Service 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 the ServiceHostFactory type in the Factory attribute.

    Note

    If you use the WCF service item template to create a new .svc file, Visual Studio will generate the ServiceContract and Service 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).

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