2. REST - Theory Resources Representations Verbs Links Headers HTTP Status Codes
3. Resources A resource is identified by a Uri. Uri’s are considered Opaque. http://localhost/api/customers http://localhost/api/customers/1
4. Representations Fetching a resource returns a Representation. Representations have a content type i.e. text/xml, application/json, text/html etc. <customer> <self>http://localhost/api.svc/customers/12</self> <organisation>DevDefined</organisation> <id>1</id> <orders> http://localhost/api.svc/customers/12/orders </orders> </customer>
10. Demos WCF – Building a Simple Rest Service Linq & REST – ADO.Net Data Services Entity Framework Custom classes MVC meets REST - MonoRail OAuth – RESTful Authorization
11. Demo #1 WCF Demo - A quick tour of a WCF REST service. webHttpBinding WebGetvsWebInvoke UriTemplates GET, POST, PUT, & Delete Changing the representation.
12. Fresh to Fiddler? Fiddler is very useful when developing REST services. There are some catches for first time users: Disable IPV6 in Fiddler when using Cassini (it doesn’t like the ::1 loopback address) Use “.” after localhost when testing with IE 7 to stop proxy being bypassed. i.e. http://localhost.:8080/api.svc/customers/ FireFox users - you need to manually configure proxy settings.
13. WCF Gotchas Exceptions end up as 500 errors on client. You need to explicitly set status for non-server errors (i.e. For invalid requests) Uri Templates are fussy Trailing slashes will cause a match to fail, contrary to many peoples expectations. HTTP Method names are case-sensitive. Using “Post” instead of “POST” can make for confusing 405 errors The input/output Representation format is defined at the contract operation level. Targeting multiple representations “across the board” is ugly. Support for linking resources is non-existent.
14. Demo #2 ADO.Net Data Services (Was Astoria) Building Service using ADO.Net Entity Framework. Exploring Query Syntax.
15. Entity Framework Using EF - Out of scope However... Provides easy way to quickly play with Astoria. Add Item -> Entity Model Use AdventureWorks database, just the tables should do, call it “AdventureWorks” Add Item -> ADO.Net Data Service Edit the .cs file, set entities class for service to AdventureWorksModel.AdventureWorksEntities. Alter InitializeService method to look like this(Don’t do this for production use) public static void InitializeService(IDataServiceConfigurationconfig) { config.SetEntitySetAccessRule("*", EntitySetRights.All); config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); }
20. ADO.Net Data Entities - Client Entities comes with a .Net Client. Found in the System.Data.Services.Client assembly. Client allows Linq -> Uri -> Linq -> Whatever. With different Types on the client and Server! Client types can be POCO objects, even if the server side representations are not. [DataServiceKey(...)] attribute required to support non-GET requests. Client implements a “unit of work” like pattern, allowing changes to be collected and then persisted at once. Changes can be batched in a single request (though this can be viewed as a perversion of HTTP principles).
Editor's Notes
Uri’s are considered opaque – don’t try to pull them apart to infer additional meaning, you can only reliably rely on the representations returned from the GET’ing the uri and it’s associated metadata in headers to discover meaning about the resource.
When you GET a resource you pull back a representation, it could be anything.Not all resources will provide enough fidelity to round-trip them.Representations should be strict out, loose in... This could mean using an XSD to validate output your service produces, but to be quite forgiving with what a client posts or puts back i.e. Using xpath to fetch only the elements you require, and to default missing elements to reasonable values.
Unlike web services, which are effectively remote procedure calls, normally aligned with business processes – REST has a set of verbs, and each verb has a specific set of behaviour, working at a resource level.
Add System.ServiceModel.Web reference.AddWebGet attributes.Add UriTemplate parameter to WebGet attribute.*Change wsHttpBinding to webHttpBindingExplain lack of “Self” or “Links” to related resources – need to be manually injected into DTO (Linq is good for this)
Create service project.Remove default service, clean up the web.config.Select AdventureWorks.Select just tables.Add ADO.Net Data Service Item (under web tab, easier to find that way)Set the missing <T> parameter on DataService<T>... Set the access rules for entities and operations to be “free for all”.
Show navigating to a workspaceDrill into Employee collectionDisplay first EmployeeDisplay contact details for first EmployeeDisplay gender property of employeeDisplay department history for a shift...
Show navigating to a workspaceDrill into Employee collectionDisplay first EmployeeDisplay contact details for first EmployeeDisplay gender property of employeeDisplay department history for a shift...
Add “SystemEntities” class exposing Queryable set of process DTO’s Process are returned by query via System.Diagnostics.Process.GetProcesses.Mention that we need to let ADO.Net data services know what the key is for out entity – and for that we need to reference another assembly: System.Data.Services.Client