Uniform Interface
Total Page:16
File Type:pdf, Size:1020Kb
www.allitebooks.com www.allitebooks.com RESTful .NET www.allitebooks.com Other Microsoft .NET resources from O’Reilly Related titles Learning C# 3.0 Programming WCF Services Learning WCF RESTful Web Services Programming C# 3.0 .NET Books dotnet.oreilly.com is a complete catalog of O’Reilly’s books on Resource Center .NET and related technologies, including sample chapters and code examples. ONDotnet.com provides independent coverage of fundamental, interoperable, and emerging Microsoft .NET programming and web services technologies. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today for free. www.allitebooks.com RESTful .NET Jon Flanders Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.allitebooks.com RESTful .NET by Jon Flanders Copyright © 2009 Jon Flanders. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safari.oreilly.com). For more information, contact our corporate/ institutional sales department: (800) 998-9938 or [email protected]. Editor: John Osborn Indexer: Lucie Haskins Production Editor: Sumita Mukherji Cover Designer: Karen Montgomery Copyeditors: Amy Thomson and Audrey Doyle Interior Designer: David Futato Proofreader: Emily Quill Illustrator: Robert Romano Printing History: November 2008: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. RESTful .NET, the image of an electric catfish, and related trade dress are trademarks of O’Reilly Media, Inc. .NET is a registered trademark of Microsoft Corporation. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-0-596-51920-9 [M] 1226506094 www.allitebooks.com Table of Contents Foreword . ix Preface . xi 1. REST Basics . 1 Architecture of the World Wide Web 1 SOAP 4 REST 5 Resources and URIs 5 Uniform Interface 7 Resource Representations 9 Implementing a Simple RESTful Service Example 11 Resources 11 URIs and Uniform Interface 11 Representations 12 Interaction 13 Wrap-Up 14 Processes 16 Summary 16 2. WCF RESTful Programming Model . 19 Isn’t WCF All About SOAP? 19 Channels and Dispatching 19 HTTP Programming with WCF 3.0 22 Web Programming in WCF 3.5 27 WebHttpBinding 30 WebHttpBehavior 30 WebServiceHost 31 WebOperationContext 31 WebGetAttribute 32 UriTemplate 33 UriTemplate Literal Values 42 v www.allitebooks.com UriTemplate Special Values 43 UriTemplate QueryString 43 Summary 44 3. Programming Read-Only Services . 47 Using WebGetAttribute and UriTemplate 47 Data Formats 49 Message 50 DataContract 52 XmlSerializer 55 Hybrid Approach 57 Summary 58 4. Programming Read/Write Services . 59 POST, PUT, and DELETE 59 Using WebInvokeAttribute 60 Resources 60 URIs and Uniform Interface 60 Representations 61 Implementation 61 Summary 71 5. Hosting WCF RESTful Services . 73 WCF REST Hosting Isn’t a Special Case 73 Self-Hosting 74 Configuring, Opening, and Closing a ServiceHost 74 Base Addresses 79 ServiceHost Versus WebServiceHost 80 Custom ServiceHost 84 Hosting in IIS 86 ASP.NET Compatibility 92 Multiple Hostnames 95 Removing the .svc File Extension 96 Custom ServiceHostFactory 98 Hosting Wrap-Up 99 Summary 99 6. Programming Feeds . 101 Building a Feed with WCF 101 SyndicationItem 105 Formatters 107 Exposing a Feed on a Live URI 110 Feed Validation 111 vi | Table of Contents www.allitebooks.com Adding Links to a Feed 115 Summary 117 7. Programming Ajax and Silverlight Clients . 119 WCF Web Services and Ajax 120 JSON 123 JSON-Enabling a Service Endpoint 127 ASP.NET Ajax 132 Silverlight 1.0 141 Silverlight 2.0 142 Parsing XML in Silverlight 2.0 144 Parsing JSON in Silverlight 2.0 151 Consuming Feeds in Silverlight 2.0 152 Cross-Domain Security in Silverlight 2.0 154 Returning JSON and XML Conditionally with a Single Method 154 Summary 157 8. Securing REST Endpoints . 159 Authenticating: Self-Hosted Endpoints 159 Setting Endpoint Security: WebHttpBinding.Security’s Mode Property 161 Setting Authentication Requirements: WebHttpBinding’s Transport Property 165 Authenticating: Managed Hosting Endpoints 169 Authorizing Endpoints 170 Authorization with Impersonation 170 Role-Based Authorization 171 Summary 174 9. Using Workflow to Deliver REST Services . 175 Consuming REST Services from WF 175 The SendActivity Instance 176 The ReceiveActivity Instance 181 Stateless Workflow Services 182 Stateful Workflow Services 189 Summary 193 10. Consuming RESTful XML Services Using WCF . 195 Defining the Client 195 Generating the Contract 197 Creating the Resource Representations 199 Creating the ServiceContract 207 Using the Service 209 Client Extensibility 214 Table of Contents | vii www.allitebooks.com Summary 216 11. Working with HTTP . 217 Programming HTTP with WCF 217 IncomingWebRequestContext 219 OutgoingWebResponseContext 220 OutgoingWebRequestContext 222 IncomingWebResponseContext 223 Context Wrap-Up 223 Status Codes 224 201 — Created 227 404 — Not Found 229 Conditional GET 231 LastModified 232 ETags 236 Caching 239 Output Caching 239 HttpContext.Cache 241 Content-Type 242 Summary 243 A. WCF 3.5 SP1 . 245 B. ADO.NET Data Services . 257 C. ADO.NET Entity Framework Walkthrough . 273 Index . 279 viii | Table of Contents www.allitebooks.com Foreword I’m an RPC guy in my bones. I spent years of my life working with various remote procedure call technologies, so when SOAP came along, it seemed like the obvious next step on this path. To me, web services meant SOAP, period. Then REST appeared. When the RESTful approach first hit the scene, I wrote a short article describing it. At the end, I noted SOAP’s broad support, then closed with this: Still, even though SOAP is already quite well established, the ideas embodied in REST are worth understanding. Web services are still new, and REST makes a remarkably interesting contribution to the technology. For a SOAP guy in 2002, I thought I was being quite open-minded. The REST fans didn’t see it this way. My inbox sizzled with mail telling me that I was stupid for not immediately seeing REST’s innate superiority over the pure evil that was SOAP. My response was to completely ignore REST for the next several years. I didn’t write about it, I didn’t speak about it, and I wouldn’t even take questions on the topic during talks on web services. I was convinced that REST was the religion of a small band of fanatics, and rude ones at that. The common appellation for a REST fan— RESTafarian—seemed very appropriate to me, derived as it was from the name of an actual religion. These people were true believers, and I couldn’t share their faith. Yet REST was too cool to ignore forever. Once you get your mind around the approach (which doesn’t take long—it’s simple), REST’s beauty is evident. More important, REST’s utility is also evident. While SOAP and the WS-* protocols still have a significant role, REST is useful in many, many situations. To one degree or another, we’re all RESTafarians now. There’s no better evidence of this than Microsoft’s embrace of REST in Windows Communication Foundation (WCF). While it’s wrong to view this as marking the end of SOAP, WCF’s REST support is a big endorsement from what was once the strongest bunch of SOAP advocates. Developers now have a single foundation on which to build all kinds of web services. But while REST is simple, WCF is not. To really understand and exploit this part of WCF requires a knowledgeable and experienced guide. I don’t know anybody who’s ix better suited to this role than Jon Flanders. Along with being one of the smartest people I know, and one of the most capable developers, Jon is first-rate at explaining compli- cated things. Even to a long-time RPC guy like me, it’s clear that RESTful services will be a big part of the future. This book is the best introduction I’ve seen to creating and using these services with WCF. If you’re a WCF developer looking to enter the RESTful world, this book is for you. —David Chappell Chappell & Associates x | Foreword Preface I’ve been working with the Web throughout my entire software engineering career. I started out writing ASP pages and COM components. I then moved into the world of .NET with ASP.NET and ASMX web services. In 2004, I got involved with BizTalk Server, which pushed me even more into the world of services and XML. I worked with Windows Communication Foundation (WCF) in its early beta stages, before its release in 2007. At that time, the Microsoft world of services was focused on service-oriented architecture (SOA), SOAP, and the WS-* specifications as the preferred methods for building services.