How to Create an RSS Feed from Windows Azure Table Storage Data

May 3rd, 2012

Today I am going to talk about how you can create an RSS feed from Azure Table Storage data. Realistically, you could very easily change this code to create a feed from SQL Azure or any store for that matter (if you get stuck email me at Liam AT Cotega dot com). The code that I am going to use is based on a sample created by DeveloperZen. I am also going to use the trick I blogged about to return the top X rows from Table Storage in reverse chronological order.

The reason I did this is because I needed to be able to provide my customers with a RSS / XML feed of the most recent items logged by the Cotega service. This service logs database statistical information on a users database such as query performance, blocked queries, etc. By providing this data as an RSS feed it is easy for my customers to then embed the feed into their own monitoring systems if they do not wish to use the Cotega dashboard. I am also hoping to use this feed (or another I am creating in JSON) to allow people to build mobile apps so that they can monitor their database from a smartphone. By the way, if you are interested in helping with this, let me know.

How it Works
At the bottom of this post, I have included an MVC3 project that shows how this works. If you open the solution in Visual Studio, you will want to focus on the HomeController.cs file which is located in the /Controllers folder.

For this to work, I will use the ServiceModel Syndication namespace for RSS and the Windows Azure namespaces to connect to my storage. As such, I have added the following to the controller.

using System.Xml;
using System.ServiceModel.Syndication;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Diagnostics;

Within this controller there is one key functions and two classes. The first class defines the structure of the data that will be queries from Azure table storage and the second defines an ActionResult for the RSS feed. The Feed function is what gets called when you enter http://x.x.x.x/home/feed in your browser. You may want to set a breakpoint on this when you run the code. Some of the things that this function does is:

  • Receive an agentname and username.  These parameters are used to filter the results that come back from Azure storage
  • Create a connection string to my Windows Azure Table store called storageConnectionString . Notice you will need to replace [ACCOUNTNAME] and [ACCOUNTKEY] with your own credentials.
  • Create an EndDt that is set to todays date.  I use this as a trick to sort the results in reverse chronological order.  For more details on why I do this see this page.
  • Take the results of this query and fill up the RSS feed with the 50 most recent items from Table Storage and returns this to the browser
[HttpGet]
public ActionResult Feed(string agentName, string userName)
{
string storageConnectionString = "DefaultEndpointsProtocol=http;AccountName=[ACCOUNTNAME];AccountKey=[ACCOUNTKEY]";

//Select most recent 50 items in desceneding order of date
DateTime endDt = DateTime.Now.ToUniversalTime();

//create syndication feed
SyndicationFeed feed = new SyndicationFeed("Monitoring Agent Feed", "This is an RSS feed for monitoring agent logged data.",
new Uri("http://127.0.0.1:81/alert/feed"), "075211", DateTime.Now);

try
{
// Get the loggged data
CloudStorageAccount Account;
Account = CloudStorageAccount.Parse(storageConnectionString);

// Connect to Azure Tables
CloudTableClient TableClient = new CloudTableClient(Account.TableEndpoint.ToString(), Account.Credentials);
TableServiceContext tableServiceContext = TableClient.GetDataServiceContext();

// Use rowEndKeyToUse so that I can sort the data in reverse chronological order
string rowEndKeyToUse = string.Format("{0:D19}", DateTime.MaxValue.Ticks - endDt.Ticks);

var results = (from g in tableServiceContext.CreateQuery("logevent")
where g.PartitionKey == userName
&& g.RowKey.CompareTo(rowEndKeyToUse) > 0
&& g.EventType.CompareTo(agentName) == 0
select g).Take(50);

//add feed items
List syndicationItems = new List();

foreach (LogEvent logEvent in results)
{
var syndicationItem = new SyndicationItem(String.Format("{0}", logEvent.EventType),
String.Format("Watching: {0}
Value: {1}", logEvent.Watching, logEvent.EventValue),
new Uri("http://cotega.com"),
// You may want to replace this guid with the rowkey and partitionkey if not concerned about publishing this in the feed
System.Guid.NewGuid().ToString(),
logEvent.Timestamp);

syndicationItems.Add(syndicationItem);
}

feed.Items = syndicationItems;
}
catch (Exception ex)
{
//Log error
}

//return feed
return new RssActionResult() { Feed = feed };
}
}

public class LogEvent : TableServiceEntity
{
public string UserName { get; set; }
public string EventType { get; set; }
public int EventValue { get; set; }
public string Watching { get; set; }
public DateTime EventTime { get; set; }
}

///
 /// RssActionResult class. /// 
public class RssActionResult : ActionResult { public SyndicationFeed Feed { get; set; } public override void ExecuteResult(ControllerContext context) { context.HttpContext.Response.ContentType = "application/rss+xml"; Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed); using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output)) { rssFormatter.WriteTo(writer); } } }

That’s it. If you have any questions, let me know in the comments.
Download Sample – Azure RSS Feed

3 Responses to “How to Create an RSS Feed from Windows Azure Table Storage Data”

  1. [...] How to Create an RSS Feed from Windows Azure Table Storage Data by @liamca (posted May 3) [...]

  2. [...] How to Create an RSS Feed from Windows Azure Table Storage Data by @liamca (posted May 3) [...]

  3. [...] Cavanagh (@liamca) explained How to Create an RSS Feed from Windows Azure Table Storage Data on 5/3/2012: Today I am going to talk about how you can create an RSS feed from Azure Table Storage [...]

RSS feed for comments on this post.

Leave a Reply