Presentation is loading. Please wait.

Presentation is loading. Please wait.

REST Applications in.NET Representational State Transfer Ben Dewey twentySix New York

Similar presentations

Presentation on theme: "REST Applications in.NET Representational State Transfer Ben Dewey twentySix New York"— Presentation transcript:

1 REST Applications in.NET Representational State Transfer Ben Dewey twentySix New York


3 Overview What is REST Rules and Guidelines Hosting a REST Service in.NET Consuming a REST Service with.NET Samples Discussion

4 What is REST? Roy Fielding – – Client-Server – Stateless – Cachable – Layered System – Code-on-demand (optional)

5 HTTP Methods

6 Courtesy of RESTful.NET by Jon Flanders

7 HTTP Status Codes Status RangeDescriptionExamples 100Informational100 Continue 200Successful200 OK 201Created 202Accepted 300Redirection301 Moved Permanently 304Not Modified 400Client error401 Unauthorized 402Payment Required 404Not Found 405Method Not Allowed 500Server error500 Internal Server Error 501Not Implemented

8 HTTP Messages GET / HTTP/1.1 User-Agent: Fiddler Host: localhost:9999 HTTP/ OK Content-Length: 256 Content-Type: application/xml; charset=utf-8 Server: Microsoft-HTTPAPI/2.0 Date: Tue, 17 Nov :11:11 GMT

9 URIs (Uniform Resource Identifiers) RPC (SOAP) Interface getYears getMakes getAutomobile updateAutomobile deleteAutomobile

10 URIs (Uniform Resource Identifiers) RESTful Interface – No need to distinguish get/update/delete, thats what the HTTP Method is for – Collection – Resource

11 Return Types XML – XHTML – ATOM Publishing Protocol – RSS – Custom (should contain hyperlinks) JSON Custom

12 WADL Web Application Definition Language WSDL for REST


14 Hosting REST in.NET Windows Communication Foundation (WCF) – Web* additions in.NET 3.5 WebHttpBinding, WebOperationContext, WebGet/Invoke Attributes, etc UriTemplates for Routing – REST Starter Kit Preview 2 – WCF Hosting (Console, IIS, etc) ASP.NET MVC – JsonResults and XmlResults for serialization – System.Web.Routing engine for routing

15 Hosting WCF (Catch-All Controller) [ServiceContract] public interface IAutomobileService { [OperationContract] [WebGet(UriTemplate = "*")] Message Get(); [OperationContract] [WebInvoke(Method = "PUT", UriTemplate = "*")] Message Put(Message data); }

16 Hosting WCF (UriTemplates) [ServiceContract] public interface IAutomobileService { [OperationContract] [WebGet(UriTemplate = "/{year}/{make}/{model}/{engine}")] Automobiles GetAutomobiles(string year, string make, string model, string engine); }

17 Hosting WCF (REST Starter Kit)

18 Hosting MVC routes.MapRoute( "Default", "{year}/{make}/{model}/{engine}", new { controller = "Main", action = "Main", year = "", make = "", model = "", engine = "" } ); public ActionResult Main(int? year, string make, string model, string engine) { if (!year.HasValue) { return Xml(AutomobileRepository.GetYears()); } else { /* … */ } }

19 Consuming REST in.NET WebRequest WCF Interface Mocking HttpClient – Ships with REST Starter Kit Preview 2

20 WebRequest var httpRequest = (HttpWebRequest)WebRequest.Create( "http://localhost/service.svc"); using (var upStream = httpRequest.GetRequestStream()) { // some complicated logic to create the message } var response = httpRequest.GetResponse(); using (var downStream = response.GetResponseStream()) { // some complicated logic to handle the response message. }

21 WCF Interface Mock [ServiceContract] public interface ITwitterService { [OperationContract] [WebInvoke(UriTemplate = "/friendships/create/{user}.xml")] TwitterUser CreateFriendship(string user); } using (var factory = new WebChannelFactory ( new Uri(""))) { factory.Credentials.UserName.UserName = username; factory.Credentials.UserName.Password = password; var proxy = factory.CreateChannel(); var user = proxy.CreateFriendship("userToFollow"); }

22 HttpClient using (var client = new HttpClient()) { var postData = new StringContent(""); var url = ""; var user = client.Post(url, postData).EnsureStatusIsSuccessful().Content.ReadAsXmlSerializable (); }

23 Samples/Demos

24 Resources Roy Fieldings Dissertation REST in Windows Communication Foundation (WCF) RESTful.NET by Jon Flanders REST on Wikipedia A Guide to Designing and Building RESTful Web Services with WCF 3.5 Creating a RESTful Web Service Using ASP.Net MVC * These slides will be posted on my blog (

25 Discussion SOAP vs REST MVC vs WCF ATOM/RSS Easiest for JavaScript Consumption OAUTH WRAP

26 Session Wrap-Up Thoughts and Impressions – Positives and Deltas Discussion of Topics for Upcoming Meetings

Download ppt "REST Applications in.NET Representational State Transfer Ben Dewey twentySix New York"

Similar presentations

Ads by Google