Create a REST API with WCF

Source: Internet
Author: User
Tags exception handling oauth

Aaron Sloman and Haider Sabri in MIX08 talk about how to "create a Rest API using WCF", which introduces an example of a rest application named Restchess.

Aaron Sloman first talked about some background information that supports rest in WCF. Then, Haider Sabri a brief introduction to the main features of rest and rest. According to Haider, the key advantage that rest provides is that:

Low threshold for learning

Roa follows the web semantics ... APIs suitable for Web services

Flexible response Format

"If the client is unknown, rest is the best option when creating a generic API," he said.

WCF supports rest through the Web programming model, which is added in the. NET Framework 3.5, including the following concepts:

URI Templates & Webget/webinvoke

They are attributes that map HTTP requests to methods in the class.

WebHttpBinding

WebHttpBinding is a binding of WCF that allows WCF to send content based on webhttp.

You can easily switch between rest and soap APIs.

Weboperationcontext

Weboperationcontext supports the process of requesting and responding in a restful manner.

Haider Sabri introduced Restchess, which is the rest API for chess games. Restchess is both a good example of the rest API implemented using the WCF Web programming model, as well as an extension set (custom WCF bindings and behavior) that compensates for the drawbacks of the WCF Web programming model:

A flexible URIs

The. svc extension for the service is required when the IIS hosting service is in place.

restchess provides a URL rewrite and is implemented as an HTTP module in IIS 7.

Build a bridge for low-level rest clients that support only get and post

There are two common ways to solve this problem:

Custom HTTP Headers

_method Query Parameters

Restchess implements a custom WCF channel that distinguishes advanced and low-level rest clients, and modifies messages that are passed by post requests to the appropriate HTTP method using a hidden delete or Put method.

Validating requests with data signatures

Restchess uses OAuth, a "open protocol that supports secure API validation" that contains data signatures.

The OAuth caller passes the signature as a query parameter.

A custom WCF channel is responsible for checking whether the request has a OAuth key, and if the caller is not authenticated (the key does not exist or is not recognized) before the request is distributed to the service model, an exception is thrown.

Various forms of expression

WCF supports only XML and JSON.

Rest exception Handling

The Restchess team builds an engine that maps exceptions to the appropriate HTTP error codes.

Although the team completed the Shing task for the WCF Web programming model, Haider said: "The flash of WCF is that it can be extended, added, and built on a high level of foundation".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.