RSS Syndication from a REST Service with WCF

In a previous post I constructed a simple REST Service with WCF. In this snippet I add a new uri that will produce a RSS 2.0 feed with Todo items. Add a using statement to System.ServiceModel.Syndication. Let’s append another operation to the webservice interface. The ServiceKnownType attribute declares the derived type for serializing, in this case the Rss20FeedFormatter.

[ServiceKnownType(typeof (Rss20FeedFormatter))]
[WebGet(UriTemplate = "/feed")]
[OperationContract]
SyndicationFeedFormatter GetRssFeed();

The implementation of the Service is pretty straightforward. Create a new SyndicationFeed and fill the feed properties and add some Todo items (using the TextSyndicationContent for escaped text content). Use the Rss20FeedFormatter to format the feed to valid RSS 2.0. Et voila.

public SyndicationFeedFormatter GetRssFeed()
{
    var feed =
        new SyndicationFeed
            {
                Title = new TextSyndicationContent(
                    "Todo items"),
                Description = new TextSyndicationContent(
                    "Al list of todo items"),
                LastUpdatedTime = new DateTimeOffset(
                    DateTime.Now),
                Items = from todoItem in _todoItems
                        select
                            new SyndicationItem
                                {
                                    Title = new TextSyndicationContent(
                                        todoItem.Title),
                                    PublishDate = new DateTimeOffset(
                                        todoItem.DateAdded)
                                }
            };

    var formatter = new Rss20FeedFormatter(feed);

    return formatter;
}

Feed RSS Formatter Browser

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>