IgorShare Thoughts and Ideas

Consulting and Training

RSS is not only for the news

Posted by Igor Moochnick on 12/23/2007

The new Syndication libraries, provided in the .NET 3.5, making creation of the RSS feeds a walk in a park. By combining them with the power of the WCF you can get an amazing results.
RSS technology allows you to reverse the client/server tandem from push to pull paradigm. Instead of client sending data to server you can publish the available data and make server to decide what to pull(download) and what to ignore, This can save a lot of valuable resources like time, network bandwidth, data transfer and processing.

To make my case more interesting and appealing let’s imagine a situation where you want to make a back-up agent that will be archiving all the text files. In order to do so, all the desktops will publish all the newly changed files that need to be archived. The server will subscribe to all the relevant feeds. In this article I’ll cover, for starters, only the client’s side and in the following articles we’ll talk about different discovery and subscription mechanisms that we have in our disposal in .NET for the server.

The following snippet shows you how to create s simple syndication feed:

SyndicationFeed feed = new SyndicationFeed();
List items = new List();
…
SyndicationItem item = new SyndicationItem(title, path, GetFileUri(path));
items.Add(item);
feed.Items = items;

As you can see it’s extremely easy, but this was just a half of the job. The other half is to publish the feed. There are a lot of ways to do it, but I’m going to cover one of them. Since WCF is one of the best things to use in windows services (as well as in Web services), I’m going to use it to publish the feed. Here how you will initialize the WCF host with the Feed Service as a singleton (single-instance):

FileChangeFeedService service = new FileChangeFeedService();
ServiceHost serviceHost = new ServiceHost(service);
serviceHost.Open();

Note: don’t forget to mark the service class as a single-instance service by adding ServiceBehavior attribute –

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
IncludeExceptionDetailInFaults = true)]

Announce: I’ll be talking about REST in the following posts as well as a new “Give it a REST” presentation is ready for broader public to attend.One of the beautiful things that became available with .NET 3.5 is Url templates. This allows to put HTTP verbs on the methods of a web service as well as to map the Url parts (not only parameters) to a method parameters. This in many cases will eliminate any need for parsing of the data. All the mentioned above, make the WCF Web Services REST compatible.For example:

[OperationContract]
[WebGet(UriTemplate = “?format={format}”)]
SyndicationFeedFormatter Feed(string format);

This contract method will be called if a web request will come to the following URL:http://localhost/feed?format=rssThe format parameter (“rss” in our case) will be extracted from the URL and passed as a parameter to the Feed method. This allows us to seamlessly provide different feed formatting (thanks to the new WCF libraries):

public SyndicationFeedFormatter Feed(string format)
{
if ((format != null) && (format.ToLower() == “rss”))
return new Rss20FeedFormatter(feed);       

return new Atom10FeedFormatter(feed);
}

Note: if no parameters will be provided (i.e.: http://localhost/feed) the Feed method will be called with a NULL value.And here is what will glue everything together – the config file:

<configuration>
  <system.serviceModel>
    <services>
      <service name="FileChangeFeed.FileChangeFeedService">
        <endpoint address="http://localhost:8080/feed"
           binding="webHttpBinding"
           bindingConfiguration=""
           contract="FileChangeFeed.IFileChangeFeed"
           behaviorConfiguration="webBehavior" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
<configuration>

Note the endpoint behavior. As you can guess the feed can be published over any protocol, but if you’ll want to view the feed using standard RSS feed readers, it’s better to publish it over HTTP.As soon as you’ll start the application and the service will be published – put the base Uri (http://localhost:8080/feed) in IE’s address box and you’ll see a rendered RSS feed view.Have fun with RSS feeds and don’t be afraid to explore your possibilities.The source code can be found on my site.


					
Advertisements

One Response to “RSS is not only for the news”

  1. […] by igormoochnick on February 11, 2008 In my recent article “RSS is not only for the news” I’ve mentioned that the current Syndication API is pretty flexible and allows a great […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: