220 likes | 345 Views
Implementing RESTful Services With Windows Communication Foundation 3.5 SP1. Concepts and Introduction(Part 1 of 2). Session Objectives. Provide you with an overview of REST Understand the REST support of WCF 3.5 Learn how to build an AJAX-Friendly service
E N D
Implementing RESTful Services With Windows Communication Foundation 3.5 SP1 • Concepts and Introduction(Part 1 of 2)
Session Objectives • Provide you with an overview of REST • Understand the REST support of WCF 3.5 • Learn how to build an AJAX-Friendly service • Learn how to build a HI-REST service • Get you fired up with Demos
Agenda • Why The Web Works • What is REST? • How REST works in WCF 3.5 • LO-REST Demo (AJAX) • HI-REST Demo (AJAX & Silverlight 2.0)
Why The Web Works • Simple & Open • Addressing scheme – URI • Application protocol – HTTP • Representation Format - (X)HTML • Response Codes – HTTP Status • Scales Best When • Stateless • Cached • It works because people find value in it
How do we move from a web of pages to a web of services? Circa 1998
SOAP Web Services • HTTP POST of an XML Message (SOAP Envelope) • WSDL describes SOAP service • WS-* provides extended functionality • Standardized and interoperable • <s:Envelope> • <s:Header> • <Action s:mustUnderstand="1"> • http://t.org/IService1/GetData • </Action> • </s:Header> • <s:Body> • <GetData> • <value>1</value> • </GetData> • </s:Body> • </s:Envelope>
WCF 3.0 SOAP/HTTP Bindings • basicHttpBinding • Conforms to WS-I Basic Profile 1.1 standards • Compatibile with ASP.NET Web Services (asmx) • wsHttpBinding • A composable protocol stack allowing standards based additions • WS-Security
Agenda • Why The Web Works • What is REST? • How REST works in WCF 3.5 • LO-REST Demo (AJAX) • HI-REST Demo (AJAX & Silverlight 2.0)
RESTfulTenents • The Web of Services should work the way the Web of Pages works. • Simple & Open • The Universal API - HTTP • Linked Resources • Services Scale Best When • Stateless • Cached • Built into the HTTP protocol when leveraged properly
REST Continuum • Well Constructed URIs • HTTP Verbs • GET – Fetch • PUT – Update / Insert • DELETE – Delete • POST – Append • Standard Representations Hi-REST Lo-REST RESTfullness Purists Pragmatists • POST to 1 URI OK • Querystrings OK • HTTP Verbs • GET – Fetch • POST - Overloaded • AJAX Services • POX OK
The Microsoft Position • SOAP is great • REST is great • Use WCF to build either one and go in peace
Agenda • Why The Web Works • What is REST? • How REST works in WCF 3.5 • LO-REST Demo (AJAX) • HI-REST Demo (AJAX & Silverlight 2.0)
webHttpBinding • New “web-friendly” WCF Binding in Fx 3.5 • Allows for the development of RESTful services • Does not use SOAP envelopes • HTTP and HTTPS Transports Only • Works across REST Continuum • Supports all HTTP verbs • Web Message Encoding • JSON • XML • Binary (streams)
[WebGet] and [WebInvoke] • Indicate the HTTP Method for the operation • WebGet – Don’t make me write it • WebInvoke – All verbs other than GET (Method parameter takes in the name of the Verb) • Other Parameters • BodyStyle – Indicates whether the Request / Response are wrapped or not • RequestFormat – Json or Xml • ResponseFormat – Json or Xml • UriTemplate – Covered next …
UriTemplate • String that allows you to define the structure of the URI, as well as to define “Holes” • The “Holes” are variables • You Bind the template with parameters to fill the holes • {productId} hole / variable gets bound to productId parameter in operation Hole [OperationContract] [WebGet(UriTemplate=“product/{productId}")] ProductGetProduct(intproductId);
WebGet/WebInvoke Examples [OperationContract] [WebInvoke( Method=“PUT", ResponseFormat=WebMessageFormat.Json, UriTemplate=“product/{productId}")] ProductUpdateProduct(intproductId, product p); [OperationContract] [WebGet(ResponseFormat=WebMessageFormat.Json, UriTemplate=“product/{productId}")] ProductDataGetProduct(intproductId);
webHttpBinding Endpoint Behaviors • Endpoint Behaviors - extend run-time behavior for an endpoint • webHttp - enables the Web programming model for a WCF service • enableWebScript – ASP.NET AJAX friendly endpoint behavior • Aligns nicely with ASP.NET AJAX client • SubClasses webHttp • Provides ASP.NET AJAX proxy generation • Only supports GET and overloaded POST • Does not support UriTemplates
Demo LO-REST (AJAX Friendly)
Demo HI-REST
Summary • REST principles borrow from principles of the web • Architectures vary with regard to adherence to REST principles • webHttpBinding supports architectures across the REST continuum • enableWebScript • Productivity features for ASP.NET AJAX applications • Imposes limitations • webHttp – Provides ability to implement services that adhere to strictest of standards
Resources • "AJAX Integration and JSON support" in .Net 3.5 conceptual docs • "Web Programming Model" in .Net 3.5 conceptual docs • Posting of Various Valuable WCF Docs • Steve Maine's Blog • HTTP Programming with WCF and the .NET Framework 3.5 (MSDN Magazine) • WCF, WF and .NET 3.5 Whitepapers by David Chappell