Generate your own RSS feed with WCF: Syndication

On a recent project I needed to expose some data via an RSS feed.  I came across Derik Whittaker’s blog post Generate your own RSS feed with Argotic.

Argotic Syndication Framework is a Microsoft .NET class library framework that enables developers to easily consume and/or generate syndicated content from within their own applications.  The Argotic Syndication Framework is very powerful and easy to use.

I didn’t go with the Argotic Syndication Framework for the project because I didn’t want to have yet another assembly in my project, maybe a lame reason but I found the solution I needed already existed in the .NET Framework with WCF: Syndication.  WCF: Syndication is a set of classes available in the .NET Framework that enables the creation and consumption of RSS feed in RSS and ATOM.

I thought it would be a good exercise for me to take Derik’s original article and show how you would accomplish the same thing using WCF: Syndication, so here goes.

In this post we will review the following steps needed to add a feed to your site. image

  1. Creating the xml (rss feed) document for publication
  2. Creating an access point for the feed
  3. Adding the feed to your site 

Creating the xml (rss feed) document for publication

Using the Syndication objects available in the .NET Framework is pretty easy.  The first thing we need to do is add a reference to the assembly that holds the classes we need to add a reference to the System.ServiceModel.Web.dll.

Once we have the reference to the assembly all we have to do is add a using statement and away we go.

 1: public SyndicationFeed GenerateRssFeed()
 2: {
 3:     var items = RssRepository.GetRssItems();
 4:     var syndicationItems = new List<SyndicationItem>();
 5:  
 6:     var feed = new SyndicationFeed("Main RSS Feed for site", "Title", null);
 7:  
 8:     foreach (var rssItem in items)
 9:     {
 10:         var newItem = new SyndicationItem
 11:                           {
 12:                               Title = new TextSyndicationContent(rssItem.Title),
 13:                               Content = new TextSyndicationContent(rssItem.Content),
 14:                               Id = Guid.NewGuid().ToString()
 15:                           };
 16:         
 17:         newItem.Links.Add(new SyndicationLink(rssItem.Link));
 18:         newItem.Authors.Add(new SyndicationPerson("author@example.com", "Author Name", "http://example.com"));
 19:         
 20:         // Add the new syndication item to the generic list
 21:         syndicationItems.Add(newItem);
 22:     }
 23:  
 24:     // add the SyndicationItems to the Syndication Items
 25:     feed.Items = syndicationItems;
 26:  
 27:     // Return the Syndication feed
 28:     return feed;
 29: }

 

Creating an access point for the feed

Following along with the original post we now need to create an access point for users to be able to consume the feed.  Instead of using a static .xml document to expose the content lets look at how we can dynamically generate the feed.  The simplest way is to create an .aspx page and change the content type to “text/xml” and place the following code in the Page_Load event.

Another good idea is to add the OutputCache to the page.

 1: // Put this code here in your html part of the page. 
 2: // This will cache the output for a given number of seconds 
 3: <%@ OutputCache Duration="600" VaryByParam="none" %> 

 

Adding the feed to your site

In order to let the world know you have a feed, you need to expose it in your html documents.  One way to accomplish this is to add it to you master page, this will make it available on every page of the site.

After you have added the reference to your html page you should see the RSS feed icon on your URL bar in your browser.

 1: <link title="[Your title here]" 
 2:         href="http://www.yourdomain.com/[rssfilehere]" 
 3:         type="application/rss+xml"
 4:         rel="alternate">

 

Conclusion

First I would like to thank  Derik for his easy to follow post on using the Argotic Syndication Framework and I hope that you can also see how easy it is to also create Syndication feed using the new classes made available in WCF.

Download the full source SyndicationVsArgotic.zip (27.26 kb).

blog comments powered by Disqus

Calendar

<<  November 2017  >>
MonTueWedThuFriSatSun
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Month List