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:
1: SyndicationFeed feed = new SyndicationFeed();
2: List<SyndicationItem> items = new List<SyndicationItem>();
3: ...
4: SyndicationItem item = new SyndicationItem(title, path, GetFileUri(path));
5: items.Add(item);
6: 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):
1: FileChangeFeedService service = new FileChangeFeedService();
2: ServiceHost serviceHost = new ServiceHost(service);
3: serviceHost.Open();
Note: don’t forget to mark the service class as a single-instance service by adding ServiceBehavior attribute –
1: [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
2: 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:
1: [OperationContract]
2: [WebGet(UriTemplate = "?format={format}")]
3: SyndicationFeedFormatter Feed(string format);
This contract method will be called if a web request will come to the following URL:
http://localhost/feed?format=rss
The 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):
1: public SyndicationFeedFormatter Feed(string format)
2: {
3: if ((format != null) && (format.ToLower() == "rss"))
4: return new Rss20FeedFormatter(feed);
5:
6: return new Atom10FeedFormatter(feed);
7: }
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.
No comments:
Post a Comment