SlideShare une entreprise Scribd logo
1  sur  27
WCF:
Building RESTful
Services
             S Steve Maine
               Senior Program
               Manager
               Microsoft Corporation
Source: Pingdom.com
1993:
Web == Content
Content-Driven Web Architecture

    The Browser
        Generic client experience
    URI’s
        Addressing and identification
    HTML
        Common presentation format
    Hyperlinks
        Anarchic interconnectivity
    HTTP GET
        Common operation everything supports
2008:
Web == Content +
 Capabilities
Capability-Enabled Web
Architecture
    Rich Browser Clients
        Programmability via script or plugins
    HTTP
        Baseline application protocol
        Common set of operations + status codes
    Domain-neutral data-oriented formats
        JSON, Atom/Atom Publishing
        Refine to support domain-specific schemas
    Presentation formats
        HTML, CSS
RESTful Tenents

  The Web is a graph of linked Resources
  Resources are identified by URI’s
  Resources support a fixed set of operations
        In practice, these are defined by HTTP
    Applications follow links
     to achieve late binding

 REST is an architectural style, not a specification
REST Continuum



Purists                         RESTfullness                Pragmatists


    Well Constructed URIs                 POST to 1 URI OK
                                            Querystrings OK
                                           HTTP Verbs
    HTTP Verbs                              GET – Fetch
         GET – Fetch                        POST – Overloaded
         PUT – Update/Insert
         DELETE – Delete
         POST – Append
                                           AJAX Services
    Standard Representations
                                           POX OK
demo
       AJAX Services
       with WCF
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
    Supports several wire formats:
        XML
        JSON
        Binary (streams)
WebServiceHost

    Specialized SerivceHost for RESTful services
        Eliminates need for lots of configuration
        Automatically configures address, binding,
     contract
  Optimized for single-endpoint services
  Use from .svc file:
<%@ ServiceHost Language="C#" Debug="true"
                Service="Caching1.FeedService"
