Click here to Skip to main content
15,868,069 members
Articles / Programming Languages / C#
Article

Publishing RSS and ATOM Feeds using WCF 3.5 Syndication Libraries

Rate me:
Please Sign up or sign in to vote.
4.72/5 (17 votes)
5 Feb 2008CPOL5 min read 108.3K   1.1K   54   7
This article focuses on using the WCF 3.5 libraries namely System.ServiceModel.Syndication namespace to create and publish an RSS and Atom feed from the same code base.
Download MyNewsSyndicator.zip - 14.2 KB

Introduction

Windows Communication Foundation with its 3.5 release provides several new and useful features including capability to publish and consume syndication feeds in a much easier and uniform way, right out of the box. This article focuses on using the WCF 3.5 libraries namely System.ServiceModel.Syndication namespace to create and publish an RSS and Atom feed from the same code base.

Background

Syndication feeds are data formats to provide users with contents which updates frequently without them having to visit the corresponding websites individually. Atom and RSS are two popular syndication formats. This feature helps readers to keep up to date with their favorite websites in an automated fashion instead of checking each of them manually. This method of syndication is quite popular with blogs, news headlines and podcast to name a few content providers.<o:p>

RSS (Really Simple Syndication) is a popular web feed format containing either the summary or full text of associated contents. Another format for web feeds is Atom which differentiates itself in terms of content model, date Format, internationalization and modularity from RSS. Details of these formats and their comparison are beyond the scope of this writing but it can be found on the links citied in the reference section.<o:p>

As we’ve established above, RSS and Atom are suitable for more content oriented entities and therefore it raises an important question of why do we need web feed support in a connected application development framework like WCF? The answer lies in one word, REST (Representational State Transfer). Briefly speaking, by introducing the webHttpBinding and UriTemplates, Microsoft has depicted their interest and paradigm shift towards the Web 2.0 model of REST style services. Asp.NET MVC framework is also closely tied with the REST style development. The Syndication support is among the core component supporting the big plan which constitutes of

  • Web feeds get parameterized and treated as a service.
  • The request and response over HTTP-GET is based on standard web feeds format, utilized by a wide variety of aggregators.
  • Underlying WCF libraries provide built in support for UriTemplate parsing and hence leveraging the “hole in the Uri” pattern over HTTP-GET for service invocation.
  • WCF 3.5 provides built in syndication support for standardized web feed format responses hence supporting parameterized Uri over multiple end points.<o:p>

An example which follows shortly will clarify the above mention points.

System.ServiceModel.Syndication Namespace

System.ServiceModel is the new namespace provided with WCF 3.0 and now with WCF 3.5, Microsoft has added few more namespaces to it and ServiceModel.Syndication namespace is one of them.
<o:p>

Some of the prominent classes of System.ServiceModel.Syndication are as follows.

<o:p>

Class<o:p>

Description<o:p>

Atom10FeedFormatter

(De)Serializes a SyndicationFeed instance in Atom 1.0 format.

Rss20FeedFormatter

(De)Serializes a SyndicationFeed instance in RSS 2.0 format.

SyndicationContent

Base class for representation of syndication content.

SyndicationFeed

Represents a top-level feed object both in Atom 1.0 and RSS 2.0.

SyndicationFeedFormatter

Abstract class serving as base class for formatters (Atom10FeedFormatter, Rss20FeedFormatter).

SyndicationItem

Represents a feed item, for example an RSS or an Atom.

TextSyndicationContent

Represents any SyndicationItem content intended to be displayed to an end user.

UrlSyndicationContent

Represents syndication content that consists of a URL to another resource.

XmlSyndicationContent

Represents XML syndication content which is not intended to be displayed in a browser.

<o:p>

The Implementation

After looking at the associated classes defined above, it’s quite clear that System.ServiceModel.Syndication provides a framework to perform the syndication leg-work for us including serialization and deserialization of web feeds. Let’s see how it works step by step.

<o:p>

  1. Create a simple Console application in Visual Studio.NET 2008. Make sure you add the Reference to System.ServiceModel and System.ServiceModel.Web namespaces in your project as shown in the screenshot below.

    AddReferenceDialog.JPG

  2. Create an interface for the news feed. The attribute WebGet facilitates the UriTemplate functionality.

<o:p>

public interface INewsFeed
{
     [OperationContract]
     [WebGet(UriTemplate = "GetNews?format={format}")]
     SyndicationFeedFormatter GetNews(string format);
 }

As you can see here, instead of doing programmatic parsing, the Uri format is being specified as a Template in the attribute and will be passed to the method when invoked from the Uri. Therefore when the user invokes a webmethod using the following Uri
<o:p>

http://localhost:8000/NewsFeedService/GetNews?format=rss

the format gets passed to the format parameter in the GetNews web method.

<o:p>

3. Now let’s setup the Feed Formatter. This is an important part of the development exercise here. If you are building a blog engine and want to expose RSS or Atom feed, your job just got a whole lot easier with the Syndication classes.

