Rest and the WCF Web API


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Rest and the WCF Web API

  1. 1. REST and the WCF Web API <br />Getting started with REST services and the WCF Web API<br />Maurice de Beijer<br />
  2. 2. Objectives<br />What are REST services<br />Why REST instead of SOAP<br />The WCF Web API framework<br />The next webinar on the 25th of October is about creating HTML5/JavaScript clients for REST services<br />
  3. 3. What are REST services<br />Representational State Transfer<br />A way of creating web services<br />Not using the WS-* SOAP specifications<br />All about resources<br />Not about calling functions, RPC<br />Embraces HTTP<br />Not just a way of getting data across the wire<br />Leverages the structure of the Internet<br />URLS<br />MIME Media Types<br />HTTP Methods<br />Caching<br />Security<br />
  4. 4. The origin of REST<br />Architectural Styles and the Design of Network-based Software Architectures<br />By Roy Thomas Fielding<br />Chapter 5<br />One of the authors of the HTTP specification<br />
  5. 5. Why are REST services popular<br />Almost every platform supports consuming them<br />All you need is an HTTP client stack<br />Many services are build around resources<br />CRUD style<br />Enables very scalable services<br />Build around the technology that powers the Internet<br />
  6. 6. The WCF Web API framework<br />On CodePlex<br /><br />Use NuGet from within Visual Studio 2010<br /><br />Current version 0.5<br />Much better than the original WCF support for REST<br />But doesn’t replace it<br />
  7. 7. The resource URL<br />The URL is the key to a resource<br /> (The collection of books)<br /> individual book)<br />A given URL always returns the same resource<br />Deleting one resource doesn’t change the URL of another resource<br />But a resource can have multiple URL’s<br /><br /><br />
  8. 8. Different request/response formats<br />Uses standard HTTP MIME Media Types<br /><br />Content Negotiation<br />The client specifies a list of acceptable media types<br />The server chooses the best supported media type to return<br />XML<br />Good general purpose data format<br />JSON<br />Optimised for usage with JavaScript clients<br />Custom formats<br />Use one of the many IANA types<br />Define your own<br />Normally prefixed with vnd.<br />Example: application/vnd.wordperfect<br />
  9. 9. HTTP Status codes<br />Tell the client application what happened<br />Successful 2xx<br />200 OK<br />201 Resource created<br />Redirection 3xx<br />301 Moved Permanently<br />Client Error 4xx<br />400 Bad request<br />404 Not found<br />Server Error 5xx<br />500 Internal Server Error<br />503 Service Unavailable<br />
  10. 10. HTTP GET Method<br />No side effects<br />Can retrieve it as often as you like<br />Allows for caching<br />Request may never get to the server<br />Makes the web as scalable as it is<br />Use Accept header to specify the acceptable media type<br />Multiple types are supported<br />Examples:<br />Accept: application/xml<br />Accept: application/json<br />Accept: image/jpeg, image/*; q=0.5<br />
  11. 11. Other HTTP Methods<br />POST<br />Creates new resource<br />Returns the URL to the created resource<br />PUT or PATCH<br />Replaces/updates resource<br />Idempotent<br />PATCH only replaces a part of the resource<br />DELETE<br />Removes resource<br />Idempotent<br />
  12. 12. Microformats<br />Enable a much smarter client application<br />Embed allowed actions with a resource<br />Client can update the UI based on the resource<br />OData is a good example<br />Based on AtomPub<br /><br />Roll your own<br />Or reuse an existing one from IANA<br />Set the content-type<br />Use your own application/<br />
  13. 13. Rest is not perfect<br />No metadata<br />Little tooling in Visual Studio 2010<br />It’s harder than it seems<br />There is no official REST standard<br />There are many strong opinions about what is REST and what is not<br />
  14. 14. Usefull resources<br />Essential Windows Communication Foundation 4 Training<br /><br />Hypertext Transfer Protocol -- HTTP/1.1<br /><br />The Atom Syndication Format<br /><br />The Atom Publishing Protocol<br /><br />Open Data Protocol (OData)<br /><br />WCF Web API Contrib<br /><br />Fiddler<br /><br />
  15. 15. Summary<br />REST is all about resources<br />CRUD actions<br />REST embraces HTTP and the web<br />HTTP isn’t just a means of moving bits<br />REST services are a good way to build scalable services<br />Build on the infrastructure of the web<br />REST allows for very flexible services<br />You control every part of the message<br />