REST, JSON and RSS with WCF 3.5

download REST, JSON and RSS with WCF 3.5

of 29

  • date post

  • Category


  • view

  • download


Embed Size (px)


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.

Transcript of REST, JSON and RSS with WCF 3.5

  • 1. REST, JSON and RSS with Windows Communication Foundation 3.5 Rob Windsor ObjectSharp Consulting

2. Me.About Visual Basic MVPSenior Consultant with ObjectSharp ConsultingPresident of the Toronto Visual Basic UserGroupMember of the MSDN Canada Speakers BureauContact me via my blog 3. AgendaWCF OverviewJSON ServicesHTTP Programming ModelSyndication 4. Windows Communication Foundation One-stop-shop for services Consistent object model First released with .NET Framework 3.0Focus on the functionality, WCF takes care of the plumbing 5. The ABCs of WCF Client ServiceA B C C BAMessage A B CAddress Binding Contract(Where)(How) (What) 6. WCF Standard Bindings NameTransportEncoding Interop BasicHttpBindingHTTP/HTTPS Text Yes NetTcpBinding TCPBinary No NetPeerTcpBinding P2PBinary No NetNamedPipeBinding IPCBinary No WSHttpBinding HTTP/HTTPS Text, MTOM Yes WSFederationBinding HTTP/HTTPS Text, MTOM Yes WSDualHttpBinding HTTP/HTTPS Text, MTOM Yes NetMsmqBindingMSMQ Binary No NetIntegrationBinding MSMQ Binary Yes 7. WCF Services 8. AgendaWCF OverviewJSON ServicesHTTP Programming ModelSyndication 9. What is JSON? JavaScript Object Notation Format for bridging JavaScript and objectsEasier for browsers than XML ASP.NET AJAX & other AJAX toolkits use itOther web-aware clients also (Silverlight, etc.) var data = {temp : 59, descr : cloudy}; document.write (The weather is + data.descr); 10. WCF / AJAX IntegrationWCF AJAX support in Visual Studio Script manager, VS Project TemplatesWCF automatically generates JS proxyUsage pattern similar to existing one: Add service to Script Manager control Write JavaScript code to work with proxyConfiguration not required Via the WebScriptServiceHostFactory (.svc file)Works in ASP.NET Medium Trust! 11. JSON Services 12. AgendaWCF OverviewJSON ServicesHTTP Programming ModelSyndication 13. Web Concepts (REST)Embrace the URI Segments map to application logicHTTP 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. The Web, the URI, and{artist}/{album}{artist}?album={album} 15. Modeling a URI in .NET 3.5System.UriTemplateType for modeling URI to application semanticsCan bind data to a template, output a URICan match a URI to a template, retrieve dataSystem.UriTemplateMatchReturned from UriTemplate match operationsCan get relative paths and wildcard segmentsSystem.UriTemplateTableFor binding a URI to a group of UriTemplates 16. Roundtrip Data in a URIUri 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. URIs in WCF ContractsSimple 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. HTTP Verbs in WCF ContractsAll 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. Data Formats and the WebHTTP 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. Specifying Data Format in WCF WebOperationContext.Current provides access to incoming request headers Can also set outgoing response headers Some are shortcut for easier useStream GetAlbumImage(String bandName, String album){ Stream stream; // get the image from somewhere WebOperationContext.Current.OutgoingResponse.ContentType =image/jpeg; return stream; } 21. Hosting / BindingWebHttpBinding endpoint on a ServiceHost Add WebHttpBehavior to the endpointUse WebServiceHost/Factory in most casesWeb endpoints do not support WSDLWorks in ASP.NET Medium Trust! 22. View It and Do It 23. AgendaLevel-setJSON ServicesHTTP Programming ModelSyndication 24. Syndication Goals in .NET 3.5Syndications are more than news and blogs Representation of any set of data Usually slowly changingUnified object model for RSS and Atom SyndicationFeed / SyndicationItemFeeds are service operationsConsume as a service or as document 25. Syndication in .NET Fx 3.5Single stop for syndications Create and Consume with or without WCFEasy to use object modelTransport AgnosticSupports syndication extensionsFormat Agnostic RSS 2.0 & ATOM 1.0, others possible Works in ASP.NET Medium Trust! 26. Syndication Contracts in WCF [ServiceKnownType(typeof(Atom10FeedFormatter))] [ServiceKnownType(typeof(Rss20FeedFormatter))] [ServiceContract] interface IAlbumSyndication { [OperationContract] [WebGet(UriTemplate=Images/{format}quot;)] SyndicationFeedFormatterFeed(String format); } 27. Syndication with PictureServices 28. Web Centric Features in WCF 3.5 Simple HTTP service developmentSOAP and POX from the same contractJSON messaging capabilitySimple syndication really!Built on WCF extensibility points from .NET 3.0 29. ResourcesMicrosoft WCF Community Site Samples EndPoint on Channel 9 Smiths Blog Maines Blog Started with WCF