MID307. Article source: /2011/05/02/6569632-facebook- marriage-killer?gt1=43001 .

38
Make Yourself Comfortable and REST with Microsoft .NET Howard Dierking Program Manager, WCF Microsoft MID307

Transcript of MID307. Article source: /2011/05/02/6569632-facebook- marriage-killer?gt1=43001 .

Make Yourself Comfortable and REST with Microsoft .NET

Howard DierkingProgram Manager, WCFMicrosoft

MID307

Agenda

Why REST?REST and WCF 4.0A Look Into the Future

REST is an architectural style grounded in the characteristics that make the Web successful

Broad Interoperability

Proliferation of Devices

Increased Scale Demands

“The Server”

Traditional Web Service

[ServiceContract(Namespace = "http://samples.microsoft.com/wcf/restcart")]public interface ICartService{ [OperationContract(Action = "GetCart")] CartDto GetCart();

[OperationContract(Action = "ClearCart")] void ClearCart();

[OperationContract(Action = "UpdateCartItem")] CartDto UpdateCartItem(int itemId, int qty);

[OperationContract(Action="RemoveCartItem")] CartDto RemoveCartItem(int itemId);

[OperationContract(Action = "AddCartItem")] CartDto AddCartItem(int itemId, int qty);}

Resources and Representations

Howard Dierking

Voice

HTMLEmail

Teleportation

A Simple Product CatalogProductResource

http://myhost/product/

GET

POST

GET (id)

PUT (id)

DELETE (id)

GET http://restcartwcf4/products

POST http://restcartwcf4/products

GET http://restcartwcf4/products/1

PUT http://restcartwcf4/products/1

DELETE http://restcartwcf4/products/1

Setting Up the Product Catalog

Demo

Content Negotiation

Content Negotiation

HTTP/1.1 200 OKCache-Control: privateContent-Length: 553Content-Type: application/json; charset=utf-8

{ "description":"Erik Evans magnum opus on DDD", "links":null,"name":"Domain Driven Design", "price":59.99,"productNumber":"200", "quantityOnHand":1}

ProductResourcehttp://myhost/product/

GET

POST

GET (id)

PUT (id)

DELETE (id)

GET http://myhost/product/1Accept: application/json

HTTP/1.1 200 OKContent-Type: application/json

Automatic Content Negotiation

Demo

Intermediaries and Caching

What did he just say?

Cache Control and Revalidation

GET http://myhost/product/1HTTP/1.1 304 Not Modified

GET http://myhost/product/1If-None-Match: “1”

HTTP/1.1 304 Not Modified

2

3

GET http://myhost/product/1

HTTP/1.1 200 OKCache-Control: max-age=60Etag:“1”...

1ProductResource

http://myhost/product/

GET

POST

GET (id)

PUT (id)

DELETE (id)

GET (id)

Optimistic ConcurrencyPUT http://myhost/product/1If-Match: “1”

HTTP/1.1 200 OKEtag: “2”...

PUT http://myhost/product/1If-Match: “1”

HTTP/1.1 412 Precondition Failed

GET http://myhost/product/1

HTTP/1.1 200 OKEtag: “2”

PUT http://myhost/product/1If-Match: “2”HTTP/1.1 200 OKEtag: “8”...

1

2

3

4

ProductResourcehttp://myhost/product/

GET

POST

GET (id)

PUT (id)

DELETE (id)

GET (id)

PUT (id)

Control Caching and Validation

Demo

Hypermedia

Hypermedia

GET http://myhost/product/Accept: application/atom+xml

GET http://myhost/product/1

HTTP/1.1 200 OK

HTTP/1.1 200 OK

<feed xmlns="http://www.w3.org/2005/Atom"> <title type="text">All Products</title> <subtitle type="text">All my products</subtitle> <id>ProductsFeed</id> <updated>2011-05-10T22:30:30-07:00</updated> <entry> <id>2</id> <title type="text">Domain Driven Design</title> <updated>2011-05-10T22:30:30-07:00</updated> <link rel="self" href="http://myhost/product/1"/> ... </entry></feed>

<link rel="self" href="http://myhost/product/1"/>

1

2

ProductResourcehttp://myhost/product/

GET

POST

GET (id)

PUT (id)

DELETE (id)

GET

GET (id)

Reduce Coupling with Hypermedia

Demo

Future: WCF Web API Improvements

Symmetric HTTP object model on the client and serverSimplified extensibility pipelineIoC and code-based configAsync (task of T on the client)…and more to come…

Extended Content Negotiation with Web API

Demo

Let’s Recap

REST BenefitsBroad interoperabilityDevice integrationNaturally suited for horizontal scale

Implementing REST with WCFResources and representations (WCF 4.0)Content negotiation (WCF 4.0, expanded in Web API)Leverage intermediaries (WCF 4.0)Hypermedia (WCF 4.0, expanded in Web API)

