wcf rest api introduction

Post on 01-Nov-2014

1.838 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

Presentation at DDDMelbourne.

TRANSCRIPT

Discover. Master. Influence.

Doing REST with new WCF Web API

Himanshu DesaiSenior Consultant - Readify

Discover. Master. Influence.

Agenda

• Web as Resource Oriented Architecture• Current Industry Trends• Overview of REST–What is REST? – REST Constraints– REST Benefits

• REST using WCF– REST Support in WCF–WCF Web API

3Discover. Master. Influence.

Resource Oriented Architecture

4Discover. Master. Influence.

Current Industry Trends

• A Move to cloud-based computing• A Migration away from SOAP• More browser based Apps• Adoption of REST• Other standards like OAuth,

WebSockets

5Discover. Master. Influence.

What is REST?

• Representational State Transfer• Idea floated by Roy Fielding• Architecture Style (not a protocol or

spec and it is not restricted to HTTP)

6Discover. Master. Influence.

REST Constraints

• Client - server• Stateless• Cacheable• Layered• Uniform interface

7Discover. Master. Influence.

REST Constraints

• Uniform interface• Client - server• Stateless• Cacheable• Layered

8Discover. Master. Influence.

REST Benefits

• Scalability of component interactions• Evolvability• Reach• Intermediary components to reduce

latency, enforce security and encapsulate legacy systems

9Discover. Master. Influence.

RESTful Services-1

• A Traditional RPC based Service

• Moving from Verb to Noun– Users– Bookmarks

Operation Description createUserAccount Creates a new user accountgetUserProfile Retrieves a specific user’s

public profile informationcreateBookmark Creates a new bookmark for

the authenticated user

10Discover. Master. Influence.

RESTful Services-2

• Designing the Uri Template– An invidual user account– An individual bookmark– A user’s collection of private/public

bookmark– Collection of all public bookmarks

• Applying the Uniform HTTP Interface

11Discover. Master. Influence.

RESTful Services-2

12Discover. Master. Influence.

RESTful Services-3

• Designing Resource RepresentationMethod URI Template Equivalent RPC

Operation PUT users/{username} createUserAccountGET users/{username} getUserAccountPUT users/{username} updateUserAccountDELETE users/{username}/

bookmarks/{id}deleteBookmark

GET users/{username}/bookmarks/{id}

getBookmark

GET users/{username}/bookmarks?tag={tag}

getUserBookmarks

13Discover. Master. Influence.

RESTful Services-4

• Designing Resource Representation<User>

< Email>aaron@pluralsight.com</Email><Name>Aaron Skonnard</Name>

</User>

• Supporting Alternate Representation?tag={tag}&format=json{username}?tag={tag}&format=jsonusers/{username}?format=jsonusers/{username}/profile?format=json

• Security Consideration• Providing Resource Metadata• Avoiding RPC Tendencies

14Discover. Master. Influence.

REQUEST

GET /index.html

Host: www.example.comRESPONSEHTTP/1.1 200 OK

Etag: „3f80f-1b6-3e1cb03b”

Content-Type: text/html;

charset=UTF-8l

GET Example

15Discover. Master. Influence.

POST /order HTTP/1.1Host: amazon.netContent-Type: application/xmlContent-Length: 216<order xmlns="http://schemas.amazon.net/order"><drink>latte</drink></order>

201 CreatedLocation: http://amazon.net/order/1234ContentType: application/xml<order xmlns="http://schemas.amazon.net/order">...

l

POST Example

16Discover. Master. Influence.

PUT /payment/1234 HTTP/1.1Host: amazon.netContent-Type: application/xmlContent-Length: 216<payment xmlns="http://schemas.amazon.net/order"><cardNumber>...</cardNumber></order>

200 OKContentType: application/vnd.amazon+xml xmlns="http://schemas.amazon.net/order"><drink>Latte</drink><link href=„payment/1234” rel=„http://relations.amazon.net/payment”/></order>

l

PUT Example

Discover. Master. Influence.

REST Using WCF

20Discover. Master. Influence.

Rest Support Comparison

.NET 3.0 & 4.0 Web API

WebGet/WebInvoke Support Available

Partial Support for Content Negotiation

Full Support

Partial HTTP Support Full Power of HTTP

Limited Reach Maximise Reach

22Discover. Master. Influence.

Message Handler 1

Transport+Encoder

Op Handler2

Resource

Response

Architecture

Dispatcher

Message Handler 2

GetById?

Op Handler 4

Op Handler 3

Requ

est

Op Handler 1

GetById?

23Discover. Master. Influence.

Components

Content Negotiation

Central Configuration (Fluent API)

Http Classes

Operation Handler Factory Error Handler

Message Handler Factory(Channel

Level)

24Discover. Master. Influence.

Classes for Request & Response

• HttpRequestMessage• HttpResponseMessage• HttpContent• HttpClient

25Discover. Master. Influence.

Content Negotiation

• Support for Multiple Formats including an add-in model– if you want xml ,it gives xml and if you

want Json, it gives Json

Discover. Master. Influence.

Demo

• Content Negotiation

27Discover. Master. Influence.

Operation Handler Factory

• Responsible for creating new instances of Media Type Formatters and Operation Handlers.

• Media Type Formatter • Operation Handler - an abstract base

class used to create transfer a set of input into a set of output

28Discover. Master. Influence.

Operation Handler Factory

Request Pipeline Response Pipeline

Operation Handler

Operation Handler

Operation Handler

Operation Handler

Operation Handler

Operation Handler

Dispatch

29Discover. Master. Influence.

Message Handler Factory

Ope

ratio

nMessage Handler 1

Message Handler2

Message Handler3

Message Handler4

30Discover. Master. Influence.

Query Composition

• Support for OData like query support–http://localhost/AlbumService/

albums?$Top=5–http://localhost/AlbumService/

albums?$Top=5&$OrderBy=Title

31Discover. Master. Influence.

Consuming RESTful Services

• Consuming with Javascript• Consuming with WCF

32Discover. Master. Influence.

Consuming Web API with Javascript

33Discover. Master. Influence.

HttpClient

• Helps you access web resources using Linqvar client = new HttpClient(address);

var personQuery = client.CreateQuery<Person>(); var results = personQuery.ExecuteAsync().ContinueWith(p =>

{//Dosomething here

});

Discover. Master. Influence.

Demo

• Query Composition & HttpClient

Discover. Master. Influence.

Questions?

top related