Top Banner
Doing REST with new WCF Web API Himanshu Desai Senior Consultant - Readify Discover. Master. Influence.
32

Wcf rest api introduction

Nov 01, 2014

Download

Technology

Himanshu Desai

Presentation at DDDMelbourne.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Wcf rest api introduction

Discover. Master. Influence.

Doing REST with new WCF Web API

Himanshu DesaiSenior Consultant - Readify

Page 2: Wcf rest api introduction

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

Page 3: Wcf rest api introduction

3Discover. Master. Influence.

Resource Oriented Architecture

Page 4: Wcf rest api introduction

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

Page 5: Wcf rest api introduction

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)

Page 6: Wcf rest api introduction

6Discover. Master. Influence.

REST Constraints

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

Page 7: Wcf rest api introduction

7Discover. Master. Influence.

REST Constraints

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

Page 8: Wcf rest api introduction

8Discover. Master. Influence.

REST Benefits

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

latency, enforce security and encapsulate legacy systems

Page 9: Wcf rest api introduction

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

Page 10: Wcf rest api introduction

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

Page 11: Wcf rest api introduction

11Discover. Master. Influence.

RESTful Services-2

Page 12: Wcf rest api introduction

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

Page 13: Wcf rest api introduction

13Discover. Master. Influence.

RESTful Services-4

• Designing Resource Representation<User>

< Email>[email protected]</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

Page 14: Wcf rest api introduction

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

Page 15: Wcf rest api introduction

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

Page 16: Wcf rest api introduction

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

Page 17: Wcf rest api introduction

Discover. Master. Influence.

REST Using WCF

Page 18: Wcf rest api introduction

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

Page 19: Wcf rest api introduction

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?

Page 20: Wcf rest api introduction

23Discover. Master. Influence.

Components

Content Negotiation

Central Configuration (Fluent API)

Http Classes

Operation Handler Factory Error Handler

Message Handler Factory(Channel

Level)

Page 21: Wcf rest api introduction

24Discover. Master. Influence.

Classes for Request & Response

• HttpRequestMessage• HttpResponseMessage• HttpContent• HttpClient

Page 22: Wcf rest api introduction

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

Page 23: Wcf rest api introduction

Discover. Master. Influence.

Demo

• Content Negotiation

Page 24: Wcf rest api introduction

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

Page 25: Wcf rest api introduction

28Discover. Master. Influence.

Operation Handler Factory

Request Pipeline Response Pipeline

Operation Handler

Operation Handler

Operation Handler

Operation Handler

Operation Handler

Operation Handler

Dispatch

Page 26: Wcf rest api introduction

29Discover. Master. Influence.

Message Handler Factory

Ope

ratio

nMessage Handler 1

Message Handler2

Message Handler3

Message Handler4

Page 27: Wcf rest api introduction

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

Page 28: Wcf rest api introduction

31Discover. Master. Influence.

Consuming RESTful Services

• Consuming with Javascript• Consuming with WCF

Page 29: Wcf rest api introduction

32Discover. Master. Influence.

Consuming Web API with Javascript

Page 30: Wcf rest api introduction

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

});

Page 31: Wcf rest api introduction

Discover. Master. Influence.

Demo

• Query Composition & HttpClient

Page 32: Wcf rest api introduction

Discover. Master. Influence.

Questions?