Upload
softserve
View
1.143
Download
1
Embed Size (px)
DESCRIPTION
Презентація підготовлена та проводиться Романом Калитою (Львів)
Citation preview
www.softserve.ua
There is time for REST
WCF REST Services
www.softserve.ua
Overview
• What is REST• Rules and Guidelines• Hosting a REST Service in .NET• Consuming a REST Service with .NET• Samples• Discussion
www.softserve.ua
What is REST?
• Roy Fieldinghttp://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
• Client-Server• Stateless• Cachable• Layered System
www.softserve.ua
REST Constraints
• Separation resource from representation• Uniform interface• Self-descriptive messages• Hypermedia as engine of application state
www.softserve.ua
HTTP Methods
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
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
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
www.softserve.ua
URIs
• RPC (SOAP) Interface
http://example.com/CarsService/getYears
http://example.com/CarsService/getMakes
http://example.com/CarsService/getAutomobile
http://example.com/CarsService/updateAutomobile
http://example.com/CarsService/deleteAutomobile
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
www.softserve.ua
Return Types
• XML– XHTML– ATOM Publishing Protocol– RSS– Custom (should contain hyperlinks)
• JSON• Custom
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.
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
www.softserve.ua
WCF REST contract
[ServiceContract]public interface IAutomobileService{ [OperationContract] [WebGet(UriTemplate = "*")] Message Get();
[OperationContract] [WebInvoke(Method = "PUT", UriTemplate = "*")] Message Put(Message data);}
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);
}
www.softserve.ua
Routing
static void RegisterRoutes(RouteCollection routes){ routes.MapServiceRoute<SampleResource>("api");}
void Application_Start(object sender,EventArgs e){ RegisterRoutes(RouteTable.Routes);}
www.softserve.ua
Consuming REST .NET
HttpWebRequest
WebClient
WebChannelFactory
HttpClient
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.}
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");}
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));}
www.softserve.ua
Tools
• Fiddlerhttp://www.fiddler2.com/fiddler2/
• Firebughttp://getfirebug.com
• IEWatchhttp://www.iewatch.com/
www.softserve.ua
SHOW ME THE CODE
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
www.softserve.ua
Books
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
www.softserve.ua