Factory=“System.ServiceModel.Activation.WebServiceHostFactory”
%>"%>
[WebGet] And [WebInvoke]

  Binds a WCF operation to URI
   space and HTTP method
  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

    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
                                            Variable
     [OperationContract]
     [WebGet(UriTemplate=“product/{productId}")]
     Product GetProduct(int productId);

    {productId} hole / variable gets bound
     to productId parameter in operation
announcing
      WCF REST
      Starter Kit
WCF REST Starter Kit

    Microsoft.ServiceModel.Web.dll
       New features supporting RESTful services
    Visual Studio 2008 Templates
       REST Collections/Singleton Services
       Atom Feed/Atom Publishing Protocol
       HTTP/POX Services
  REST Samples
  Codeplex Project
       Released at PDC
       Written by WCF team
       Features may be included in .NET 4.0
demo
       The REST
       0f WCF
What We've Talked About Today

  REST and the “zen” of the web
  WCF features for REST scenarios
        [WebGet] + [WebInvoke]
        UriTemplate
        WebHttpBinding
        And many more…
    The WCF REST Starter Kit
        Available today at http://msdn.com/wcf/rest
Evals & Recordings



              l
   Please fil                This sess
                                       ion will
             r
   out you                   be availa
                                        ble as
    evalua  tion for         a record
                     :                 ing at:
     this se ssion at



               www.microsoftpdc.com
QA   &

Please use the microphones
provided
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other
                                                                                           countries.
   The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to
changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of
               this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
WebGet/WebInvoke Examples



 [OperationContract]
 [WebGet( ResponseFormat=WebMessageFormat.Json,
          UriTemplate=“product/{productId}")]
 ProductData GetProduct(int productId);



 [OperationContract]
 [WebInvoke( Method=“PUT",
             ResponseFormat=WebMessageFormat.Json,
             UriTemplate=“product/{productId}")]
 Product UpdateProduct(int productId, product p);
WebProtocolException

  CLR Exception that can be turned into HTTP
   status code on the wire
  Can optionally carry detail information for
   display in the browser


 [OperationContract, WebGet]
 Stream EchoText(string text)
 {
   if( text == null )
     throw new WebProtocolException( HttpStatusCode.BadRequest );
   . . .
 }
AdapterStream

    Used when you want a Stream or
     TextWriter but don’t have one handy

[OperationContract, WebGet]
Stream EchoText(string text)
{
     return new AdapterStream( textWriter =>
                {
                    textWriter.WriteLine("You said: ");
                    textWriter.WriteLine(text);
                    textWriter.Flush();
                }, Encoding.UTF8);
}
Request Interceptors

  MessageInspectors at the channel layer
  Think of them like a per-service pipeline
  Arbitrary message manipulation prior to
   dispatch; can prevent dispatch too

 public class XHttpMethodOverrideInterceptor:RequestInterceptor
 {

       public override void ProcessRequest(
                             ref RequestContext requestContext)
        {
          //Change the HTTP method used for dispatch based on
          //the X-HTTP-Method-Override header
        }
 }
[WebCache]

  Integrates WCF operations with ASP.NET
   caching (requires ASP.NET Compat Mode)
  Cache profile stored in Web.config
[OperationContract, WebGet]
[WebCache(CacheProfileName="CacheFor1Min")]
public Atom10FeedFormatter GetFeed(int i) { … }

<system.web>
  <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
            <add name="CacheFor1Min" duration="60"
                 enabled="true" location="Any" varyByParam=“i"/>
        </outputCacheProfiles>
      </outputCacheSettings>
   </caching>
 </system.web>
ASP.NET Authentication Support

  “It just works” with ASP.NET
  Full support for Membership + Roles
  Requires ASP.NET Compatibility Mode



 [OperationContract, WebGet]
 [PrincipalPermission(SecurityAction.Demand, Role=“admin")]
 SampleResponseBody GetData(int i, string s)

Contenu connexe

Tendances

Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleFelix Meschberger
 
Messaging for Web and Mobile with Apache ActiveMQ
Messaging for Web and Mobile with Apache ActiveMQMessaging for Web and Mobile with Apache ActiveMQ
Messaging for Web and Mobile with Apache ActiveMQdejanb
 
Windows service best practice
Windows service best practiceWindows service best practice
Windows service best practiceYu GUAN
 
Managing an OSGi Framework with Apache Felix Web Console
Managing an OSGi Framework with  Apache Felix Web ConsoleManaging an OSGi Framework with  Apache Felix Web Console
Managing an OSGi Framework with Apache Felix Web ConsoleFelix Meschberger
 
Introduction to service stack
Introduction to service stackIntroduction to service stack
Introduction to service stackFabio Cozzolino
 
Web service through cxf
Web service through cxfWeb service through cxf
Web service through cxfRoger Xia
 
Java API for XML Web Services (JAX-WS)
Java API for XML Web Services (JAX-WS)Java API for XML Web Services (JAX-WS)
Java API for XML Web Services (JAX-WS)Peter R. Egli
 
Spring + WebSocket integration
Spring + WebSocket integrationSpring + WebSocket integration
Spring + WebSocket integrationOleksandr Semenov
 
JMS and ActiveMQ - VuNV 201307
JMS and ActiveMQ - VuNV 201307JMS and ActiveMQ - VuNV 201307
JMS and ActiveMQ - VuNV 201307Framgia Vietnam
 
Camelone-2012 HTML5 WebSocket ActiveMQ/Camel
Camelone-2012 HTML5 WebSocket ActiveMQ/CamelCamelone-2012 HTML5 WebSocket ActiveMQ/Camel
Camelone-2012 HTML5 WebSocket ActiveMQ/CamelCharles Moulliard
 
Jax ws
Jax wsJax ws
Jax wsF K
 
Large scale web socket system with AWS and Web socket
Large scale web socket system with AWS and Web socketLarge scale web socket system with AWS and Web socket
Large scale web socket system with AWS and Web socketLe Kien Truc
 
Lecture 7 Web Services JAX-WS & JAX-RS
Lecture 7   Web Services JAX-WS & JAX-RSLecture 7   Web Services JAX-WS & JAX-RS
Lecture 7 Web Services JAX-WS & JAX-RSFahad Golra
 
Services web RESTful
Services web RESTfulServices web RESTful
Services web RESTfulgoldoraf
 

Tendances (20)

WebSockets in JEE 7
WebSockets in JEE 7WebSockets in JEE 7
WebSockets in JEE 7
 
Jetty Vs Tomcat
Jetty Vs TomcatJetty Vs Tomcat
Jetty Vs Tomcat
 
Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style
 
Messaging for Web and Mobile with Apache ActiveMQ
Messaging for Web and Mobile with Apache ActiveMQMessaging for Web and Mobile with Apache ActiveMQ
Messaging for Web and Mobile with Apache ActiveMQ
 
Ws
WsWs
Ws
 
Windows service best practice
Windows service best practiceWindows service best practice
Windows service best practice
 
Apache Felix Web Console
Apache Felix Web ConsoleApache Felix Web Console
Apache Felix Web Console
 
Managing an OSGi Framework with Apache Felix Web Console
Managing an OSGi Framework with  Apache Felix Web ConsoleManaging an OSGi Framework with  Apache Felix Web Console
Managing an OSGi Framework with Apache Felix Web Console
 
Java EE 6
Java EE 6Java EE 6
Java EE 6
 
J web socket
J web socketJ web socket
J web socket
 
Introduction to service stack
Introduction to service stackIntroduction to service stack
Introduction to service stack
 
Web service through cxf
Web service through cxfWeb service through cxf
Web service through cxf
 
Java API for XML Web Services (JAX-WS)
Java API for XML Web Services (JAX-WS)Java API for XML Web Services (JAX-WS)
Java API for XML Web Services (JAX-WS)
 
Spring + WebSocket integration
Spring + WebSocket integrationSpring + WebSocket integration
Spring + WebSocket integration
 
JMS and ActiveMQ - VuNV 201307
JMS and ActiveMQ - VuNV 201307JMS and ActiveMQ - VuNV 201307
JMS and ActiveMQ - VuNV 201307
 
Camelone-2012 HTML5 WebSocket ActiveMQ/Camel
Camelone-2012 HTML5 WebSocket ActiveMQ/CamelCamelone-2012 HTML5 WebSocket ActiveMQ/Camel
Camelone-2012 HTML5 WebSocket ActiveMQ/Camel
 
Jax ws
Jax wsJax ws
Jax ws
 
Large scale web socket system with AWS and Web socket
Large scale web socket system with AWS and Web socketLarge scale web socket system with AWS and Web socket
Large scale web socket system with AWS and Web socket
 
Lecture 7 Web Services JAX-WS & JAX-RS
Lecture 7   Web Services JAX-WS & JAX-RSLecture 7   Web Services JAX-WS & JAX-RS
Lecture 7 Web Services JAX-WS & JAX-RS
 
Services web RESTful
Services web RESTfulServices web RESTful
Services web RESTful
 

Similaire à Building+restful+webservice

Building RESTful Services with WCF 4.0
Building RESTful Services with WCF 4.0Building RESTful Services with WCF 4.0
Building RESTful Services with WCF 4.0Saltmarch Media
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonWeb API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonAdnan Masood
 
REST, JSON and RSS with WCF 3.5
REST, JSON and RSS with WCF 3.5REST, JSON and RSS with WCF 3.5
REST, JSON and RSS with WCF 3.5Rob Windsor
 
.Net3.5 Overview
.Net3.5 Overview.Net3.5 Overview
.Net3.5 Overviewllangit
 
Annotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVCAnnotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVCJohn Lewis
 
Wcf rest api introduction
Wcf rest api introductionWcf rest api introduction
Wcf rest api introductionHimanshu Desai
 
Automated testing web services - part 1
Automated testing web services - part 1Automated testing web services - part 1
Automated testing web services - part 1Aleh Struneuski
 
The Windows Runtime and the Web
The Windows Runtime and the WebThe Windows Runtime and the Web
The Windows Runtime and the WebJeremy Likness
 
SharePoint Alerts with WCF and jQuery
SharePoint Alerts with WCF and jQuerySharePoint Alerts with WCF and jQuery
SharePoint Alerts with WCF and jQueryNick Hadlee
 
There is time for rest
There is time for rest There is time for rest
There is time for rest SoftServe
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiTiago Knoch
 
WinRT and the Web: Keeping Windows Store Apps Alive and Connected
WinRT and the Web: Keeping Windows Store Apps Alive and ConnectedWinRT and the Web: Keeping Windows Store Apps Alive and Connected
WinRT and the Web: Keeping Windows Store Apps Alive and ConnectedJeremy Likness
 
D22 Portlet Development With Open Source Frameworks
D22 Portlet Development With Open Source FrameworksD22 Portlet Development With Open Source Frameworks
D22 Portlet Development With Open Source FrameworksSunil Patil
 
D22 portlet development with open source frameworks
D22 portlet development with open source frameworksD22 portlet development with open source frameworks
D22 portlet development with open source frameworksSunil Patil
 

Similaire à Building+restful+webservice (20)

RESTful WCF Services
RESTful WCF ServicesRESTful WCF Services
RESTful WCF Services
 
Building RESTful Services with WCF 4.0
Building RESTful Services with WCF 4.0Building RESTful Services with WCF 4.0
Building RESTful Services with WCF 4.0
 
Wcf remaining
Wcf remainingWcf remaining
Wcf remaining
 
Web API or WCF - An Architectural Comparison
Web API or WCF - An Architectural ComparisonWeb API or WCF - An Architectural Comparison
Web API or WCF - An Architectural Comparison
 
ASP.NET WEB API Training
ASP.NET WEB API TrainingASP.NET WEB API Training
ASP.NET WEB API Training
 
REST, JSON and RSS with WCF 3.5
REST, JSON and RSS with WCF 3.5REST, JSON and RSS with WCF 3.5
REST, JSON and RSS with WCF 3.5
 
.Net3.5 Overview
.Net3.5 Overview.Net3.5 Overview
.Net3.5 Overview
 
Annotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVCAnnotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVC
 
Wcf rest api introduction
Wcf rest api introductionWcf rest api introduction
Wcf rest api introduction
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Automated testing web services - part 1
Automated testing web services - part 1Automated testing web services - part 1
Automated testing web services - part 1
 
The Windows Runtime and the Web
The Windows Runtime and the WebThe Windows Runtime and the Web
The Windows Runtime and the Web
 
SharePoint Alerts with WCF and jQuery
SharePoint Alerts with WCF and jQuerySharePoint Alerts with WCF and jQuery
SharePoint Alerts with WCF and jQuery
 
There is time for rest
There is time for rest There is time for rest
There is time for rest
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 
WinRT and the Web: Keeping Windows Store Apps Alive and Connected
WinRT and the Web: Keeping Windows Store Apps Alive and ConnectedWinRT and the Web: Keeping Windows Store Apps Alive and Connected
WinRT and the Web: Keeping Windows Store Apps Alive and Connected
 
D22 Portlet Development With Open Source Frameworks
D22 Portlet Development With Open Source FrameworksD22 Portlet Development With Open Source Frameworks
D22 Portlet Development With Open Source Frameworks
 
D22 portlet development with open source frameworks
D22 portlet development with open source frameworksD22 portlet development with open source frameworks
D22 portlet development with open source frameworks
 
Servlet 3.0
Servlet 3.0Servlet 3.0
Servlet 3.0
 
WCF And ASMX Web Services
WCF And ASMX Web ServicesWCF And ASMX Web Services
WCF And ASMX Web Services
 

Building+restful+webservice

  • 1. WCF: Building RESTful Services S Steve Maine Senior Program Manager Microsoft Corporation
  • 4. Content-Driven Web Architecture  The Browser Generic client experience  URI’s Addressing and identification  HTML Common presentation format  Hyperlinks Anarchic interconnectivity  HTTP GET Common operation everything supports
  • 5. 2008: Web == Content + Capabilities
  • 6. Capability-Enabled Web Architecture  Rich Browser Clients Programmability via script or plugins  HTTP Baseline application protocol Common set of operations + status codes  Domain-neutral data-oriented formats JSON, Atom/Atom Publishing Refine to support domain-specific schemas  Presentation formats HTML, CSS
  • 7. RESTful Tenents  The Web is a graph of linked Resources  Resources are identified by URI’s  Resources support a fixed set of operations In practice, these are defined by HTTP  Applications follow links to achieve late binding REST is an architectural style, not a specification
  • 8. REST Continuum Purists RESTfullness Pragmatists  Well Constructed URIs  POST to 1 URI OK Querystrings OK  HTTP Verbs  HTTP Verbs  GET – Fetch  GET – Fetch  POST – Overloaded  PUT – Update/Insert  DELETE – Delete  POST – Append  AJAX Services  Standard Representations  POX OK
  • 9. demo AJAX Services with WCF
  • 10. 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  Supports several wire formats: XML JSON Binary (streams)
  • 11. WebServiceHost  Specialized SerivceHost for RESTful services Eliminates need for lots of configuration Automatically configures address, binding, contract  Optimized for single-endpoint services  Use from .svc file: <%@ ServiceHost Language="C#" Debug="true" Service="Caching1.FeedService" Factory=“System.ServiceModel.Activation.WebServiceHostFactory” %>"%>
  • 12. [WebGet] And [WebInvoke]  Binds a WCF operation to URI space and HTTP method  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 
  • 13. 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 Variable [OperationContract] [WebGet(UriTemplate=“product/{productId}")] Product GetProduct(int productId);  {productId} hole / variable gets bound to productId parameter in operation
  • 14. announcing WCF REST Starter Kit
  • 15. WCF REST Starter Kit  Microsoft.ServiceModel.Web.dll New features supporting RESTful services  Visual Studio 2008 Templates REST Collections/Singleton Services Atom Feed/Atom Publishing Protocol HTTP/POX Services  REST Samples  Codeplex Project Released at PDC Written by WCF team Features may be included in .NET 4.0
  • 16. demo The REST 0f WCF
  • 17. What We've Talked About Today  REST and the “zen” of the web  WCF features for REST scenarios [WebGet] + [WebInvoke] UriTemplate WebHttpBinding And many more…  The WCF REST Starter Kit Available today at http://msdn.com/wcf/rest
  • 18. Evals & Recordings l Please fil This sess ion will r out you be availa ble as evalua tion for a record : ing at: this se ssion at www.microsoftpdc.com
  • 19. QA & Please use the microphones provided
  • 20. © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • 21.
  • 22. WebGet/WebInvoke Examples [OperationContract] [WebGet( ResponseFormat=WebMessageFormat.Json, UriTemplate=“product/{productId}")] ProductData GetProduct(int productId); [OperationContract] [WebInvoke( Method=“PUT", ResponseFormat=WebMessageFormat.Json, UriTemplate=“product/{productId}")] Product UpdateProduct(int productId, product p);
  • 23. WebProtocolException  CLR Exception that can be turned into HTTP status code on the wire  Can optionally carry detail information for display in the browser [OperationContract, WebGet] Stream EchoText(string text) { if( text == null ) throw new WebProtocolException( HttpStatusCode.BadRequest ); . . . }
  • 24. AdapterStream  Used when you want a Stream or TextWriter but don’t have one handy [OperationContract, WebGet] Stream EchoText(string text) { return new AdapterStream( textWriter => { textWriter.WriteLine("You said: "); textWriter.WriteLine(text); textWriter.Flush(); }, Encoding.UTF8); }
  • 25. Request Interceptors  MessageInspectors at the channel layer  Think of them like a per-service pipeline  Arbitrary message manipulation prior to dispatch; can prevent dispatch too public class XHttpMethodOverrideInterceptor:RequestInterceptor { public override void ProcessRequest( ref RequestContext requestContext) { //Change the HTTP method used for dispatch based on //the X-HTTP-Method-Override header } }
  • 26. [WebCache]  Integrates WCF operations with ASP.NET caching (requires ASP.NET Compat Mode)  Cache profile stored in Web.config [OperationContract, WebGet] [WebCache(CacheProfileName="CacheFor1Min")] public Atom10FeedFormatter GetFeed(int i) { … } <system.web> <caching> <outputCacheSettings> <outputCacheProfiles> <add name="CacheFor1Min" duration="60" enabled="true" location="Any" varyByParam=“i"/> </outputCacheProfiles> </outputCacheSettings> </caching> </system.web>
  • 27. ASP.NET Authentication Support  “It just works” with ASP.NET  Full support for Membership + Roles  Requires ASP.NET Compatibility Mode [OperationContract, WebGet] [PrincipalPermission(SecurityAction.Demand, Role=“admin")] SampleResponseBody GetData(int i, string s)