Successfully reported this slideshow.

Ellerslie User Group - ReST Presentation


Published on

This presentation covers ReST and implementing ReSTful services on .Net. It was presented at the Ellerslie .Net user group on the 28th of May 2009.

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Ellerslie User Group - ReST Presentation

  1. 1. REST<br />REpresentationalState Transfer<br />
  2. 2. REST - Theory<br />Resources <br />Representations<br />Verbs<br />Links<br />Headers<br />HTTP Status Codes<br />
  3. 3. Resources<br />A resource is identified by a Uri.<br />Uri’s are considered Opaque.<br />http://localhost/api/customers<br />http://localhost/api/customers/1<br />
  4. 4. Representations<br />Fetching a resource returns a Representation.<br />Representations have a content type i.e. text/xml, application/json, text/html etc.<br />&lt;customer&gt;<br /> &lt;self&gt;http://localhost/api.svc/customers/12&lt;/self&gt;<br /> &lt;organisation&gt;DevDefined&lt;/organisation&gt;<br /> &lt;id&gt;1&lt;/id&gt;<br /> &lt;orders&gt;<br />http://localhost/api.svc/customers/12/orders<br /> &lt;/orders&gt;<br />&lt;/customer&gt;<br />
  5. 5. Verbs<br /><ul><li>HTTP Verbs are used to operate on resources.
  6. 6. GET must be Side Effect Free
  7. 7. PUT must be Idempotent.</li></li></ul><li>Links<br />Links are returned in representations.<br />Clients follow links, allowing them to transition state.<br />Using Links can provide elegant ways to scale out or integrate 3rd party services.<br />
  8. 8. Headers<br />If-Modified-Since<br />Etag<br />Cache-Control<br />Accept<br />Content-Type<br />Authorization<br />
  9. 9. HTTP Status Codes (Just a few...)<br />
  10. 10. Demos<br />WCF – Building a Simple Rest Service<br />Linq & REST – ADO.Net Data Services<br />Entity Framework<br />Custom classes<br />MVC meets REST - MonoRail<br />OAuth – RESTful Authorization<br />
  11. 11. Demo #1<br />WCF Demo - A quick tour of a WCF REST service.<br />webHttpBinding<br />WebGetvsWebInvoke<br />UriTemplates<br />GET, POST, PUT, & Delete<br />Changing the representation.<br />
  12. 12. Fresh to Fiddler?<br />Fiddler is very useful when developing REST services.<br />There are some catches for first time users:<br />Disable IPV6 in Fiddler when using Cassini (it doesn’t like the ::1 loopback address)<br />Use “.” after localhost when testing with IE 7 to stop proxy being bypassed.<br />i.e. http://localhost.:8080/api.svc/customers/<br />FireFox users - you need to manually configure proxy settings.<br />
  13. 13. WCF Gotchas<br />Exceptions end up as 500 errors on client. <br />You need to explicitly set status for non-server errors (i.e. For invalid requests)<br />Uri Templates are fussy<br />Trailing slashes will cause a match to fail, contrary to many peoples expectations.<br />HTTP Method names are case-sensitive.<br />Using “Post” instead of “POST” can make for confusing 405 errors <br />The input/output Representation format is defined at the contract operation level.<br />Targeting multiple representations “across the board” is ugly.<br />Support for linking resources is non-existent.<br />
  14. 14. Demo #2<br />ADO.Net Data Services (Was Astoria)<br />Building Service using ADO.Net Entity Framework.<br />Exploring Query Syntax.<br />
  15. 15. Entity Framework<br />Using EF - Out of scope <br />However... Provides easy way to quickly play with Astoria.<br />Add Item -&gt; Entity Model <br />Use AdventureWorks database, just the tables should do, call it “AdventureWorks”<br />Add Item -&gt; ADO.Net Data Service<br />Edit the .cs file, set entities class for service to AdventureWorksModel.AdventureWorksEntities.<br />Alter InitializeService method to look like this(Don’t do this for production use)<br />public static void InitializeService(IDataServiceConfigurationconfig)<br />{<br />config.SetEntitySetAccessRule(&quot;*&quot;, EntitySetRights.All);<br />config.SetServiceOperationAccessRule(&quot;*&quot;, ServiceOperationRights.All);<br />}<br />
  16. 16. Querying -Simple<br />
  17. 17. Querying – Advanced<br />
  18. 18. Custom Objects – Demo #3<br />ADO.Net Data Services isn’t limited to the EF.<br />Anything implementing IQueryable interface will work too.<br />Let’s try!<br />
  19. 19. Implementation<br />
  20. 20. ADO.Net Data Entities - Client<br />Entities comes with a .Net Client.<br />Found in the System.Data.Services.Client assembly.<br />Client allows Linq -&gt; Uri -&gt; Linq -&gt; Whatever.<br />With different Types on the client and Server!<br />Client types can be POCO objects, even if the server side representations are not.<br />[DataServiceKey(...)] attribute required to support non-GET requests.<br />Client implements a “unit of work” like pattern, allowing changes to be collected and then persisted at once.<br />Changes can be batched in a single request (though this can be viewed as a perversion of HTTP principles).<br />