27
WCF: Building RESTful Services Steve Maine Senior Program Manager Microsoft Corporation

Building+restful+webservice

Embed Size (px)

Citation preview

Page 1: Building+restful+webservice

WCF: Building RESTful Services

Steve MaineSenior Program ManagerMicrosoft Corporation

Page 2: Building+restful+webservice

Source: Pingdom.com

Page 3: Building+restful+webservice

1993:Web == Content

Page 4: Building+restful+webservice

The BrowserGeneric client experience

URI’sAddressing and identification

HTMLCommon presentation format

HyperlinksAnarchic interconnectivity

HTTP GETCommon operation everything supports

Content-Driven Web Architecture

Page 5: Building+restful+webservice

2008:Web == Content + Capabilities

Page 6: Building+restful+webservice

Capability-Enabled Web Architecture

Rich Browser ClientsProgrammability via script or plugins

HTTPBaseline application protocolCommon set of operations + status codes

Domain-neutral data-oriented formatsJSON, Atom/Atom PublishingRefine to support domain-specific schemas

Presentation formatsHTML, CSS

Page 7: Building+restful+webservice

RESTful Tenents

The Web is a graph of linked Resources Resources are identified by URI’s Resources support a fixed set of operations

In practice, these are defined by HTTP Applications follow links

to achieve late binding

REST is an architectural style, not a specification

Page 8: Building+restful+webservice

REST Continuum

Well Constructed URIs

HTTP Verbs GET – Fetch PUT – Update/Insert DELETE – Delete POST – Append

Standard Representations

RESTfullness

POST to 1 URI OKQuerystrings OK

HTTP Verbs GET – Fetch POST – Overloaded

AJAX Services POX OK

Purists Pragmatists

Page 9: Building+restful+webservice

AJAX Services with WCF

demo

Page 10: Building+restful+webservice

webHttpBinding

New “web-friendly” WCF Binding in Fx 3.5Allows for the development of RESTful

servicesDoes not use SOAP envelopesHTTP and HTTPS Transports Only

Supports several wire formats:XMLJSONBinary (streams)

Page 11: Building+restful+webservice

WebServiceHost

Specialized SerivceHost for RESTful servicesEliminates need for lots of configurationAutomatically configures address, binding,

contract Optimized for single-endpoint services Use from .svc file:

<%@ ServiceHost Language="C#" Debug="true" Service="Caching1.FeedService" Factory=“System.ServiceModel.Activation.WebServiceHostFactory” %>"%>

Page 12: Building+restful+webservice

[WebGet] And [WebInvoke]

Binds a WCF operation to URI space and HTTP method

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 – Rich binding to URI

Page 13: Building+restful+webservice

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);

Variable

Page 14: Building+restful+webservice

WCF REST Starter Kit

announcing

Page 15: Building+restful+webservice

WCF REST Starter Kit

Microsoft.ServiceModel.Web.dllNew features supporting RESTful services

Visual Studio 2008 TemplatesREST Collections/Singleton ServicesAtom Feed/Atom Publishing ProtocolHTTP/POX Services

REST Samples Codeplex Project

Released at PDCWritten by WCF teamFeatures may be included in .NET 4.0

Page 16: Building+restful+webservice

The REST 0f WCF

demo

Page 17: Building+restful+webservice

What We've Talked About Today

REST and the “zen” of the web WCF features for REST scenarios

[WebGet] + [WebInvoke]UriTemplateWebHttpBindingAnd many more…

The WCF REST Starter KitAvailable today at

http://msdn.com/wcf/rest

Page 18: Building+restful+webservice

Evals & Recordings

Please fill

out your

evaluation for

this session at:

This session will be available as a recording at:

www.microsoftpdc.com

Page 19: Building+restful+webservice

Please use the microphones provided

Q&A

Page 20: Building+restful+webservice

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

Page 21: Building+restful+webservice
Page 22: Building+restful+webservice

WebGet/WebInvoke Examples

[OperationContract][WebInvoke( Method=“PUT", ResponseFormat=WebMessageFormat.Json, UriTemplate=“product/{productId}")]Product UpdateProduct(int productId, product p);

[OperationContract][WebGet( ResponseFormat=WebMessageFormat.Json, UriTemplate=“product/{productId}")]ProductData GetProduct(int productId);

Page 23: Building+restful+webservice

WebProtocolException

CLR Exception that can be turned into HTTP status code on the wire

Can optionally carry detail information for display in the browser

[OperationContract, WebGet]Stream EchoText(string text){ if( text == null ) throw new WebProtocolException( HttpStatusCode.BadRequest ); . . .}

Page 24: Building+restful+webservice

AdapterStream

Used when you want a Stream or TextWriter but don’t have one handy

[OperationContract, WebGet]Stream EchoText(string text){ return new AdapterStream( textWriter => { textWriter.WriteLine("You said: "); textWriter.WriteLine(text); textWriter.Flush(); }, Encoding.UTF8);}

Page 25: Building+restful+webservice

Request Interceptors

MessageInspectors at the channel layer Think of them like a per-service pipeline Arbitrary message manipulation prior to

dispatch; can prevent dispatch too

public class XHttpMethodOverrideInterceptor:RequestInterceptor {

public override void ProcessRequest(ref RequestContext

requestContext) {

//Change the HTTP method used for dispatch based on

//the X-HTTP-Method-Override header }}

Page 26: Building+restful+webservice

[WebCache]

Integrates WCF operations with ASP.NET caching (requires ASP.NET Compat Mode)

Cache profile stored in Web.config[OperationContract, WebGet][WebCache(CacheProfileName="CacheFor1Min")]public Atom10FeedFormatter GetFeed(int i) { … }

<system.web> <caching> <outputCacheSettings> <outputCacheProfiles>

<add name="CacheFor1Min" duration="60" enabled="true" location="Any" varyByParam=“i"/>

</outputCacheProfiles> </outputCacheSettings> </caching> </system.web>

Page 27: Building+restful+webservice

ASP.NET Authentication Support

“It just works” with ASP.NET Full support for Membership + Roles Requires ASP.NET Compatibility Mode

[OperationContract, WebGet][PrincipalPermission(SecurityAction.Demand, Role=“admin")]SampleResponseBody GetData(int i, string s)