REST, JSON and RSS with WCF 3.5


Published on

WCF is not just for SOAP based services and can be used with popular protocols like RSS, REST and JSON. Rob Windsor covers URI templates, the importance of HTTP GET in the programmable web, how to expose service operations via HTTP GET, how to control the format of data exposed by service operations, and finally how to use the WebOperationContext to access the specifics of HTTP.

Published in: Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

REST, JSON and RSS with WCF 3.5

  1. 1. REST, JSON and RSS with Windows Communication Foundation 3.5 Rob Windsor ObjectSharp Consulting
  2. 2. 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
  3. 3. Agenda WCF Overview JSON Services HTTP Programming Model Syndication
  4. 4. Windows Communication Foundation 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
  5. 5. The ABCs of WCF Client Service A B C C B A Message A B C Address Binding Contract (Where) (How) (What)
  6. 6. 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
  7. 7. WCF Services
  8. 8. Agenda WCF Overview JSON Services HTTP Programming Model Syndication
  9. 9. What is JSON? 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);
  10. 10. WCF / AJAX Integration 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!
  11. 11. JSON Services
  12. 12. Agenda WCF Overview JSON Services HTTP Programming Model Syndication
  13. 13. Web Concepts (REST) 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.
  14. 14. The Web, the URI, and Apps{artist}/{album}{artist}?album={album}
  15. 15. Modeling a URI in .NET 3.5 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
  16. 16. Roundtrip Data in a URI 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”];
  17. 17. URIs in WCF Contracts 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);
  18. 18. HTTP Verbs in WCF Contracts 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);
  19. 19. Data Formats and the Web 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
  20. 20. Specifying Data Format in WCF 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; }
  21. 21. Hosting / Binding 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!
  22. 22. View It and Do It
  23. 23. Agenda Level-set JSON Services HTTP Programming Model Syndication
  24. 24. Syndication Goals in .NET 3.5 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
  25. 25. Syndication in .NET Fx 3.5 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!
  26. 26. Syndication Contracts in WCF [ServiceKnownType(typeof(Atom10FeedFormatter))] [ServiceKnownType(typeof(Rss20FeedFormatter))] [ServiceContract] interface IAlbumSyndication { [OperationContract] [WebGet(UriTemplate=“Images/{format}quot;)] SyndicationFeedFormatter<SyndicationFeed> Feed(String format); }
  27. 27. Syndication with PictureServices
  28. 28. Web Centric Features in WCF 3.5 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
  29. 29. Resources Microsoft WCF Community Site PictureServices Samples The EndPoint on Channel 9 Justin Smith’s Blog Steve Maine’s Blog Getting Started with WCF