WCF REST Starter Kit Preview 2

 

Preview 2 of the starter kit introduces a second set of client-side features for accessing WCF and third-party REST services from within .Net applications. The new HttpClient class provides the REST developer with a uniform extensible model for sending HTTP requests and processing HTTP responses, in a variety of formats. The new "Paste Xml as Type" Visual Studio add-in enhances the serialization support in HttpClient by generating serializable types based on XML examples or XSD schema.

Download here

Creating and consuming Rss/Atom syndication feed using WCF 3.5

 

The .NET Framework did not provide any built-in functionality for creating or consuming syndication feeds until version 3.5 with the introduction of the System.ServiceModel.Syndication namespace. The most germane class in this namespace is SyndicationFeed. As you can probably guess from its name, this class represents a syndication feed. It has properties like Title, Description, Links, and Copyright, which provide details about the feed. The content items that make up the feed are specified via the Items property, which is a collection of SyndicationItem objects. The SyndicationFeed class also has a static Load method that parses and loads the information from a specified RSS 2.0 or Atom 1.0 syndication feed.

In addition to the SyndicationFeed and SyndicationItem classes, the System.ServiceModel.Syndication namespace also includes two formatter classes, Atom10FeedFormatter and Rss20FeedFormatter. These classes take a SyndicationFeed object and generate the corresponding XML content that conforms to either the Atom 1.0 or RSS 2.0 specificiations.

More… How to create a syndication feed for your website

What’s inside the WCF REST Starter Kit

  • Visual Studio Project Templates image
    • REST Singleton Service produces a service that exposes the full HTTP interface (GET, POST, PUT, and DELETE) around a singleton resource, and it automatically provides both XML and JSON representations for the underlying resource.
    • REST Collection Service similar to the REST Singleton Service only it also provides support for managing a collection of SampleItem resources.
    • HTTP Plain XML Service provides ONLY the GET and POST operations for those who don’t care about fully conforming to the RESTful design  and would rather build a REST/RPC hybrid service by overloading GET and POST using the HTTP Headers.
    • Atom Feed Service template provides a sample implementation that shows how to programmatically generate and return a SyndicationFeed instance.
    • Atom Publishing Protocol Service template produces a full-fledged AtomPub service capable of managing collections of resources as well as media entries.
  • WebProtocolException allows you to directly set your HTTP status code.
  • WebServiceHost2/WebServiceHost2Factory which provides a zero-config experience. Provide support for Help Page.
  • Help Page and using [WebHelp] for annotation. Simply navigate to /help image
  • [WebCache]
  • RequestInterceptor
  • Added Extension methods to the WebOperationContext class.

You can download the WCF REST Starter Kit here.

For samples of using the starter kit, look at this A Developer Guide to the WCF REST Starter Kit.

A Tutorial on Building RESTful Web Services with WCF 3.5

 

  • [WebGet], [WebInvoke] to define mappings for the Http Methods Interface
  • UriTemplate to map objects to method signatures through WebGet/WebInvoke Attributes image image
  • WebOperationContext to access the HTTP Request/Response including header and status code
  • <%@ ServiceHost Factory=”WebServiceHostFactory” to auto-config the Web endpoints binding and behavior without edit web.config.
  • aspNetCompatibilityEnabled(web.config) & AspNetCompatibilityRequirements(.cs) to access HttpContext
  • (optional) using Microsoft URL Rewrite Module for IIS 7.0 to remove the .svc from the URIs.
  • RequestFormat/ResponseFormat=WebMessageFormat.Xml/WebMessageFormat.Json through WebGet/WebInvoke Attributes.image
  • WebScriptServiceHost to auto-generate a Ajax-friendly JSON javascript proxy for web browsers. Append /js to the end of base address to get to the proxy.
  • Use SyndicationFeed, SyndicationItem, SyndicationContent to build generic logical feeds.
  • Use Atom10FeedFormatter, RSS20FeedFormatterClass to turn a SyndicationFeed object to Atom/RSS specific feed format. Method can use SyndicationFeedFormatter base class to return various formatter object.  
    image 
  • Use ServiceDocument, ServiceDocumentFormatter, AtomPub10ServiceDocumentFormatter, and Workspace to generate AtomPub service document. note: AtomPub as a standard application of the HTTP uniform interface(GET/PUT/POST/DELETE) but applied specifically to Atom feeds, which are modeled as collections of entries.
  • Too tedious, try WCF REST Starter Kit. and you can download here.

Via A Guide to Designing and Building RESTful Web Services with WCF 3.5

Building OAuth channel for WCF RESTful services

 

What is OAuth?

While OpenID and WS-Federation focus on delegating user identity (or a collection of identity claims), OAuth was designed to address a different and complementary scenario, the delegation of user authorization. In few words, OAuth allows a client application to obtain user consent (i.e. Authorization for consuming a private resource as access tokens) for executing operations over private resources on his behalf.

If you want to know more about how OAuth works, you should read the following posts

OAuth .NET Library

Alex Henderson (Aka Bittercoder) has written a pretty good OAuth library in .NET for implementing an OAuth consumer and service provider. The library is available here under a MIT license (do wherever you want with it), and it is very easy to use. Alex has definitively made a very good work.

OAuth WCF Channel using REST Starter Kit’s RequestInterceptor

