Having the Result Set of a Stored Proc Sent to You by RSS Feed.
by JBrooks 14. 十二月 2010 12:44
I wanted to monitor one of my system from my desk top and from my phone. I found a simple solution whereby I can subscribe to the result set of a stored proc by using RSS. So I can have this feed MS Outlook, my phone or my web browser.
First, Visual Studio 2010 makes creating an RSS feed a simple matter that is about 1 page of code.
I simply add an ASPX page to my project and remove most of the markup so it only has 2 lines:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="rss.aspx.cs" Inherits="RSS.rss" %>
<%@ OutputCache Duration="60" VaryByParam="none" %>
Next the code behind simply calls the stored proc placing the results into a table and then loading up some of the RSS related collections VS2010 gives you.
using System;
using System.Data;
using System.ServiceModel.Syndication;
using System.Web;
using System.Collections.Generic;
using System.Xml;
namespace RSS
{
public partial class rss : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"];
// I don‘t want just anyone to subscribe, so you have to know the GUID.
if (id== null || id != "23F14EA1-1B20-443B-9B94-92C4EA4A8099")
throw new Exception("Guid not reconized");
Response.ContentType = "application/atom+xml";
// this gets the data from the database and populates a table.
DataTable dt = cDB.getFeed();
SyndicationFeed myFeed = new SyndicationFeed();
myFeed.Title = TextSyndicationContent.CreatePlaintextContent("SampleApp Activity");
myFeed.Description = TextSyndicationContent
.CreatePlaintextContent(@"A syndication of the most recently
SampleApp activity including exceptions.");
myFeed.Links.Add(SyndicationLink.CreateAlternateLink(
new Uri(GetFullyQualifiedUrl("/rss.aspx"))));
myFeed.Links.Add(SyndicationLink.CreateSelfLink(
new Uri(GetFullyQualifiedUrl(Request.RawUrl))));
myFeed.Copyright = TextSyndicationContent
.CreatePlaintextContent("Copyright SampleApp");
myFeed.Language = "en-us";
List<SyndicationItem> feedItems = new List<SyndicationItem>();
foreach (DataRow dr in dt.Rows)
{
SyndicationItem item = new SyndicationItem();
item.Title = TextSyndicationContent.CreatePlaintextContent(dr["title"].ToString());
SyndicationPerson authInfo = new SyndicationPerson();
authInfo.Email = "[email protected]";
item.Authors.Add(authInfo);
// RSS feeds can only have one author.
// The stored proc returns different categories of data that I am interested in.
switch (dr["category"].ToString())
{
case "WindFarms":
case "WindFarms ":
item.Links.Add(SyndicationLink.CreateAlternateLink(
new Uri(GetFullyQualifiedUrl("/WindFarms.aspx"))));
authInfo.Name = "SampleApp WindFarm";
break;
case "Exceptions":
item.Links.Add(SyndicationLink.CreateAlternateLink(
new Uri(GetFullyQualifiedUrl("/ErrorLog.aspx"))));
authInfo.Name = "SampleApp Exception";
break;
default:
authInfo.Name = "SampleApp";
break;
}
item.Summary = TextSyndicationContent.CreatePlaintextContent(
dr["summary"].ToString());
item.Categories.Add(new SyndicationCategory(dr["category"].ToString()));
item.PublishDate = DateTime.Parse(dr["pubdate"].ToString());
item.LastUpdatedTime = item.PublishDate;
item.Id = item.PublishDate.ToString();
// Add the item to the feed
feedItems.Add(item);
}
myFeed.Items = feedItems;
XmlWriter feedWriter = XmlWriter.Create(Response.OutputStream);
// Use Atom 1.0
Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(myFeed);
atomFormatter.WriteTo(feedWriter);
feedWriter.Close();
}
private string GetFullyQualifiedUrl(string s)
{
Uri u = new Uri(HttpContext.Current.Request.Url, s);
return u.ToString();
}
}
}
To have this feed my Outlook RSS folder I just need to right click “RSS Feeds” and select “Add a New RSS Feed…”.
Then enter the URL of my RSS feed. Don’t forget to add the GUID at the end with ?id=23F14EA1-1B20-443B-9B94-92C4EA4A8099
If you site uses authentication in your site you will have to turn it off for the rss.aspx page. To do this you would add an entry in your web config file:
<location path="rss.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
You should now have a folder in Outlook that will get populated by the feed.