2009
08.28

In a previous post i explained briefly how you can make a simple wcf service. In this post i’ll try to explain how you can extend that simple service to provide an RSS feed.

In fact, it’s a pretty easy task. It comes down to adding a single method to your service contract, implementing the method and then using it in a webpage. This webpage will render our feed.
So that’s 3 steps and i’ll run them down for you:

Preparing your WCF service to provide the RSS feed:

[OperationContract]
[WebGet]
Rss20FeedFormatter GetRSSFeed();

This method is added to your IService (or IWhateverService). The Rss20FeedFormatter serializes a SyndicationFeed to and from RSS 2.0. That’s pretty cool.

public System.ServiceModel.Syndication.Rss20FeedFormatter GetRSSFeed()
{
SyndicationFeed feed = new SyndicationFeed("feedTitle", "feedDescription", new Uri("http://localhost:8000/RSS"));
feed.Authors.Add(new SyndicationPerson("dries@fakemail.be"));

List items = new List();

SyndicationItem newItem = new SyndicationItem();
newItem.Title = new TextSyndicationContent(String.Format("item title"));

newItem.PublishDate = DateTime.Now;
newItem.Content = new TextSyndicationContent("html content of your post", TextSyndicationContentKind.XHtml);
items.Add(newItem);
feed.Items = items;
return new Rss20FeedFormatter(feed);
}

As you can see, this is the method that does all of the work. What we do here is create “the feed”, give it a name and description and then add a list of feed items.
Obviously, in a production environment you’ll want to fill the list based on a database query or something similar.

As you can see you can give the inner content of your rss post a html string. That works great and you can put images and links in your posts this way.

After the list is filled up with all our fine content we send it to the requesting client. In this case the client will be our webpage.

Setting up the webpage that will render the RSS feed

Create a new webpage in your site project and in the Page_Load request the feed from the WCF service.

protected void Page_Load(object sender, EventArgs e)
{
Service.ServiceClient Service = new Service.ServiceClient();
try
{
Rss20FeedFormatter feed = Service.GetRSSFeed(20, SessionProvider.CustomerID, "feed title", "feed description", Request.Url.OriginalString.Replace("/rss.aspx", ""));
Service.Close();
XmlWriter feedWriter = XmlWriter.Create(Response.OutputStream);

feed.WriteTo(feedWriter);
feedWriter.Close();
}
catch (Exception)
{

throw;
}
}

When all went well you can now surf to your rss.aspx page and you’ll see your rss feed.


So that’s basically all it takes. Ofcourse, this is a brief example again of what you can do but i hope it gives you a clearer sight on how to handle things.
It’s merely meant as a basis from which you can create something more complex.

kick it on DotNetKicks.com

  • Share/Bookmark

2 comments so far

Add Your Comment
  1. how can I add an extra link for the feed?

    need to add an extra link at the top of the rss feed.

    e.g.

    Title link1

    extra link2

    ——-

    item1

    item2

    itme3

  2. Hello Kevin, you can only use the properties of SyndicationFeed to specify more information about your feed. I don’t think you can add an extra link on it.