REST, JSON and RSS with WCF 3.5
-
Upload
rob-windsor -
Category
Technology
-
view
12.286 -
download
9
description
Transcript of REST, JSON and RSS with WCF 3.5
![Page 1: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/1.jpg)
REST, JSON and RSS with Windows Communication Foundation 3.5
Rob WindsorObjectSharp [email protected]
![Page 2: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/2.jpg)
Me.About
Visual Basic MVP
Senior Consultant with ObjectSharp Consulting
President of the Toronto Visual Basic User Group
Member of the MSDN Canada Speakers Bureau
Contact me via my blog
http://msmvps.com/windsor
![Page 3: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/3.jpg)
WCF Overview
JSON Services
HTTP Programming Model
Syndication
Agenda
![Page 4: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/4.jpg)
One-stop-shop for services
Consistent object model
First released with .NET Framework 3.0
Focus on the functionality, WCF takes care of the plumbing
Windows Communication Foundation
![Page 5: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/5.jpg)
The ABCs of WCF
Client Service
MessageABC A B C
A B C
Address Binding Contract
(Where) (How) (What)
![Page 6: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/6.jpg)
WCF Standard Bindings
Name Transport Encoding Interop
BasicHttpBinding HTTP/HTTPS Text Yes
NetTcpBinding TCP Binary No
NetPeerTcpBinding P2P Binary No
NetNamedPipeBinding IPC Binary No
WSHttpBinding HTTP/HTTPS Text, MTOM Yes
WSFederationBinding HTTP/HTTPS Text, MTOM Yes
WSDualHttpBinding HTTP/HTTPS Text, MTOM Yes
NetMsmqBinding MSMQ Binary No
NetIntegrationBinding MSMQ Binary Yes
![Page 7: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/7.jpg)
WCF Services
![Page 8: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/8.jpg)
WCF Overview
JSON Services
HTTP Programming Model
Syndication
Agenda
![Page 9: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/9.jpg)
JavaScript Object Notation
Format for bridging JavaScript and objects
Easier for browsers than XML
ASP.NET AJAX & other AJAX toolkits use it
Other web-aware clients also (Silverlight, etc.)
var data = {“temp” : 59, “descr” : “cloudy”};
document.write (“The weather is “ + data.descr);
What is JSON?
![Page 10: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/10.jpg)
WCF AJAX support in Visual Studio
Script manager, VS Project Templates
WCF automatically generates JS proxy
Usage pattern similar to existing one:
Add service to Script Manager control
Write JavaScript code to work with proxy
Configuration not required
Via the WebScriptServiceHostFactory (.svc file)
Works in ASP.NET Medium Trust!
WCF / AJAX Integration
![Page 11: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/11.jpg)
JSON Services
![Page 12: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/12.jpg)
WCF Overview
JSON Services
HTTP Programming Model
Syndication
Agenda
![Page 13: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/13.jpg)
Embrace the URI
Segments map to application logic
HTTP GET is special
GET is idempotent (View It)
Multiple GETs to a URI should produce the same (or similar) results
PUT / POST / DELETE do “stuff” (Do It)
Content-type header is the data model
Image, XML, JSON, etc.
Web Concepts (REST)
![Page 14: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/14.jpg)
objectsharp.com/artists/Flaming+Hammer?album=HitMeobjectsharp.com/artists/Northwind?album=Overdone
objectsharp.com/astists/{artist}?album={album}
objectsharp.com/artists/Flaming+Hammer/HitMeobjectsharp.com/artists/Northwind/Overdone
objectsharp.com/artists/{artist}/{album}
The Web, the URI, and Apps
![Page 15: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/15.jpg)
System.UriTemplate
Type for modeling URI to application semantics
Can “bind” data to a template, output a URI
Can “match” a URI to a template, retrieve data
System.UriTemplateMatch
Returned from UriTemplate “match” operations
Can get relative paths and wildcard segments
System.UriTemplateTable
For “binding” a URI to a group of UriTemplates
Modeling a URI in .NET 3.5
![Page 16: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/16.jpg)
Uri address = new Uri(“http://localhost:2000”);
UriTemplate template =
new UriTemplate(“{artist}/{album}”);
Uri boundUri =
template.BindByPosition(address,
“Northwind”, “Overdone”);
UriTemplateMatch match = template.Match(address,
boundUri);
String bandName = match.BoundVariables[“artist”];
Roundtrip Data in a URI
![Page 17: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/17.jpg)
Simple URI-to-application mapping
[OperationContract]
[WebGet(UriTemplate=“/Image/{artist}/{album}”)]
Stream GetAlbumImage(String artist, String album);
[OperationContract]
[WebGet(UriTemplate=“/Image?name={artist})]
Stream GetMainImage(String artist);
URIs in WCF Contracts
![Page 18: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/18.jpg)
All HTTP verbs are first class citizens
GET, POST, PUT, etc.
“View It” vs “Do It” separation mimics web
[OperationContract]
[WebGet(UriTemplate=“/Image/{bandName}/{album}”)]
Stream GetAlbumImage(String bandName, String album);
[OperationContract]
[WebInvoke(METHOD=“PUT”)] // {PUT, POST, DELETE}
void AddAlbum(AlbumInfo albumInfo);
HTTP Verbs in WCF Contracts
![Page 19: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/19.jpg)
HTTP headers can indicate
Accepted data formats (Request)
The format of the returned data (Response)
Common header names:
Accept (Request), Content-Type (Response)
Small sampling of varieties:
text/html, text/css,
image/gif, image/jpeg,
application/atom+xml, application/json,
video/mp4
Data Formats and the Web
![Page 20: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/20.jpg)
WebOperationContext.Current provides access to incoming request headers
Can also set outgoing response headers
Some are shortcut for easier use
Stream GetAlbumImage(String bandName, String album){
Stream stream; // get the image from somewhere
WebOperationContext.Current.OutgoingResponse.ContentType =
“image/jpeg”;
return stream;
}
Specifying Data Format in WCF
![Page 21: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/21.jpg)
WebHttpBinding endpoint on a ServiceHost
Add WebHttpBehavior to the endpoint
Use WebServiceHost/Factory in most cases
Web endpoints do not support WSDL
Works in ASP.NET Medium Trust!
Hosting / Binding
![Page 22: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/22.jpg)
View It and Do It
![Page 23: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/23.jpg)
Level-set
JSON Services
HTTP Programming Model
Syndication
Agenda
![Page 24: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/24.jpg)
Syndications are more than news and blogs
Representation of any set of data
Usually slowly changing
Unified object model for RSS and Atom
SyndicationFeed / SyndicationItem
Feeds are service operations
Consume as a service or as document
Syndication Goals in .NET 3.5
![Page 25: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/25.jpg)
Single stop for syndications
Create and Consume with or without WCF
Easy to use object model
Transport Agnostic
Supports syndication extensions
Format Agnostic
RSS 2.0 & ATOM 1.0, others possible
Works in ASP.NET Medium Trust!
Syndication in .NET Fx 3.5
![Page 26: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/26.jpg)
[ServiceKnownType(typeof(Atom10FeedFormatter))]
[ServiceKnownType(typeof(Rss20FeedFormatter))]
[ServiceContract]
interface IAlbumSyndication {
[OperationContract]
[WebGet(UriTemplate=“Images/{format}")]
SyndicationFeedFormatter<SyndicationFeed>
Feed(String format);
}
Syndication Contracts in WCF
![Page 27: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/27.jpg)
Syndication with PictureServices
![Page 28: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/28.jpg)
Simple HTTP service development
SOAP and POX from the same contract
JSON messaging capability
Simple syndication – really!
Built on WCF
extensibility points
from .NET 3.0
Web Centric Features in WCF 3.5
![Page 29: REST, JSON and RSS with WCF 3.5](https://reader034.fdocuments.net/reader034/viewer/2022042518/54c551ee4a79599d0d8b457f/html5/thumbnails/29.jpg)
Resources
Microsoft WCF Community Site
http://wcf.netfx3.com/
PictureServices Samples
http://www.cloudsamples.net/pictureservices/
The EndPoint on Channel 9
http://channel9.msdn.com/shows/The_EndPoint
Justin Smith’s Blog
http://blogs.msdn.com/justinjsmith/
Steve Maine’s Blog
http://hyperthink.net/blog/
Getting Started with WCF
http://msdn2.microsoft.com/en-us/vbasic/bb736015.aspx