SyndicationFeed feed = new SyndicationFeed("Technical
News Feed", "Technical News Feed", new Uri("http://WcfInsiders.com"));            
feed.Authors.Add(new SyndicationPerson("<st1:personname w:st=""</span">"</span">adnanmasood@gmail.com</st1:personname>"));           
feed.Categories.Add(new SyndicationCategory("Technical News"));           
feed.Description = new TextSyndicationContent("Technical News demo for RSS and ATOM publishing via WCF"); 

4. This is the step for adding the contents where we specify the SyndicationItems. The method supports several overloads and one of them is used here.

SyndicationItemOverload.JPG
<o:p>

SyndicationItem item1 = new SyndicationItem( "Overhaul of net addresses begins",                
"The first big steps on the road to overhauling the net's core addressing system have been taken. On Monday the master address books for the
net are being updated to include records prepared in a new format known as IP version 6...",               
new Uri("http://news.bbc.co.uk"),               
System.Guid.NewGuid ().ToString(),           
DateTime.Now);

<o:p>

<o:p> Now that the syndication items have been created, let’s add the items to the generic list of Syndication items.<o:p>

List<SyndicationItem> items = new List<SyndicationItem>();
items.Add(item1);  

<o:p>

6. Last but not the least, service the items based on their required format i.e. either RSS or Atom.
if (format == "rss")
     return new Rss20FeedFormatter(feed);
 else if (format == "atom")
     return new Atom10FeedFormatter(feed);

7. Another key step is required to actually Host the feed. What we did above was all about creating the feed and now we need to actually host it. Like any other service, hosting would require us to provide an end point Uri.<o:p>

Uri address = new Uri("http://localhost:8000/NewsFeedService/");

<o:p>

and a corresponding service host
WebServiceHost svcHost = new WebServiceHost(typeof(NewsFeedService), address); <o:p> 
</o:p>

<o:p> The next step is to open the service host
<o:p>

svcHost.Open();  

and load the corresponding readers. This is done in two steps and is the core of SyndicationFeed process<o:p>

First expose the Uri for the GetNews webmethod via the XMLReader.Create

<o:p>

XmlReader atomReader = XmlReader.Create("http://localhost:8000/NewsFeedService/GetNews?format=atom");

And then load the corresponding XmlReader instance to a syndication feed.<o:p>

SyndicationFeed atomFeed = SyndicationFeed.Load(atomReader);

Repeat the same process with Rss feed and voila, you have your Atom and RSS feeds available which can later be used by any RSS aggregator (reader).

NewsSyndicatorConsole.JPG

Figure: The News Syndication Application

AddingTheFeed.JPG
<o:p>

Figure: Adding the Atom feed to SharpReader

DisplayingTheFeeds.JPG<o:p>

Figure: Displaying both the RSS and Atom Feeds in the SharpReader RSS Aggregator.

The complete source code can be downloaded from the links provided by the article you can use to test the complete functionality out for yourself.<o:p>
<o:p>

Conclusion:

Providing the syndication framework with WCF is a big step towards empowerment of UriTemplate style distributed communication frameworks based on standard web data communication. If you are writing a simple blogging engine or a complex enterprise service bus which exposes its events via publish/subscribe event style communication, these set of libraries provide great value.

References:

<o:p>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Green Dot Corporation
United States United States
Adnan Masood works as a Sr. Software Engineer / Technical Lead in a Monrovia based financial institution where he develops middle tier architectures, distributed systems and web applications using Microsoft .NET framework. He holds various professional memberships (ACM, BCS, and ACS) and several technical certifications including MCSD.NET, MCAD.NET and SCJP-II. Adnan is attributed and published in print media and on the web and holds a Masters Degree in Computer Science from Nova Southeastern University, FL and is currently pursuing his doctoral studies in Machine Learning. Adnan has taught WCF courses at University of California at San Diego and regularly present at local code camps. He is actively involved in the .NET community as co-founder and president of San Gabriel Valley .NET Developers group. His blog can be found at www.AdnanMasood.com and he can be reached via email at adnanmasood at acm.org

Comments and Discussions

 
Generalmy vote 5 Pin
Amey K Bhatkar24-Aug-13 8:45
Amey K Bhatkar24-Aug-13 8:45 
Question<pre lang="sql">Consuming RSS in Asp.Mvc 3 using C# and Syndication class Pin
Maksym Protsenko1-Jul-13 1:58
Maksym Protsenko1-Jul-13 1:58 
GeneralMy vote of 5 Pin
DarachRoots11-Apr-13 15:04
DarachRoots11-Apr-13 15:04 
QuestionHow do you host this in IIS Pin
badgers away17-Nov-08 5:10
badgers away17-Nov-08 5:10 
AnswerRe: How do you host this in IIS Pin
smitsc23-Dec-08 16:49
smitsc23-Dec-08 16:49 
AnswerRe: How do you host this in IIS Pin
Yooakim19-Feb-09 21:53
Yooakim19-Feb-09 21:53 
QuestionWhat to do on restful endpoints with an invalid parameter Pin
StrawberryFrog10-Jul-08 1:40
StrawberryFrog10-Jul-08 1:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.