39,59 €
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.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 329
Veröffentlichungsjahr: 2012
Copyright © 2012 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: July 2012
Production Reference: 1180712
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-849685-92-4
www.packtpub.com
Cover Image by Sandeep Babu (<[email protected]>)
Author
Steven Cheng
Reviewers
Shayne Burgess
Ibrahim Sukru
Acquisition Editor
Dhwani Devater
Lead Technical Editor
Kedar Bhat
Technical Editors
Veronica Fernandes
Manasi Poonthottam
Zinal Shah
Copy Editors
Brandt D'Mello
Laxmi Subramanian
Project Coordinator
Joel Goveya
Proofreader
Ting Baker
Indexer
Tejal R. Soni
Graphics
Valentina D'silva
Manu Joseph
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
Steven Cheng is a Senior Support Engineer at Microsoft CSS, China. He has been supporting Microsoft development products and technologies for more than seven years. He is also working actively in the Microsoft MSDN forum community.
His technical specialties have covered many popular Microsoft development technologies including .NET Framework, ASP.NET, XML WebService, Windows Communication Foundation, Silverlight, Windows Azure, and Windows Phone. His technical blog can be found at http://blogs.msdn.com/stcheng.
In 2010, he wrote the book Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications, Packt Publishing.
The publication of this book could not have been possible without the efforts put in by a large number of individuals. I would like to thank my colleagues Shayne Burgess, Yi-lun Luo, and Mog Liang who have given me lots of ideas and suggestions on the book recipes. And thanks goes to my friends Jasmine Gong and Le Fei who have helped me a lot during the entire book authoring lifecycle.
Most importantly, none of this would have been possible without the love and patience of my family. I would like to express my heartfelt gratitude to my family.
Lastly, I offer my regards and blessings to all of those who supported me in any respect during the completion of this book.
Shayne Burgess is a Program Manager on the SQL Server engineering team at Microsoft. He has worked on the OData team at Microsoft for the past four years, contributing to the definition of the OData protocol and building Microsoft implementations of OData.
Ibrahim Sukru is a Software Engineer from Istanbul. He is the founder of xomila.com. He developed several RESTful web services with ASP.NET MVC, OData, and WCF. He loves web standards and technologies, HTML, CSS, and Microformats and enjoys contributing to open source software and coffee.
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
Get notified! Find out when new books are published by following @PacktEnterprise on Twitter, or the Packt Enterprise Facebook page.
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.
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.
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:
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.
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.
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 <[email protected]>, 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.
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.
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.
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 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 <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
In this chapter we will cover:
Open Data Protocol (OData) is a web protocol for querying and updating data, which can be freely incorporated in various kinds of data access applications. OData makes itself quite simple and flexible to use by applying and building upon existing well-defined technologies, such as HTTP, XML, AtomPub, and JSON.
WCF Data Service is the main component for building OData service on .NET Framework platform. WCF Data Service supports exposing various data source models such as ADO.NET Entity Framework, LINQ to SQL, and CLR Objects through OData service endpoints. Also, we're not limited to these existing data models, we can build our own custom Data Service Provider or convert other services (such as WCF RIA service) to OData service. In this chapter, we will demonstrate several cases of using WCF Data Service to build OData services that can deal with different kinds of data source models.
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.
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.
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:
In the .svc file, we need to specify the ServiceHostFactory and Service types through the @ServiceHost directive (see the following code snippet).
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.
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).
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).
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.
