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).

Fluent NHibernate LiveMeeting Chat

Last night I sat in on a LiveMeeting session hosted by Chad Myers and Zach Young, both Chad and Zach have both been working with others on the Fluent NHibernate project.  This was an awesome discussion and Chad did a great job demonstrating NHibernate and how Fluent NHibernate is trying to fill the gap for programmatically create the NHibernate configuration instead of creating the XML files.

As Chad explained Fluent NHibernate is an API for creating NHibernate mappings programmatically instead of using XML configuration files. Its goal is to reduce the difficulties faced when incorporating NHibernate in a project by providing improved readability, testing capabilities, and compile time safety.

I have just started trying to learn NHibernate and after working through a few projects definitely can see how a lot of time is creating the XML configuration and mapping files.  After viewing what Fluent NHibernate I plan on working through a few of the applications and convert them to see how well it works.

Related post by Chad Myers:
     Fluent NHibernate – Configuration
Related post by Zach Young:
     Does the Fluent NHibernate create static XML mapping files? 

The NHibernate Project
The Fluent NHibernate Project 

James Gregory also has a great article Introducing the Fluent NHibernate

.NET Framework 3.5 SP1 and Visual Studio 2008 SP1 Released

Microsoft has released the .NET Framework 3.5 SP1 and Visual Studio 2008 SP1 this week to Microsoft Downloads only a few days after releasing SQL Server 2008.

Two of the main features added in the .NET Framework service is pack is the ADO.NET Data Services and the ADO.NET Entity Framework. Visual Studio 2008 Service Pack 1 comes with a better WPF designer, a rich entity designer and full compatibility with SQL Server 2008 that was released a few days ago.

Thanks to by buddy John Oswalt for keeping me in the loop.

Free Foundations of Programming E-book by Karl Sequin

How important are foundations?  Well, have you ever spent any time around a construction site?  You should sometime,  it is amazing the amount of work that goes into just getting the foundation setup.  They move or level ground, bring in support, cut rebar, dig out where the supports needs to be.  A lot of planning and thought, not to mention sweat goes into preparing the foundation to support the building or home that it will eventually hold.

Every developer should strive to have a foundation that is as thought out, sturdy, and planned as that and this E-book from Karl will developers build that foundation.

Karl Sequin’s blog series on the Foundations of Programming has been one of my favorites.  The insight and practicality of what he teaches has touched me and my coding habits in an exponential way.  I downloaded and printed the a PDF that Tim Barcz created that included everything up to Part 7 – Active Record.  I have carried that copy with me everywhere.  Oh yes, I have printed this one and replaced my worn short version and am prepared for to read it through again. 

Download it from http://codebetter.com/blogs/karlseguin/archive/2008/06/24/foundations-of-programming-ebook.aspx.

Try Visual Studio 2008 on a Pre-Installed Virtual PC

If you haven't made the jump to Visual Studio 2008 cause you don't have the bits, give a virtual PC a test drive.  Microsoft is offering pre built virtual PC with several Visual Studio configurations.  These are a perfect way to get a look at Visual Studio 2008 and not have to install it on your local machine.  This can also be great for creating presentations. 

Check out the full listing of Virtual PC Microsoft has made available pre installed with Visual Studio 2008 here: http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx These Virtual PC images will work until April 1, 2008, so that should give you plenty of time to play around.

Calendar

<<  April 2024  >>
MonTueWedThuFriSatSun
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

View posts in large calendar

Widget Category list not found.

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))X

Tag cloud

    Widget Month List not found.

    Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))X

    Widget AuthorList not found.

    Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))X

    Widget TextBox not found.

    Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))X