Wednesday, March 18, 2009

Writing RSS Feeds in .net

This article will cover following topics

• About RSS feeds
• Various ways to generate RSS Feed
o Using HTTP handlers and Cache mechanism.
o Building an HTTP Handler
• RSS Library
• RSS XML Format
• Advantages
• Limitation and support
 About RSS Feeds:
RSS stands for really simple syndication. It’s one of the medium to deliver information to users’ offline directly in their mailbox/ offline RSS reader application on subscription. It mostly consist the frequently updated data in a website such as blog entries, web feeds, news & alerts, audio & video links etc. RSS feeds are updated on defined frequency depends on the new contents available on the site. It might be hourly, weekly, monthly etc. The users subscribe to the feed on which they like to get timely updates or to aggregate feeds from many sources to one application


An RSS document includes full text or summary based on content plus it’s related metadata such as date of publishing, author, web links etc. it can aggregated using an RSS reader application sometimes called aggregator also. A user provides a link to RSS document and thereafter RSS reader application checks for timely update on each feed subscribed. The RSS icon ("
 ") is very much popular among many web2.0 based websites.

RSS formats are specified using XML, a generic specification for the creation of data formats

Various ways to generate RSS Feed:
RSS Feeds can be generated in following three ways.

• Using HTTP handlers and Cache mechanism.
• Using a scheduler (Window services).
• Using default RSS provided by the MOSS Lists.

1.1 HTTP handlers and Cache mechanism:
1.1.1 When user clicks on the RSS icon attached to the web page/data list, the client browser or RSS reader requests an RSS document from the site.
1.1.2 IIS checks the .rss file extension and invokes .NET library to process it. It will be configured in IIS. The details steps are discussed later in the document
1.1.3 Based on information in the Web.Config file, .NET knows to invoke RSS library class (HTTP handler) to process the request.
1.1.4 RSS library class checks for a cached copy of the RSS document. If it exists, RSS library sends the document back to the browser and terminates. If the response has not been cached (or if it expired), then RSS library continues.
1.1.5 RSS library merges information from the datatable/list/xml with item data from the appropriate list to build the RSS document.
1.1.6 RSS library caches the RSS document and sends it back to the browser.

1.2 Building an HTTP Handler

Whenever a page request comes to IIS it checks the extension of the page requested which is in the Web server configuration that which file to serve on that particular request, since IIS initially handles the request.

An HTTP handler can be configured so that the browser request can ask for a file that doesn't physically exist under the Web site. For example, you won't find a file under the Web root called "example.rss." Instead, IIS sends all requests for .rss files to the .net assembly which is directed from the web configuration file, and .net assembly writes the response from memory.

An http handler be configured in the following way


• Open ISS, Click start, run and enter inetmgr
• Click on configuration button on home directory tab, Application configuration dialog popup
• click on add button in mapping tab, Add/edit application extension mapping dialog open up
• Select the path of the DLL in executable text box and set the extension to .rss
• Close all the dialog boxes and now IIS is ready to serve any request that comes from .rss extension
To configure handler to a file extension through .NET, add a section to the Web.Config file in the root folder of Web application. Here's an example that shows how to associat RSS library with rss file requests .

The httpHandlers "add" tag tells .NET to send all rss file requests (verb="*" path="*.rss") to the RssHandler class in the RSS library assembly (type="Forum3.RSS library.RssHandler, RSS library").




Fig: Sample Web.Config

An HTTP handler is nothing more than a simple class library with at least one class that implements the IHttpHandler interface. The interface has just two members
• ProcessRequest method
• IsReusable property.
IsReusable tells .NET whether or not instances of your class can be safely pooled and reused. RssHandler returns true because it doesn't maintain any state that would interfere with its reusability.

The ProcessRequest method is where all the fun stuff happens. The .NET worker process passes you a reference to the request's HTTP context, which gives you access to the Request and Response objects as well as the other standard elements of the context.

2. RSS Library:

RSS library (RSS library) is .net class file which defines all the methods related to fetch/search and cache the resulted output to RSS format. it can be use any data source to find the result as databases/ xml/MOSS list etc. RSS profile contains all the settings related to the particular RSS that has to served like connection setting, Web link to RSS, title of feed etc.

FIG: Format of well defined RSS
3. RSS Format:

RSS format contain the data according to RSS 2.0 specification.
Here each item depicts a new feed in the document with its metadata. The properties of an RSS feed is classified under channel node. It can title of RSS feed, short description, web link, publish date etc.
Channel node contains many item nodes which translate into specific information available for the user in RSS feed. Each item node contains similar properties like title, description, web link, date of publish etc



References: www.msdn.microsoft.com