REST, JSON and RSS with Windows Communication Foundation 3.5

Preview:

DESCRIPTION

REST, JSON and RSS with Windows Communication Foundation 3.5. Rob Windsor ObjectSharp Consulting rwindsor@objectsharp.com. 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 - PowerPoint PPT Presentation

Citation preview

REST, JSON and RSS with Windows Communication Foundation 3.5

Rob WindsorRob WindsorObjectSharp ConsultingObjectSharp Consultingrwindsor@objectsharp.comrwindsor@objectsharp.com

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

WCF OverviewWCF OverviewJSON ServicesJSON ServicesHTTP Programming ModelHTTP Programming ModelSyndicationSyndication

AgendaAgenda

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

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)

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

WCF Services

WCF OverviewWCF OverviewJSON ServicesJSON ServicesHTTP Programming ModelHTTP Programming ModelSyndicationSyndication

AgendaAgenda

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?

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

JSON Services

WCF OverviewWCF OverviewJSON ServicesJSON ServicesHTTP Programming ModelHTTP Programming ModelSyndicationSyndication

AgendaAgenda

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)

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

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

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

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

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

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

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

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

View It and Do It

Level-setLevel-setJSON ServicesJSON ServicesHTTP Programming ModelHTTP Programming ModelSyndicationSyndication

AgendaAgenda

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

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

[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

Syndication with PictureServices

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

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

Recommended