WCF channel implementation for OAuth mounts on top of his library and it basically transforms a OAuth token into a .NET security principal that can be used later within the service implementation. The channel is implemented as a RequestInterceptor, one of new features introduced in the REST WCF Starter Kit. This interceptor basically captures the request at channel level and performs all the validations required by OAuth. The following sample illustrates how the interceptors can be plugged into an existing service host (service.svc),

Samples here: OAuth channel for WCF RESTful services – Pablo M. Cibraro (aka Cibrax)

Windows Communication Foundation (WCF) 3.5 REST Starter Kit

Windows Communication Foundation (WCF) REST Starter Kit

The WCF REST Starter Kit is a set of features, Visual Studio templates, samples and guidance that enable users to create REST style services using WCF. It provides features that enable or simplify various aspects of using the http capabilities in WCF, such as caching, security, error handling, help page support, conditional PUT, push style streaming, type based dispatch and semi-structured XML support. The WCF REST Starter Kit also provides Visual Studio templates for creating REST style services such as an Atom feed service, a REST-RPC hybrid service, Resource singleton and collection services and an Atom Publishing Protocol service. In addition, there are samples, documentation and other guidance that help to use these new capabilities.

Download WCF REST Starter KitDownload

Learn about WCF REST Starter Kit

Via Windows Communication Foundation (WCF) REST Starter Kit

In the case of REST Singleton Web Service, using jQuery to do AJAX is as simple as:

<script type=”text/javascript”>
    $(function() {
        $.getJSON(“http://localhost:62537/Service.svc/?format=json”, null, function(json) {
            $(‘#result’).html(json.Value);});  
    })
</script>

Walkthrough: Using ASP.NET Application Services

 

ASP.NET provides application services on the Web that enable client applications to access user authentication, role, and profile information. This information can be accessed by client applications that are written in different languages and that run on different operating systems. The only requirement is that these clients be able to communicate through SOAP 1.1 protocol.

This walkthrough is divided into the following parts:

  • Part 1 shows how to configure an ASP.NET Web site (Web Application and WCF Web Service) to expose the application services.

  • Part 2 shows how to build a Windows console application that accesses a user’s authentication, roles, and profile information. In this walkthrough you will build a Windows console application, but the ASP.NET application services are available to client applications that run on any operating system.

Mapping and Configuring the Application Services

Before you can access the user’s credential and profile information from the client, you must create mapping files – WCF service file (.svc) that point to the application services. This makes the services available to any application that is running on any platform that can send and receive messages in SOAP format. You must also configure the Web site so the application services are exposed on the network

Walkthrough: Using ASP.NET Application Services

Server Authentication on Desktop Application using Client Application Services

Client application services (.NET 3.5) make it easy for you to create Windows-based applications that use the ASP.NET AJAX login, roles, and profile application services included in the Microsoft ASP.NET 2.0 AJAX Extensions. These services enable multiple Web and Windows-based applications to share user information and user-management functionality from a single server. For example, you can use these services to perform the following tasks:

  • Authenticate a user. You can use the authentication service to verify a user’s identity.

  • Determine the role or roles of an authenticated user. You can use the roles service to change the user interface of your application depending on the user’s role. For example, you can provide additional features for users who are in an administrator role.

  • Store and access per-user application settings located on the server. You can use the Web settings service (also known as the profile service) to share settings across multiple applications and locations.

Client application services take advantage of the Web services extensibility model through client service providers that you can specify in your application configuration files. These service providers include offline functionality that uses a local cache for authentication, roles, and settings data when a network connection is unavailable.

Sample: Using WCF, ASP.NET Client application services to authenticate a user on a desktop windows application.

image

Accessing ASP.NET Session, Request, Response (HttpContext / HttpApplication) in WCF

 

WCF services are designed to be transport independent. Even when they are hosted inside ASP.NET applications, the decoupling of the relationship to the HTTP-flavored features is required so that users are not confused. Also most of those ASP.NET features have counterparts in WCF. Nevertheless, WCF provides a mechanism to support smooth migration from ASMX to WCF by introducing two different hosting modes for WCF services:

  • Mixed Transports Mode
  • ASP.NET compatibility mode

This is controlled by the application-level configuration flag “aspNetCompatibilityEnabled”:

<system.serviceModel>

<serviceHostingEnvironment aspNetCompatibilityEnabled=”true”/>

</system.serviceModel>

This flag is false by default.

Details from Wenlong Dong’s Blog : ASP.NET Compatibility Mode

TopXML : How can WCF support multiple IIS Binding specified per site in WCF

 

IIS supports specifying multiple IIS bindings per site, which results in multiple base addresses per scheme. A WCF service hosted under a site allows binding to only one baseAddress per scheme.

So if you get an error like this “This collection already contains an address with scheme http wcf”, you may want to look at the following:

Solution in .Net Fx 3.0:Supporting Multiple IIS Bindings Per Site

Solution in .Net Fx3.5: BaseAddressPrefixFilters

<system.serviceModel>

<serviceHostingEnvironment>

<baseAddressPrefixFilters>

        <add prefix=”http://shipping.myorg.com”/>

</baseAddressPrefixFilters>

</serviceHostingEnvironment>

</system.serviceModel>

Via TopXML : How can WCF support multiple IIS Binding specified per site in WSCF/WCF