Partager via


RSS Feeds with ADO.NET Entity Framework and the ASP.NET RSS Toolkit

 

The ASP.NET team has released the RSS toolkit beta on sandbox.asp.net. This led me to want to tinker with exposing some RSS feeds using ADO.NET Entity Framework to retrieve the data. I pulled the toolkit and within 10 minutes had my first couple of feeds going.

 

Pretty simple, yet cool stuff. The RSS toolkit essentially provides a specialized HttpHandler that one then derives from to expose the RSS functionality.

 

I created a new WAP project and added an EDM model on top of the Northwind Database.

From their I added a new HttpHandler class and changed the base class to be the GenericRssHttpHandlerBase class from the RSS Toolkit.

At this point it was merely a matter of retrieving the data that I wanted to return and setting it accordingly.

 

The only thing one need do to use the RSS Toolkit is to provide an override of the PopulateChannel method.

In this case my override uses the EntityFramework to retrieve all products from the database.

 

    /// <summary>

    /// Summary description for $codebehindclassname$

    /// </summary>

    [WebService(Namespace = "https://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    public class NorthwindProducts : GenericRssHttpHandlerBase

    {

        protected override void PopulateChannel(string channelName, string userName)

        {

            Channel["title"] = "Products";

            using (EntityNorthwindContext context = new EntityNorthwindContext())

            {

                foreach(Product product in context.Products.Execute(MergeOption.NoTracking))

                {

                    GenericRssElement item;

                    item = new GenericRssElement();

                    item["title"] = product.ProductName;

                    item["description"] = BuildProductDescription(product);

                    Channel.Items.Add(item);

                }

            }

        }

        protected String BuildProductDescription(Product product)

        {

            StringBuilder bldr = new StringBuilder();

            bldr.AppendLine(String.Format("ProductID={0};", product.ProductID));

            bldr.AppendLine(String.Format("ProductName={0};", product.ProductName));

            bldr.AppendLine(String.Format("QuantityPerUnit={0};", product.QuantityPerUnit));

            bldr.AppendLine(String.Format("ReorderLevel={0};", product.ReorderLevel));

            bldr.AppendLine(String.Format("UnitPrice={0};", product.UnitPrice));

            bldr.AppendLine(String.Format("UnitsInStock={0};", product.UnitsInStock));

            bldr.AppendLine(String.Format("UnitsOnOrder={0};", product.UnitsOnOrder));

            return bldr.ToString();

        }

    }

 

 

From there one can just hit the page to get back an RSS representation of the products in the Northwind database.

 

Tim Mallalieu

Program Manager, ADO.NET

Comments