Learn more about WCF and REST

http://wcf.codeplex.com Roy Fielding’s thesisIANA registry of media typesIANA registry of link relationshipsREST in PracticeMSDN WCF Developer Center

http://msdn.microsoft.com/en-US/netframework/aa663324.aspx

MeBlog: http://codebetter.com/howarddierkingEmail: [email protected]: howard_dierking

Related ContentBreakout Sessions

MID302 AppFabric Caching: How It Works and When You Should Use ItMID314 Services Made Easy with WCF 4, Microsoft Visual Studio 2010 and Windows Server AppFabricMID311 Windows Communication Foundation RIA: Ready for Business MID310 Windows Communication Foundation Futures MID315 Building Highly Scalable and Available WCF Services with Windows Azure AppFabric

Hands-on LabsMID374-HOL | Windows Communication Foundation (WCF) 4: Configuration is Much More Simple - Learn How MID378-HOL | Windows Server AppFabric Hosting: Developer Basics - Workflow Services MID377-HOL | Windows Server AppFabric Hosting: Developer Basics - Windows Communication Foundation (WCF) Services

Resources

www.microsoft.com/teched

Sessions On-Demand & Community Microsoft Certification & Training Resources

Resources for IT Professionals Resources for Developers

www.microsoft.com/learning

http://microsoft.com/technet http://microsoft.com/msdn

Learning

http://northamerica.msteched.com

Connect. Share. Discuss.

Complete an evaluation on CommNet and enter to win!

Scan the Tag to evaluate this session now on myTech•Ed Mobile

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

WCF and HTTP: A Brief History

.NET Framework

3.0

• No support for REST – SOAP only

.NET Framework

3.5

• Basic infrastructure for non-SOAP HTTP services

REST Starter Kit Preview 1

• Visual Studio templates for RESTful services

• Request interceptors

REST Starter Kit Preview 2

• HTTP client library

.NET Framework

4.0

• ASPNET routes integration

• Automatic content negotiation

The RESTful Path

Level 3: Hypermedia

Level 2: HTTP

Level 1: URIs

Level 0: Web Services

Richardson’s HTTP maturity model

Traditional Web ServiceCartService.svc

GetCart()

ClearCart()

UpdateCartItem(itemId, Qty)

RemoveCartItem(itemId)

AddCartItem(itemId, Qty)

ApplyShippingInfo(shippingOption, shippingAddress)

ApplyBillingInfo(billingAddress, paymentType, paymentInfo)

POST http://localhost/CartService.svc HTTP/1.1Content-Type: text/xml; charset=utf-8SOAPAction: "GetCart"Content-Length: 154

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <GetCart xmlns="http://samples.microsoft.com/wcf/restcart"/> </s:Body></s:Envelope>

HTTP/1.1 200 OKContent-Length: 984Content-Type: text/xml; charset=utf-8Date: Tue, 26 Apr 2011 01:22:53 GMT

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <GetCartResponse xmlns="http://samples.microsoft.com/wcf/restcart"> <GetCartResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <DateCreated>2011-04-25T18:22:53.5759314-07:00</DateCreated> <Items> <CartItemDto> <Description>Product 1</Description> <ProductNumber>1</ProductNumber> <Quantity>2</Quantity> <Subtotal>5</Subtotal> <UnitPrice>2.5</UnitPrice> </CartItemDto> </Items> <LastUpdated>2011-04-25T18:22:53.5759314-07:00</LastUpdated> ... </s:Body></s:Envelope>

POST http://localhost/CartService.svc

SOAPAction: "GetCart"

200 OK

Content-Type: text/xml

<LastUpdated>2011-04-25T18:22:53.5759314-07:00</LastUpdated>

Traditional Web ServiceCartService.svc

GetCart()

ClearCart()

UpdateCartItem(itemId, Qty)

RemoveCartItem(itemId)

AddCartItem(itemId, Qty)

ApplyShippingInfo(shippingOption, shippingAddress)

ApplyBillingInfo(billingAddress, paymentType, paymentInfo)

POST http://localhost/CartService.svc HTTP/1.1Content-Type: text/xml; charset=utf-8SOAPAction: "GetCart"Content-Length: 154

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <GetCart xmlns="http://samples.microsoft.com/wcf/restcart"/> </s:Body></s:Envelope>

HTTP/1.1 500 Internal Server ErrorContent-Length: 734Content-Type: text/xml; charset=utf-8Date: Wed, 27 Apr 2011 00:15:00 GMT

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <s:Fault> <faultcode>a:InternalServiceFault</faultcode> <faultstring xml:lang="en-US">...</faultstring> </s:Fault> </s:Body></s:Envelope>

500 Internal Server Error

<faultcode>a:InternalServiceFault</faultcode><faultstring xml:lang="en-US">...</faultstring>

Simplify Etag Validation with Web API MessageHandlers

Demo

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