InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Creating a RESTful API with WCF

Posted by Hartmut Wilms on Mar 13, 2008

Sections
Architecture & Design,
Development,
Enterprise Architecture
Topics
.NET ,
REST ,
SOA ,
.NET Framework
Tags
Web services ,
WCF

Aaron Sloman and Haider Sabri gave a talk about "Creating a RESTful API with WCF" at MIX08 introducing a sample REST application called RESTChess.

The talks starts with some background information about the REST support within WCF provided by Aaron Sloman. Haider Sabri continues by giving a brief introduction to REST and its principals. According to Haider the key benefits of REST are

    • Low barrier of entry
    • ROA follows Web Semantics ... convenient for Web Service APIs
    • Flexibility in Response formats

In his opinion "REST is best choice when creating a general API when clients are unknown".

WCF supports REST through the Web Programming Model, which has been added in the .NET Framework 3.5 and consists of the following concepts:

  • URI Templates & WebGet/WebInvoke
    • These are attributes, which map HTTP requests to methods in a class.
  • WebHttpBinding
    • WebHttpBinding is a WCF binding that enables dispatching based on the above.
    • You can easily switch between REST and a SOAP API.
  • WebOperationContext
    • The WebOperationContext allows to manipulate requests and responses in a RESTful way.

Haider Sabri introduces RESTChess, which is a RESTful API for a chess game. RESTChess is both a nice sample of a RESTful API implemented with WCF's Web Programming Model and a bunch of extensions (custom WCF bindings and behaviors) that (partially) make up for the shortcomings of the WCF web programming model:

  • Flexible URIs
    • The service .svc extension is required when hosting services in IIS.
    • RESTChess provides a URL rewriter implemented as a HTTP module within IIS 7.
  • Bridging Low REST clients, which only support GET and POST
    • There are two common approaches to solve this problem:
      • Custom HTTP headers
      • _method query parameter
    • RESTChess implements a custom WCF channel that distinguishes between high and low REST clients and changes messages that make use of  DELETE or PUT methods tunneled through a POST request to the appropriate HTTP method.
  • Request Authentication with Digital Signing
    • RESTChess makes use of OAuth,  which is an "open protocol to allow secure API authentication" including digital signature.
    • The signature is passed via an OAuth consumer key as a query parameter.
    • A custom WCF channel checks the request for the OAuth key, and throws an exception before the request gets dispatched to the service model in case the consumer is not authenticated (the key is not present or unknown).
  • Multiple Representation Formats
    • WCF only supports XML and JSON.
  • RESTful Exception Handling
    • The RESTChess team build an engine to map exceptions to appropriate HTTP error codes.

Although the team had to add several missing pieces to the WCF web programming model, in Haider's opinion "the area where WCF shines is the ability to extend it, to insert into it, to build on the stack at a high level".

  • This article is part of a featured topic series on SOA
Interesting, but arguably an inappropriate use of REST by Gerhard Kessell-Haak Posted
  1. Back to top

    Interesting, but arguably an inappropriate use of REST

    by Gerhard Kessell-Haak

    An interesting article, particularly with respect to its implementation.

    However, its arguable that this is not an appropriate use of the REST architectural style. REST is most appropriate when dealing with resources (indeed, Representational State Transfer is a style of software architecture that is ideal for distributed hypermedia systems, and was first identified with the World Wide Web).

    I would argue XML-RPC in conjunction with HTTP authentication (or, if you like complexity for the sake of it, SOAP) to be a better fit as it implies state on the server (unlike REST), and would result in a cleaner mapping to the underlying API.

Educational Content

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.