wcf rest api introduction
DESCRIPTION
Presentation at DDDMelbourne.TRANSCRIPT
![Page 1: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/1.jpg)
Discover. Master. Influence.
Doing REST with new WCF Web API
Himanshu DesaiSenior Consultant - Readify
![Page 2: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/3.jpg)
3Discover. Master. Influence.
Resource Oriented Architecture
![Page 4: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/6.jpg)
6Discover. Master. Influence.
REST Constraints
• Client - server• Stateless• Cacheable• Layered• Uniform interface
![Page 7: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/7.jpg)
7Discover. Master. Influence.
REST Constraints
• Uniform interface• Client - server• Stateless• Cacheable• Layered
![Page 8: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/11.jpg)
11Discover. Master. Influence.
RESTful Services-2
![Page 12: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/17.jpg)
Discover. Master. Influence.
REST Using WCF
![Page 18: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/21.jpg)
24Discover. Master. Influence.
Classes for Request & Response
• HttpRequestMessage• HttpResponseMessage• HttpContent• HttpClient
![Page 22: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/23.jpg)
Discover. Master. Influence.
Demo
• Content Negotiation
![Page 24: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/26.jpg)
29Discover. Master. Influence.
Message Handler Factory
Ope
ratio
nMessage Handler 1
Message Handler2
Message Handler3
Message Handler4
![Page 27: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/28.jpg)
31Discover. Master. Influence.
Consuming RESTful Services
• Consuming with Javascript• Consuming with WCF
![Page 29: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/29.jpg)
32Discover. Master. Influence.
Consuming Web API with Javascript
![Page 30: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/30.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/31.jpg)
Discover. Master. Influence.
Demo
• Query Composition & HttpClient
![Page 32: Wcf rest api introduction](https://reader033.vdocuments.net/reader033/viewer/2022061113/54583ccfb1af9f37608b5259/html5/thumbnails/32.jpg)
Discover. Master. Influence.
Questions?