WEBSITE

Produce an RSS Feed Dynamically in IIS

9/26/2010 7:48:47 PM
You already have all the pieces you need to actually create the feed: It’s merely an XML file, after all. The more important question is this: When do you generate it?

There are a few options:

  • Each time content is created or updated.

  • On a regular schedule (once an hour, for example).

  • On each request (possibly with caching).

An example of the last option is to create a custom handler for IIS.

In a class library project, define this class:

class RssGenerator : System.Web.IHttpHandler
{
RssLib.Feed feed = new RssLib.Feed();

#region IHttpHandler Members

public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(System.Web.HttpContext context)
{
context.Response.ContentType = "application/xml";
CreateFeedContent(context.Response.OutputStream);
}

#endregion

private void CreateFeedContent(Stream outStream)
{
RssLib.Channel channel = GetFeedFromDB();

feed.Write(outStream, channel);
}

private RssLib.Channel GetFeedFromDB()
{
using (IDataReader reader = CreateDataSet().CreateDataReader())
{
RssLib.Channel channel = new RssLib.Channel();
channel.Title = "Test Feed";
channel.Link = "http://localhost";
channel.Description = "A sample RSS generator";
channel.Culture = CultureInfo.CurrentCulture;
channel.Items = new List<RssLib.Item>();
while (reader.Read())
{
RssLib.Item item = new RssLib.Item();
item.Title = reader["title"] as string;
item.Link = reader["link"] as string;
item.PubDate = reader["pubDate"] as string;
item.Description = reader["description"] as string;
channel.Items.Add(item);
}
return channel;
}
}

private static DataSet CreateDataSet()
{
DataSet dataSet = new DataSet();
//get results from database and populate DataSet
return dataSet;
}
}


To use this in a web project, you must modify the web project’s web.config to reference it:

<httpHandlers>
...
<!-- type="Namespace.ClassName,AssemblyName" -->
<add verb="GET" path="feed.xml"
type="IISRssHandler.RssGenerator,IISRssHandler"/>
</httpHandlers>

Now, whenever you access the project’s feed.xml file via a web browser, the handler will intercept the request and run your code to generate the RSS.

Note

Rather than generate the results on each request, you may want to implement a simple caching system—only generate if the results haven’t been generated in the last 10 minutes or so; otherwise, use the previous results.

Other  
 
Most View
HP Photosmart 7520 e-All-In-On Printer – So Much Potential *Sniffle*
The Assemblage Of GeForce GTX 650 Ti Graphics Cards (Part 7)
Sharepoint 2013 : Creating team sites (part 1) - Customizing team sites, Working with document libraries
101 Recommended Apps (Part 13)
Xara Photo And Graphic Designer MX 2013
Innovate & Inspire - Delight, Surprise, Love, Connection (Part 3)
PowerShell for SharePoint 2013 : Word Automation Services - Configure the Conversion Processes, Configure Conversion Throughput
Parallel Programming with Microsoft Visual Studio 2010 : Introduction to LINQ (part 2) - PLINQ
WD Black 4TB - 4TB Storage Goes Mainstream
Lenovo Ideatab S2110 - A Transformer’s Strong Competitor Running Android 4.0 (Part 2)
Top 10
Sharepoint 2013 : Farm Management - Disable a Timer Job,Start a Timer Job, Set the Schedule for a Timer Job
Sharepoint 2013 : Farm Management - Display Available Timer Jobs on the Farm, Get a Specific Timer Job, Enable a Timer Job
Sharepoint 2013 : Farm Management - Review Workflow Configuration Settings,Modify Workflow Configuration Settings
Sharepoint 2013 : Farm Management - Review SharePoint Designer Settings, Configure SharePoint Designer Settings
Sharepoint 2013 : Farm Management - Remove a Managed Path, Merge Log Files, End the Current Log File
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered