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

Adding custom operations on OData service


By default, WCF Data Service will expose all data object collections provided by the data source in the format of OData entity sets. In addition to this, we can also add custom methods/operations on a given WCF Data Service. By using such custom operations, we can further extend our OData services so as to expose additional data in arbitrary formats, such as XML, JSON, and Binary.

In this recipe, we will demonstrate how to add custom operations to a WCF Data Service.

Getting ready

This sample case still uses the same ADO.NET Entity Framework based WCF Data Service like what we've discussed in the previous recipes. We will add some custom operations to it so as to expose additional data to client.

The source code for this recipe can be found in the \ch01\CustomOperationServiceSln\ directory.

How to do it...

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

  2. 2. Create an ADO.NET Entity Framework based WCF Data Service through the Northwind sample database.

  3. 3. Add custom operations into the WCF Data Service class.

    We will add two operations here, one for retrieving the current time on service server (return DateTime value) and another for retrieving some test data entities of the Category entity type (see the following code snippet).

    public class NWDataService : DataService<CustomOperationService.NorthwindEntities>
    {
    ......
    [WebGet]
    public DateTime GetServerTime()
    {
    return DateTime.Now;
    }
    [WebGet]
    public IQueryable<Category> GetDummyCategories()
    {
    var cates = new List<Category>();
    cates.Add(new Category() { CategoryID = 1, CategoryName = "Category 1", Description = "Desc of Category 1" });
    cates.Add(new Category() { CategoryID = 2, CategoryName = "Category 2", Description = "Desc of Category 2" });
    cates.Add(new Category() { CategoryID = 3, CategoryName = "Category 3", Description = "Desc of Category 3" });
    return cates.AsQueryable();
    }
    }
    

    As the shown in the previous code, both operation functions need to be public and non-static member methods of the service class.

    Note

    Don't forget the WebGetAttribute attribute on the declaration of each operation.

  4. 4. Enable the operation access rules in the service initialization code (see the following code snippet).

    public static void InitializeService(DataServiceConfiguration config)
    {
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    config.SetEntitySetAccessRule("*", EntitySetRights.All);
    config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
    }
    
  5. 5. Select the .svc file to launch the service and directly invoke the custom operations (by typing the operation address) in the web browser.

    The following screenshot shows the web browser window after invoking the GetServerTime operation:

    The following is the output obtained by invoking the GetDummyCategories operation:

How it works...

As shown in the previous sample code, we can add custom operations to WCF Data Service in the same way as we add service operations to a standard WCF REST service. Also, the WebGetAttribute attribute over each sample operation indicates that the operation can be accessed through the HTTP GET method (the expected method for operations that return data). We can also apply the WebInvokeAttribute attribute so as to make the operation support other HTTP methods.

Also, in order to allow clients to invoke custom operations, we need to grant the access rules in the InitializeService function just like we do for entity sets exposed in WCF Data Service.

For more information about access rules and permission configuration on WCF Data Service, see Chapter 7, Working with Security.

See also

  • Building an OData service via WCF Data Service and ADO.NET Entity Framework recipe