![Page 1: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/1.jpg)
REST, JSON and RSS with Windows Communication Foundation 3.5
Rob WindsorRob WindsorObjectSharp ConsultingObjectSharp [email protected]@objectsharp.com
![Page 2: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/2.jpg)
Me.AboutMe.AboutVisual Basic MVPVisual Basic MVPSenior Consultant with ObjectSharp Senior Consultant with ObjectSharp ConsultingConsultingPresident of the Toronto Visual Basic User President of the Toronto Visual Basic User GroupGroupMember of the MSDN Canada Speakers Member of the MSDN Canada Speakers BureauBureauContact me via my blogContact me via my blog
http://msmvps.com/windsorhttp://msmvps.com/windsor
![Page 3: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/3.jpg)
WCF OverviewWCF OverviewJSON ServicesJSON ServicesHTTP Programming ModelHTTP Programming ModelSyndicationSyndication
AgendaAgenda
![Page 4: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/4.jpg)
One-stop-shop for servicesOne-stop-shop for servicesConsistent object modelConsistent object modelFirst released with .NET Framework 3.0First released with .NET Framework 3.0
Focus on the functionality, WCF takes Focus on the functionality, WCF takes care of the plumbingcare of the plumbing
Windows Communication Windows Communication FoundationFoundation
![Page 5: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/5.jpg)
The ABCs of WCFThe ABCs of WCF
ClientClient ServiceService
MessageMessageAABBCC AA BB CC
AA BB CC
AddressAddress BindingBinding ContractContract(Where)(Where) (How)(How) (What)(What)
![Page 6: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/6.jpg)
WCF Standard BindingsWCF Standard BindingsNameName TransporTranspor
ttEncodingEncoding InteroIntero
ppBasicHttpBindingBasicHttpBinding HTTP/HTTPSHTTP/HTTPS TextText YesYesNetTcpBindingNetTcpBinding TCPTCP BinaryBinary NoNoNetPeerTcpBindingNetPeerTcpBinding P2PP2P BinaryBinary NoNoNetNamedPipeBindingNetNamedPipeBinding IPCIPC BinaryBinary NoNoWSHttpBindingWSHttpBinding HTTP/HTTPSHTTP/HTTPS Text, MTOMText, MTOM YesYesWSFederationBindingWSFederationBinding HTTP/HTTPSHTTP/HTTPS Text, MTOMText, MTOM YesYesWSDualHttpBindingWSDualHttpBinding HTTP/HTTPSHTTP/HTTPS Text, MTOMText, MTOM YesYesNetMsmqBindingNetMsmqBinding MSMQMSMQ BinaryBinary NoNoNetIntegrationBindingNetIntegrationBinding MSMQMSMQ BinaryBinary YesYes
![Page 7: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/7.jpg)
WCF Services
![Page 8: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/8.jpg)
WCF OverviewWCF OverviewJSON ServicesJSON ServicesHTTP Programming ModelHTTP Programming ModelSyndicationSyndication
AgendaAgenda
![Page 9: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/9.jpg)
JJavaavaSScript cript OObject bject NNotationotationFormat for bridging JavaScript and objectsFormat for bridging JavaScript and objects
Easier for browsers than XMLEasier for browsers than XMLASP.NET AJAX & other AJAX toolkits use itASP.NET AJAX & other AJAX toolkits use it
Other web-aware clients also (Silverlight, etc.)Other web-aware clients also (Silverlight, etc.)
var data = {“temp” : 59, “descr” : “cloudy”};document.write (“The weather is “ + data.descr);
What is JSON?What is JSON?
![Page 10: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/10.jpg)
WCF AJAX support in Visual Studio WCF AJAX support in Visual Studio Script manager, VS Project TemplatesScript manager, VS Project Templates
WCF automatically generates JS proxyWCF automatically generates JS proxyUsage pattern similar to existing one:Usage pattern similar to existing one:
Add service to Script Manager controlAdd service to Script Manager controlWrite JavaScript code to work with proxyWrite JavaScript code to work with proxy
Configuration not requiredConfiguration not requiredVia the WebScriptServiceHostFactory (.svc file)Via the WebScriptServiceHostFactory (.svc file)
Works in ASP.NET Medium Trust!Works in ASP.NET Medium Trust!
WCF / AJAX IntegrationWCF / AJAX Integration
![Page 11: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/11.jpg)
JSON Services
![Page 12: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/12.jpg)
WCF OverviewWCF OverviewJSON ServicesJSON ServicesHTTP Programming ModelHTTP Programming ModelSyndicationSyndication
AgendaAgenda
![Page 13: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/13.jpg)
Embrace the URIEmbrace the URISegments map to application logicSegments map to application logic
HTTP GET is specialHTTP GET is specialGET is idempotent (View It)GET is idempotent (View It)
Multiple GETs to a URI should produce the same (or Multiple GETs to a URI should produce the same (or similar) resultssimilar) results
PUT / POST / DELETE do “stuff” (Do It)PUT / POST / DELETE do “stuff” (Do It)Content-type header is the data modelContent-type header is the data model
Image, XML, JSON, etc.Image, XML, JSON, etc.
Web Concepts (REST)Web Concepts (REST)
![Page 14: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/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 AppsThe Web, the URI, and Apps
![Page 15: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/15.jpg)
System.UriTemplateSystem.UriTemplateType for modeling URI to application semanticsType for modeling URI to application semanticsCan “bind” data to a template, output a URICan “bind” data to a template, output a URICan “match” a URI to a template, retrieve dataCan “match” a URI to a template, retrieve data
System.UriTemplateMatchSystem.UriTemplateMatchReturned from UriTemplate “match” operationsReturned from UriTemplate “match” operationsCan get relative paths and wildcard segmentsCan get relative paths and wildcard segments
System.UriTemplateTableSystem.UriTemplateTableFor “binding” a URI to a group of UriTemplatesFor “binding” a URI to a group of UriTemplates
Modeling a URI in .NET 3.5Modeling a URI in .NET 3.5
![Page 16: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/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 URIRoundtrip Data in a URI
![Page 17: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/17.jpg)
Simple URI-to-application mappingSimple 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 ContractsURIs in WCF Contracts
![Page 18: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/18.jpg)
All HTTP verbs are first class citizensAll HTTP verbs are first class citizensGET, POST, PUT, etc.GET, POST, PUT, etc.
““View It” vs “Do It” separation mimics webView 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 ContractsHTTP Verbs in WCF Contracts
![Page 19: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/19.jpg)
HTTP headers can indicate HTTP headers can indicate Accepted data formats (Request)Accepted data formats (Request)The format of the returned data (Response)The format of the returned data (Response)
Common header names:Common header names:Accept (Request), Content-Type (Response)Accept (Request), Content-Type (Response)
Small sampling of varieties:Small sampling of varieties:text/html, text/css,text/html, text/css,image/gif, image/jpeg, image/gif, image/jpeg, application/atom+xml, application/json, application/atom+xml, application/json, video/mp4video/mp4
Data Formats and the WebData Formats and the Web
![Page 20: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/20.jpg)
WebOperationContext.Current provides WebOperationContext.Current provides access to incoming request headersaccess to incoming request headersCan also set outgoing response headersCan also set outgoing response headers
Some are shortcut for easier useSome 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 WCFSpecifying Data Format in WCF
![Page 21: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/21.jpg)
WebHttpBinding endpoint on a ServiceHostWebHttpBinding endpoint on a ServiceHostAdd WebHttpBehavior to the endpointAdd WebHttpBehavior to the endpoint
UseUse WebServiceHost/Factory in most casesWebServiceHost/Factory in most casesWeb endpoints do not support WSDLWeb endpoints do not support WSDL
Works in ASP.NET Medium Trust!Works in ASP.NET Medium Trust!
Hosting / BindingHosting / Binding
![Page 22: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/22.jpg)
View It and Do It
![Page 23: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/23.jpg)
Level-setLevel-setJSON ServicesJSON ServicesHTTP Programming ModelHTTP Programming ModelSyndicationSyndication
AgendaAgenda
![Page 24: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/24.jpg)
Syndications are more than news and blogsSyndications are more than news and blogsRepresentation of any set of dataRepresentation of any set of dataUsually slowly changingUsually slowly changing
Unified object model for RSS and AtomUnified object model for RSS and AtomSyndicationFeed / SyndicationItemSyndicationFeed / SyndicationItem
Feeds are service operationsFeeds are service operationsConsume as a service or as documentConsume as a service or as document
Syndication Goals in .NET 3.5Syndication Goals in .NET 3.5
![Page 25: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/25.jpg)
Single stop for syndicationsSingle stop for syndicationsCreate and Consume with or without WCFCreate and Consume with or without WCF
Easy to use object modelEasy to use object modelTransport AgnosticTransport AgnosticSupports syndication extensions Supports syndication extensions Format AgnosticFormat Agnostic
RSS 2.0 & ATOM 1.0, others possibleRSS 2.0 & ATOM 1.0, others possible
Works in ASP.NET Medium Trust!Works in ASP.NET Medium Trust!
Syndication in .NET Fx 3.5Syndication in .NET Fx 3.5
![Page 26: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/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 WCFSyndication Contracts in WCF
![Page 27: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/27.jpg)
Syndication with PictureServices
![Page 28: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/28.jpg)
Simple HTTP service developmentSimple HTTP service developmentSOAP and POX from the same contractSOAP and POX from the same contractJSON messaging capabilityJSON messaging capabilitySimple syndication – really!Simple syndication – really!
Built on WCF Built on WCF extensibility extensibility
points from .NET points from .NET 3.03.0
Web Centric Features in WCF Web Centric Features in WCF 3.53.5
![Page 29: REST, JSON and RSS with Windows Communication Foundation 3.5](https://reader030.vdocuments.net/reader030/viewer/2022020322/568149fe550346895db72f9c/html5/thumbnails/29.jpg)
ResourcesResourcesMicrosoft WCF Community SiteMicrosoft WCF Community Site
http://wcf.netfx3.com/http://wcf.netfx3.com/PictureServices SamplesPictureServices Samples
http://www.cloudsamples.net/pictureservices/http://www.cloudsamples.net/pictureservices/The EndPoint on Channel 9The EndPoint on Channel 9
http://channel9.msdn.com/shows/The_EndPointhttp://channel9.msdn.com/shows/The_EndPointJustin Smith’s BlogJustin Smith’s Blog
http://blogs.msdn.com/justinjsmith/http://blogs.msdn.com/justinjsmith/Steve Maine’s BlogSteve Maine’s Blog
http://hyperthink.net/blog/http://hyperthink.net/blog/Getting Started with WCFGetting Started with WCF
http://msdn2.microsoft.com/en-us/vbasic/bb736015.aspxhttp://msdn2.microsoft.com/en-us/vbasic/bb736015.aspx