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>
<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>
- 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.
- 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.
<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).
Figure: The News Syndication Application
<o:p>
Figure: Adding the Atom feed to SharpReader
<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>