This is the fourth in a series of posts featuring the WCF Facility:
Today I’m going to show how easy it is to leverage the new atom support in that was introduced with 3.5 sp1 to provide feeds supplied by Windsor components.
Download the Sample Solution here:
Let’s jump right in to the code. I’m going to use a blog example because it’s obviously a natural fit for atom, but you can use it to publish anything you want. Let’s say we have a Post class:
That’s pretty much what you’d expect. Now say we have an IBlogService that allows us to add and retrieve Posts:
Now let’s create a service contract for our atom feed:
The WebGet attribute was introduced with WCF 3.5 to make it easy to create RESTful web services and uses a URI templating scheme to route URLs to service methods. Here we are routing the root URL to our GetPosts method. Note that the GetPosts method returns Atom10FeedFormatter. This class knows how to format your items as an atom feed.
So far this is all straight from WCF, we haven’t brought the WCF Facility to the party yet. To tell the WCF Facility to supply the service instance, we need to create a specific AtomFeed.svc file. My one looks like this
We tell WCF to use the WCF Facility’s host factory with its RestServiceModel. The bizarre square bracket syntax is simply the CLR notation for generics. The service is specified as ‘atomFeed’, this tells the WCF Facility to resolve the component named ‘atomFeed’ from the Windsor container.
Next we have the Windsor configuration:
Note that we have a component named ‘atomFeed’ that is implemented by DefaultAtomFeed. The container will create an instance of DefaultAtomFeed to service the request.
Note that it has dependencies on both the IBlogService, that we saw above, and IPostAtomFeedMapper. IPostAtomFeedMapper is responsible for mapping the array of Post instances returned from IBlogService to a SyndicationFeed instance. SyndicationFeed is another WCF type to support feeds and is consumed by the Atom10FeedFormatter. Here is the IPostAtomFeedMapper implementation:
And it’s as simple as that. No need for any Web.config configuration. When I run the service and browse to its URL with Firefox I see the familiar atom feed page: