Download pptx - There is time for rest

Transcript
Page 1: There is time for rest

www.softserve.ua

There is time for REST

WCF REST Services

Page 2: There is time for rest

www.softserve.ua

Overview

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

Page 3: There is time for rest

www.softserve.ua

What is REST?

• Roy Fieldinghttp://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

• Client-Server• Stateless• Cachable• Layered System

Page 4: There is time for rest

www.softserve.ua

REST Constraints

• Separation resource from representation• Uniform interface• Self-descriptive messages• Hypermedia as engine of application state

Page 5: There is time for rest

www.softserve.ua

HTTP Methods

Page 6: There is time for rest

www.softserve.ua

HTTP Methods

• Retrieves a resource• Guaranteed not cause side-effect (SAFE)• Cacheable

GET

• Creates new resource• Unsafe, effect of this verb isn’t defined by

HTTPPOST

• Updates existing resource• Can call N times, same thins will always

happen (idempotent)PUT

• Removes an resource• IdempotentDELETE

Page 7: There is time for rest

www.softserve.ua

HTTP Status Codes

Status Range Description Examples

100 Informational 100 Continue

200 Successful 200 OK

201 Created

202 Accepted

300 Redirection 301 Moved Permanently

304 Not Modified

400 Client error 401 Unauthorized

402 Payment Required

404 Not Found

405 Method Not Allowed

500 Server error 500 Internal Server Error

501 Not Implemented

Page 8: There is time for rest

www.softserve.ua

HTTP Messages

GET http://localhost:1128/api?$skip=2&$top=2 HTTP/1.1User-Agent: FiddlerHost: localhost:1128Accept: application/xml

HTTP/1.1 200 OKCache-Control: privateContent-Length: 157Content-Type: application/xml; charset=utf-8Server: Microsoft-IIS/7.5Date: Fri, 26 Aug 2011 18:51:22 GMT

<?xml version="1.0" encoding="utf-8"?><Data />

Response

Request

Page 10: There is time for rest

www.softserve.ua

URIs

• RESTful Interface–No need to distinguish get/update/delete,

that’s what the HTTP Method is for–Collection

http://example.com/2011/Honda/ –Resource

http://example.com/2011/Honda/Civic/2.0

Page 11: There is time for rest

www.softserve.ua

Return Types

• XML– XHTML– ATOM Publishing Protocol– RSS– Custom (should contain hyperlinks)

• JSON• Custom

Page 12: There is time for rest

www.softserve.ua

WCF and REST

WCF 3.5 REST

Starter Kit

WCF 4 REST

WCF Web Api

WCF 4 REST– REST endpoints– Uri templates for routing– WCF Hosting (Console, IIS, etc) etc.

Page 13: There is time for rest

www.softserve.ua

WCF 4 REST, Web Api

• Programming model for HTTP in WCF– Access HTTP from top to bottom– REST endpoints– Uri templates for routing– WCF Hosting (Console, IIS, etc) etc.

• Http Client• Plug in any format / media type• Typeless JSON support• IQueryable support

Page 14: There is time for rest

www.softserve.ua

WCF REST contract

[ServiceContract]public interface IAutomobileService{ [OperationContract] [WebGet(UriTemplate = "*")] Message Get();

[OperationContract] [WebInvoke(Method = "PUT", UriTemplate = "*")] Message Put(Message data);}

Page 15: There is time for rest

www.softserve.ua

Uri Templates

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

}

Page 16: There is time for rest

www.softserve.ua

Routing

static void RegisterRoutes(RouteCollection routes){ routes.MapServiceRoute<SampleResource>("api");}

void Application_Start(object sender,EventArgs e){ RegisterRoutes(RouteTable.Routes);}

Page 17: There is time for rest

www.softserve.ua

Consuming REST .NET

HttpWebRequest

WebClient

WebChannelFactory

HttpClient

Page 18: There is time for rest

www.softserve.ua

WebRequest

var httpRequest = (HttpWebRequest)WebRequest.Create("http://localhost/myservice");

 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.}

Page 19: There is time for rest

www.softserve.ua

WebChannelFactory

[ServiceContract]public interface ITwitterService{ [OperationContract] [WebInvoke(UriTemplate = "/friendships/create/{user}.xml")] TwitterUser CreateFriendship(string user);} using (var factory = new WebChannelFactory<ITwitterService>(

new Uri("http://www.twitter.com/"))){ factory.Credentials.UserName.UserName = username; factory.Credentials.UserName.Password = password; var proxy = factory.CreateChannel(); var user = proxy.CreateFriendship("userToFollow");}

Page 20: There is time for rest

www.softserve.ua

HttpClient

var sample = new Sample();

using (var client = new HttpClient()){ client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json"));

client.Post( "http://localhost/myservice", new ObjectContent(typeof(Sample), sample, Sample.JsonMediaType));}

Page 21: There is time for rest

www.softserve.ua

Tools

• Fiddlerhttp://www.fiddler2.com/fiddler2/

• Firebughttp://getfirebug.com

• IEWatchhttp://www.iewatch.com/

Page 22: There is time for rest

www.softserve.ua

SHOW ME THE CODE

Page 23: There is time for rest

www.softserve.ua

Implementations

• OpenRastaOpenRasta brings the concept of REST to the .NET platform in ways that allow it to be deployed alongside ASP.NET and WCF components. http://trac.caffeine-it.com/openrasta/wiki/Doc

http://code.google.com/p/implementing-rest/wiki/ByLanguage

Page 24: There is time for rest

www.softserve.ua

Books

Page 25: There is time for rest

www.softserve.ua

Resources

WCF Web API

• http://wcf.codeplex.com

WCF Web APis: "There's a URI for That"

• http://channel9.msdn.com/Events/MIX/MIX11/FRM14

RFC HTTP

• http://www.ietf.org/rfc/rfc2616.txt

Implementing REST, small reference and list of frameworks

• http://code.google.com/p/implementing-rest/

REST on Wikipedia

• http://en.wikipedia.org/wiki/REST

Page 26: There is time for rest

www.softserve.ua


Recommended