REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft http://blogs.msdn.com/glengordon Session Objectives Provide you with an overview of REST Illustrate the new webHttpBinding Show you how.

Download Report

Transcript REST and WCF 3.5 Glen Gordon Developer Evangelist, Microsoft http://blogs.msdn.com/glengordon Session Objectives Provide you with an overview of REST Illustrate the new webHttpBinding Show you how.

REST and WCF 3.5
Glen Gordon
Developer Evangelist, Microsoft
http://blogs.msdn.com/glengordon
Session Objectives
Provide you with an overview of REST
Illustrate the new webHttpBinding
Show you how to create and consume REST
services
Agenda
Life before REST
Understanding the Web
SOAP Services
WCF 3.0
REST Overview (including the REST
Continuum)
webHttpBinding Overview
Pragmatic REST Demo
Purist REST Demo
The Web in the real world
Everything (mostly) is URI addressable
HTTP Verbs
GET - Most Prevalent
POST – Overloaded, Used for actions
PUT, DELETE – Largely Ignored
Representation Format – (X)HTML
HTTP Response Codes
Stateless
SOAP Services
(from 50,000 feet)
Typically overload POST
Message body contains method information
Messages wrapped with a SOAP Envelope
WSDL describes SOAP service
WS-* provides extended functionality
Standardized
and
interoperable
POST
/ProductServices.svc
Host: www.somesite.com
Sample:
SOAPAction: GetProduct
…
<soap:Envelope xmlns: …
<soap:Body>
<GetProduct xmlns: …
<ProductId>1</ProductId>
</GetProduct>
</soap:Body>
</soap:Envelope>
WCF 3.0 HTTP Bindings
basicHttpBinding
SOAP Binding
Conforms to WS-I Basic Profile 1.0 standards
wsHttpBinding
SOAP Binding
Provides extended functionality
ws-AtomicTransaction
ws-ReliableMessaging
etc.
No “web-friendly” bindings
Agenda
Life before REST
Understanding the Web
SOAP Services
WCF 3.0
REST Overview (including the REST
Continuum)
webHttpBinding Overview
pragmatic REST Demo
purist REST Demo
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
Foundations of REST
Everything* can be modeled as a resource
Every resource can have one or more
“names”
Every resource can have one or more
representations
Resources are manipulated via the uniform
interface
Leverage HTTP features as recommended
in the specs
Anatomy of an HTTP request
Headers
Verb
URI
GET /ControlPanel/Blogs/postlist.aspx HTTP/1.1
Accept: 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
[optional] body
Anatomy of an HTTP response
Headers
Response code
HTTP/1.1 200 OK
Proxy-Connection: Keep-Alive
Keep-Alive
Content-Type header
Content-Length: 52535
Expires: -1
Date: Fri, 15 Feb 2008 00:52:40 GMT
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/6.0
…
Cache-Control: no-cache
Pragma: no-cache
Connection:
<!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…
Resources
Anything
Static objects (html files, images, etc)
Entities that have a representation
Results of queries & methods execution
…
URIs
URIs
URL is an artifact of the readonly web
http://localhost/MyApplication/images/img.jpg
C:\inetpub\wwwroot\MyApplication\images\img.jpg
The Web does not distinguish between static
& dynamic
URI is a true identifier
http://localhost/MyApplication/proc?id=256
http://localhost/MyApplication/proc/256
http://localhost/MyApplication/proc/lightsabre
URIs partition the application space
Transparent vs opaque
REST Continuum
Purists
RESTfullness
Well Constructed URIs
HTTP Verbs
GET – Fetch
PUT – Update / Insert
DELETE – Delete
POST – Append
Standard Representations
Pragmatists
POST to 1 URI OK
Querystrings OK
HTTP Verbs
GET – Fetch
POST - Overloaded
POX OK
Agenda
Life before REST
Understanding the Web
SOAP Services
WCF 3.0
REST Overview (including the REST
Continuum)
webHttpBinding Overview
pragmatic REST Demo
purist REST Demo
webHttpBinding
New “web-friendly” WCF Binding in Fx 3.5
Allows for the development of RESTful
services
Works across REST Continuum
HTTP and HTTPS Transports Only
Does not use SOAP envelopes
WebMessageEncoding
JSON
XML
Binary
[WebGet] and [WebInvoke]
Indicate the HTTP Method for the operation
WebGet – Don’t make me write it
WebInvoke – All verbs other than GET (Method
parameter takes in the name of the Verb)
Other Parameters
BodyStyle – Indicates whether the Request /
Response are wrapped or not
RequestFormat – Json or Xml
ResponseFormat – Json or Xml
UriTemplate – Covered in a minute…
UriTemplate
String that allows you to define the
structure of the URI, as well as to define
“Holes”
The “Holes” are variables
You Bind the template with parameters to fill
the holes
Hole
[OperationContract]
[WebGet(UriTemplate=“product/{productId}")]
Product GetProduct(int productId);
{productId} hole / variable gets bound to
productId parameter in operation
webHttpBinding Endpoint
Behaviors
Endpoint Behaviors - extend run-time
behavior for an endpoint
webHttp - enables the Web programming
model for a WCF service
enableWebScript – ASP.NET AJAX friendly
endpoint behavior
Aligns nicely with ASP.NET AJAX client
SubClasses webHttp
Provides ASP.NET AJAX proxy generation
Only supports GET and overloaded POST
Does not support UriTemplates
DEMO
Pragmatic (ASP.NET AJAX-Friendly)
REST Services
enableWebScript endpoint behavior
Consuming with an ASP.NET AJAX Client
Purist REST Services
webHttp endpoint behavior
Consuming with an AJAX Client
Summary
REST principles borrow from principles of the
web
Architectures vary with regard to adherence
to REST principles
webHttpBinding supports architectures across
the REST continuum
enableWebScript
Productivity features for ASP.NET AJAX applications
Imposes limitations
webHttp – Provides ability to implement services
that adhere to strictest of standards
Resources
Steve Maine's Blog http://hyperthink.net/blog/
Justin Smith's Blog http://blogs.msdn.com/justinjsmith
HTTP Programming with WCF and the .NET
Framework 3.5
http://msdn.microsoft.com\msdnmag\issues\0
8\01\WCFinOrcas
Glen Gordon
Developer Evangelist, Microsoft
http://blogs.msdn.com/glengordon
© 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.