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

Preface

OData (Open Data Protocol) is a web protocol for querying and updating data, which can be freely incorporated in various kind of data access applications. OData makes it quite simple and flexible to use by applying and building upon existing well-defined technologies such as HTTP, XML, AtomPub, and JSON.

WCF Data Services (formerly known as ADO.NET Data Services) is a well-encapsulated component for creating OData services based on the Microsoft .NET Framework platform. It also provides a client library with which you can easily build client applications that consume OData services. In addition to WCF Data Services, there are many other components or libraries, which make OData completely available to the non-.NET or even non-Microsoft world.

This book provides a collection of recipes that help .NET developers to become familiar with OData programming in a quick and efficient way. The recipes have covered most OData features from the former ADO.NET Data Services to the current WCF Data Services 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.

What this book covers

Chapter 1, Building OData Services, introduces how we can use WCF Data Services to create OData services based on various kind of data sources such as ADO.NET Entity Framework, LINQ to SQL, and custom data objects.

Chapter 2, Working with OData at Client Side, shows how to consume OData services in client applications. This will cover how we can use strong-typed client proxy, WebRequest class, and unmanaged code to access OData services. You will also learn how to use OData query options, asynchronous query methods, and other client-side OData programming features.

Chapter 3, OData Service Hosting and Configuration, discusses some typical OData service hosting scenarios including IIS hosting, custom .NET application hosting, and Windows Azure cloud hosting. This chapter also covers some service configuration scenarios such as applying basic access rules, exposing error details, and enabling HTTP compression.

Chapter 4, Using OData in Web Application, talks about how to take advantage of OData services for developing various data-driven web applications including ASP.NET Web Form application, ASP.NET MVC application, Silverlight web application, AJAX style web application, and PHP web application.

Chapter 5, OData on Mobile Devices, demonstrates how to use OData services in mobile application development. Recipes in this chapter will cover the most popular mobile device platforms including iOS, Android, and Windows Phone 7.

Chapter 6, Working with Public OData Producers, introduces some existing public products and services, which have adopted OData for exposing application data. The recipes in this chapter will demonstrate how to create client applications to consume data from these public OData producers.

Chapter 7, Working with Security, discusses some common and easy-to-use means for securing OData services. Topics covered in this chapter include applying Windows authentication, applying ASP.NET Forms authentication, using HTTPS transport, and implementing custom authentication/authorization code logic.

Chapter 8, Other OData Programming Tips, explores some trivial but useful OData programming topics. You will learn how to use some existing tools for testing and debugging OData services. This chapter also demonstrates how to consume OData services in Windows PowerShell scripts and Windows 8 Metro style applications.

Chapter 9, New Features of WCF Data Service 5.0 (OData V3), demonstrates some of the new features introduced in WCF Data Service 5.0 (OData V3). The new features covered in this chapter include geospatial types, "Any" and "All" query operators, Dynamic entity set URI resolving, Named Resource Stream, and custom Service Actions.

What you need for this book

All the recipes in this book are based on the .NET C# programming language. However, you don't have to be a very experienced C# Developer. In order to follow the recipes and run the corresponding sample code, you need a test environment with the following items:

  • A development machine with Windows 7 or Windows Server 2008 OS

  • Visual Studio 2010 Professional or Ultimate edition (with SP1)

  • SQL Server 2005 (or 2008) Developer (or Expression) edition with Northwind sample database installed

  • IIS 7.x (for Windows 7 or Windows 2008)

  • IE 9 web browser

  • Fiddler web debugger

For other software or components required by some specific recipes, they will be listed as prerequisites in the Getting ready section of the relevant recipe.

Who this book is for

If you are a .NET Developer and you want to learn how to use OData in real-world data access application development using a quick and efficient approach, then this book is for you. With this book you will be able to find quick and handy solutions for various kind of OData programming scenarios using Microsoft .NET Framework. To follow the recipes, you will need to be comfortable with .NET Framework, Visual Studio IDE, C# programming language, and the basics of web programming such as HTTP, XML, and JSON.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "Finally, it comes to the FileEntity class."

A block of code is set as follows:

public partial class InitSession : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session.Count == 0)
{
Session.Add("string item", "some text");
Session.Add("int item", 120);
Session.Add("boolean item", true);
Session.Add("date item", DateTime.Now);
Session.Add("array item", new int[]{1,2,3});
}
}
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

namespace ODataEFService
{
public class NWDataService : DataService< ODataEFService.NorthwindEntities >
{
public static void InitializeService(DataServiceConfiguration config)
{
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.SetEntitySetAccessRule ("*", EntitySetRights.All);
}
}
}

Any command-line input or output is written as follows:

DataSvcUtil.exe /in:Northwind.edmx /out:NWDataServiceProxy.cs

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Specify the necessary publish options in the Publish Web dialog."

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.