24
REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft http://blogs.msdn.com/ glengordon

REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

Embed Size (px)

Citation preview

Page 1: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

REST and WCF 3.5

Glen GordonDeveloper Evangelist, Microsofthttp://blogs.msdn.com/glengordon

Page 2: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

Session Objectives

Provide you with an overview of RESTIllustrate the new webHttpBinding Show you how to create and consume REST services

Page 3: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

Agenda

Life before RESTUnderstanding the WebSOAP ServicesWCF 3.0

REST Overview (including the REST Continuum)webHttpBinding OverviewPragmatic REST DemoPurist REST Demo

Page 4: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

The Web in the real world

Everything (mostly) is URI addressableHTTP Verbs

GET - Most PrevalentPOST – Overloaded, Used for actionsPUT, DELETE – Largely Ignored

Representation Format – (X)HTMLHTTP Response Codes Stateless

Page 5: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

SOAP Services (from 50,000 feet)

Typically overload POSTMessage body contains method information

Messages wrapped with a SOAP Envelope

WSDL describes SOAP serviceWS-* provides extended functionalityStandardized and interoperable Sample:

POST /ProductServices.svcHost: www.somesite.comSOAPAction: GetProduct…<soap:Envelope xmlns: … <soap:Body> <GetProduct xmlns: … <ProductId>1</ProductId> </GetProduct> </soap:Body></soap:Envelope>

Page 6: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

WCF 3.0 HTTP Bindings

basicHttpBindingSOAP BindingConforms to WS-I Basic Profile 1.0 standards

wsHttpBindingSOAP BindingProvides extended functionality

ws-AtomicTransactionws-ReliableMessagingetc.

No “web-friendly” bindings

Page 7: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

Agenda

Life before RESTUnderstanding the WebSOAP ServicesWCF 3.0

REST Overview (including the REST Continuum)webHttpBinding Overviewpragmatic REST Demopurist REST Demo

Page 8: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

REST

Representational State Transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web

From Wikipedia.org

Representational State Transfer (REST) works on top of HTTP and takes advantage of URLs as a sort of "command line interface". In such an environment, one computer creates a URL defining a request from a second program or computer. Once received the second computer treats the URL as a command, processes it, and returns the results as an XML stream From osuosl.org

Page 9: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

Foundations of REST

Everything* can be modeled as a resourceEvery resource can have one or more “names”Every resource can have one or more representationsResources are manipulated via the uniform interfaceLeverage HTTP features as recommended in the specs

Page 10: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

Anatomy of an HTTP request

GET /ControlPanel/Blogs/postlist.aspx HTTP/1.1Accept: image/gif, … application/x-shockwave-flash, application/x-silverlight, */*

Accept-Language: en-US,zh-CN;…

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; Tablet PC 2.0; InfoPath.2; .NET CLR 3.5.21022; .NET CLR 3.0.04506; MS-RTC LM 8)

Host: blogs.msdn.com

Proxy-Connection: Keep-Alive

Verb URIHeaders

[optional] body

Page 11: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

HTTP/1.1 200 OK

Proxy-Connection: Keep-AliveConnection: Keep-AliveContent-Length: 52535Expires: -1Date: Fri, 15 Feb 2008 00:52:40 GMTContent-Type: text/html; charset=utf-8Server: Microsoft-IIS/6.0…Cache-Control: no-cachePragma: no-cache

Anatomy of an HTTP response

Response code

Content-Type header

Headers

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Community Server Control Panel </title…

Page 12: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

Resources

AnythingStatic objects (html files, images, etc)Entities that have a representationResults of queries & methods execution…

Page 13: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

URIs

Page 14: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

URIs

URL is an artifact of the readonly webhttp://localhost/MyApplication/images/img.jpg C:\inetpub\wwwroot\MyApplication\images\img.jpg

The Web does not distinguish between static & dynamic

URI is a true identifierhttp://localhost/MyApplication/proc?id=256http://localhost/MyApplication/proc/256http://localhost/MyApplication/proc/lightsabre

URIs partition the application spaceTransparent vs opaque

Page 15: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

REST Continuum

Well Constructed URIs

HTTP VerbsGET – FetchPUT – Update / InsertDELETE – DeletePOST – Append

Standard Representations

RESTfullness

Hi-

REST

Lo-

REST

POST to 1 URI OKQuerystrings OKHTTP Verbs

GET – FetchPOST - Overloaded

POX OK

Purists Pragmatists

Page 16: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

Agenda

Life before RESTUnderstanding the WebSOAP ServicesWCF 3.0

REST Overview (including the REST Continuum)webHttpBinding Overviewpragmatic REST Demopurist REST Demo

Page 17: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

webHttpBinding

New “web-friendly” WCF Binding in Fx 3.5Allows for the development of RESTful servicesWorks across REST ContinuumHTTP and HTTPS Transports OnlyDoes not use SOAP envelopesWebMessageEncoding

JSONXMLBinary

Page 18: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

[WebGet] and [WebInvoke]

Indicate the HTTP Method for the operationWebGet – Don’t make me write itWebInvoke – All verbs other than GET (Method parameter takes in the name of the Verb)

Other ParametersBodyStyle – Indicates whether the Request / Response are wrapped or notRequestFormat – Json or XmlResponseFormat – Json or XmlUriTemplate – Covered in a minute…

Page 19: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

UriTemplate

String that allows you to define the structure of the URI, as well as to define “Holes”

The “Holes” are variablesYou Bind the template with parameters to fill the holes

{productId} hole / variable gets bound to productId parameter in operation

[OperationContract][WebGet(UriTemplate=“product/{productId}")]Product GetProduct(int productId);

Hole

Page 20: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

webHttpBinding Endpoint Behaviors

Endpoint Behaviors - extend run-time behavior for an endpoint

webHttp - enables the Web programming model for a WCF serviceenableWebScript – ASP.NET AJAX friendly endpoint behavior

Aligns nicely with ASP.NET AJAX clientSubClasses webHttpProvides ASP.NET AJAX proxy generationOnly supports GET and overloaded POSTDoes not support UriTemplates

Page 21: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

DEMO

Pragmatic (ASP.NET AJAX-Friendly) REST Services

enableWebScript endpoint behaviorConsuming with an ASP.NET AJAX Client

Purist REST ServiceswebHttp endpoint behaviorConsuming with an AJAX Client

Page 22: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

Summary

REST principles borrow from principles of the webArchitectures vary with regard to adherence to REST principleswebHttpBinding supports architectures across the REST continuum

enableWebScript Productivity features for ASP.NET AJAX applicationsImposes limitations

webHttp – Provides ability to implement services that adhere to strictest of standards

Page 23: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

Resources

Steve Maine's Blog - http://hyperthink.net/blog/Justin Smith's Blog - http://blogs.msdn.com/justinjsmithHTTP Programming with WCF and the .NET Framework 3.5http://msdn.microsoft.com\msdnmag\issues\08\01\WCFinOrcas

Page 24: REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft

© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.

The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee

the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Glen GordonDeveloper Evangelist, Microsofthttp://blogs.msdn.com/glengordon