Building RESTful Services using WCF

Download Report

Transcript Building RESTful Services using WCF

Jon Flanders
http://www.rest-ful.net/
INT303
About Me



Jon Flanders –
http://www.rest-ful.net/
Independent
consultant/trainer
BizTalk MVP
Storytime: Once upon time…
3
REST
Representational State Transfer
introduced by Roy Fielding
Architectural style
A set of constraints distilled from the
architecture of the Web
URIs represent resources
Clients interact via with resources via a uniform
interface (GET, POST, PUT, DELETE)
Services are stateless
4
Why should you care?
Microsoft Live Services
.NET Services
More to come from Microsoft and
others – but why let them have all the
fun (and rewards)?
ADO.NET Data Services
5
EVERYBODY that isn’t
Microsoft (Twitter,Google,
Yahoo, Facebook,
MySpace)
REST Advantages
GET responses can be cached
Using URIs build on experience using the Web
Uniform interface simplifies building and using
services
Statelessness constraint eases scalability
GET is safe, can be called N times without
causing change
PUT and DELETE are idempotent (same effect
no matter how many times called)
6
Building a RESTful service
Design your resource(s)
Determine the URI for each resource
Determine what part of the uniform interface
each resource should implement
7
Uniform Interface
GET
POST
8
• Retrieves a resource
• Guaranteed not to cause side-effects (SAFE)
• Results are cacheable
• Creates a new resource
• Unsafe: effect of this verb isn’t defined by HTTP
PUT
• Updates an existing resource
• Used for resource creation when client knows URI
• Can call N times, same thing will always happen (idempotent)
DELETE
• Removes a resource
• Can call N times, same thing will always happen (idempotent)
Windows Communication Foundation
(WCF)
Framework for building applications that
communicate
Client-Server
Message oriented
Service oriented
WCF abstracts away network complexities,
developer concentrates on code
Most of WCF is highly geared toward SOAP
REST support added in 3.5 and improved in 3.5
sp1
9
WCF Messages to Methods
In WCF developers write methods in classes that
respond to network messages
SOAP support in 3.0/3.5 routes messages to
methods based on SOAP Action
URI always the same
Always uses POST
Done by the default dispatcher
10
WCF 3.5 Web Programming
Messages are routed to methods based on:
URI
HTTP Verb
Same programming model as “SOAP”
ServiceContract, OperationContract, DataContract
New dispatcher that maps URIs/Verbs to
methods
11
WCF 3.5 REST infrastructure
UriTemplate & UriTemplateTable
URI parsing
WebGet & WebInvoke attributes
UriTemplate defines URI
WebGet + UriTemplate maps GET requests to
methods
WebInvoke + UriTemplate maps remainder of
uniform interface to methods , Method property
determines verb (POST is default)
WebHttpBinding/WebServiceHost/WebServiceH
ostFactory
Eases use and configuration
12
WCF Dispatching
Network
Message
Transport Channel
Transport Channel
(HTTP)
Message Encoder
Message Encoder
(Text + No SOAP)
Protocol Channel (1-N)
Dispatcher
(URI
+ VERB)
(Action)
Your Code
AJAX Support
Many RESTful clients are JavaScript based AJAX
clients
JavaScript Object Notation (JSON) is the
preferred resource representation
Smaller footprint than XML
No parsing necessary
WCF has support for AJAX applications at two
levels
JSONDataContractSerializer for JSON parsing and
generation
JavaScript “proxy” build on ASP.NET AJAX
14
Web Feeds
Web Feeds are endpoints that expose machine
consumable resource representations
Commonly used for web logs (blogs), news etc.
Expansion into enterprise data is happening
15
WCF support for Feeds
Built on top of WCF REST support
(WebGet/UriTemplate)
Feed specific feature is feed-format neutral
object model
Transform your data into the WCF OM
OM serialized to either RSS or Atom (or other
future formats – extensible formatting capabilities)
OM is closer to Atom model (Atom is richer than
RSS)
16
Know the rules, bend when
necessary
“…REST isn’t an all or nothing proposition. One
can get significant value from partial adoption. “
Sam Ruby - Author RESTFul Web Services
Don’t underestimate
the power of GET!
17
Summary
REST is an architectural style that encourages
interoperable, scalable web services
Builds on the existing architecture of the web
WCF 3.5 adds support for this architecture
18
Complete an evaluation
on CommNet and enter to
win an Xbox 360 Elite!
© 2009 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.