Talking Points: WCF and RESTful Services (Part I)
Today, March 09, 2009, 13 hours ago | Public Sector DPE Team
An expanded Web Programming Model is now available in Windows Communication Foundation (WCF) 3.5. This Web Programming Model allows developers to build Services using a RESTful architecture. Services built using this new architecture approach is growing significantly. This Talking Point will cover the basics of REST versus SOAP/WS-*, and how to build Services using WCF 3.5 based on REST. We also cover Web feeds (RSS and/or ATOM) for Services to enable easy access to enterprise data (see ADO.NET Data Services to see this technology applied to Data Services).
REST
REST is an architectural style that derives from the client-server architectural style. The basic premise of REST is to build client-server applications using the same principles that the World Wide Web is built on. The main concept behind REST as an architectural style is to follow the best practices of the web.
The constraints of REST are based on the idea that clients interact with resources (representations of data that we want to work with). Each resource is identified by a unique URI. We interact with resources by addressing the URI and using a uniform interface to tell the service what we want to do with a resources. Other constraints are that services should be stateless (to increase scalability), and that resources should have links to other resources and those links can drive the state of the application.
The basic premise of REST is to build client-server applications using the same principles that the World Wide Web is built on. (HTTP Methods):
- GET is used to get a resource.
- POST is used to create a resource, and is considered “un-safe” – it will likely have side effects.
- PUT is used to update a resource, and
- DELETE is used to delete a resource.
- (others)
Microsoft has adopted REST as a first class approach to Web Programming, examples of that are:
- Microsoft Live Services
- ADO.NET Data Services
- SQL Server Data Services
- Cloud (Azure, .NET Services)
The reasons and benefits are many but mainly:
- It fits well when building wide open systems when you don’t control or even know who the clients are going to be). The wide interoperability and caching capabilities are key. The emphasis on a stateless service implementation enabling scalability through web farming.
- There isn’t a new interface to be learned for each service. The uniform interface allows a developer to concentrate on the URI and the resource representation.
Building a RESTful Service involved 4 high-level steps:
- Determine what your resources are going to be (Orders, Customers, etc.) and determine what content-type(s) will represent those resources.
- Design your URI structure. For example, your collection of orders could be /orders, and each individual order could be /orders/{orderid}.
- For each resource, determine what part of the HTTP uniform interface to implement.
- You also need to determine how your resources link to each other (Hyperlinking).
WCF
WCF is a core building block within out platform for building systems that communicate over networks. It is built as an open framework that supports many styles of applications, like client-server, message oriented, or service oriented, etc. It abstracts away from the developer all the complexity of networked systems, so developers can focus on the business logic.
Although the underlying message model is based on the SOAP message format, with a Header and a Body, building RESTful services as possible with WCF even before .NET 3.5 (with considerably more plumbing code), but it is a testament of WCF’s truly open and extensible design.
With 3.5, WCF added both infrastructure support (bindings, etc.) as well as a programming model.
In ServicePack1 the team added:
- Support for AtomPub’s ServiceDocument and CategoriesDocument
- Support for characters inside of path segments (/{lat};{long} )and file extensions (/{name}.json)
- and more…
This make building RESTful endpoints with WCF very straightforward, even if you don’t know a lot about REST.
AJAX
AJAX (Asynchronous JavaScript and XML), is a very popular way to build rich web applications. Although conceived as XML-based, most AJAX applications today use JSON (JavaScript Object Notation).
JSON has a smaller serialization footprint and saves bandwidth and processing. It also has a natural integration with JavaScript (through the eval function), and so the programming experience in JavaScript is much nicer.
On top of the base functionality WCF has additional support for building AJAX-based applications. First, there is a serializer that will take .NET objects and turn them into JSON, or take JSON objects and turn them into .NET objects. Also, if you are using ASP.NET AJAX as the client programming model, there is a JavaScript “proxy” which is exposed from the endpoint that enables you to use the service endpoint with very little effort, and has the added benefit of integrating with ASP.NET’s IntelliSense in Visual Studio.
WEB FEEDS and WCF
Feeds are a common way of exposing data on the web, particularly to automate posting machine-readable data for blogs and news. Organizations are finding feeds to be a very useful way to also expose various types of enterprise data. For instance, almost all of the data in a SharePoint portal can be exposed as a feed.
Syndication, the act of exposing data using web feeds relies in two typical formats: RSS and Atom. RSS is the older standard, and Atom is quickly become the more dominant (although many sites/endpoints expose both formats) because Atom has a richer model for non-HTML data as well as linking.
The support built into WCF for feeds is built on top of the base functionality we mentioned above.
The basic idea behind the WCF support for feeds it to abstract away from the developer the need to know the various feed formats (in this version of WCF that’s RSS 2.0 and Atom 1.0). This is done by separating the data from the serialization model.
Developers can simply take data and push it into the WCF OM (Object Model). Then take the WCF OM and pass it to the formatter. The formatter takes care of turning the WCF OM (which is layered on top of the data) into the correct feed format.
The WCF OM is closer to the Atom model, since the Atom model is as we said before, a little richer in terms of its data.
Finally, the WCF object model from WCF relies in two core objects: SyndicationFeed and SyndicationItem. The SyndicationFeed represents the whole feed, and each SyndicationItem represents an entry. SyndicationFeed has an Items property which holds onto the individual items.
In Part II we will explore:
- WCF Dispatching
- AtomPub
- Tooling support
- Security
- Processing
- Transactions
- Purity (Purists vs Pragmatics)
Conclusion
REST as an architectural style encourages interoperable, scalable web services, while WCF 3.5 provides the support for building services using this architectural style. Building RESTful services in .NET is an elegant integration of simplicity and power. We leverage the simplicity of the fundamental web protocols with the power of our framework and development tools.
References
- Check Soap & RESTful Services on Channel9
- MSDN REST Resources
- Windows Communication Foundation REST Starter Kit
Joel Reyes
No comments:
Post a Comment