Content Syndication with RSS and ATOM
RSS and ATOM are content syndication formats for the Web. These formats are used for all types of content syndication, such as news, video, and blogs. By far the widest use for these formats is for blogging. Since its initial popularity, RSS and ATOM have been used by every major Web site. WCF provides several mechanisms for working with RSS and ATOM syndication feeds. A new namespace, called System.ServiceModel.Syndication, contains classes for creating, consuming, and formatting syndication feeds based on RSS and ATOM. The core class for creating and consuming content syndication feeds is the SyndicationFeed class. Listing 13.16 shows an example application using this class to expose an RSS and ATOM. This application enumerates over a music collection and exposes the information using a syndication feed.
Listing 13.16. Zune Music Syndication
using System; using System.IO; using System.Collections.Generic; using System.ServiceModel; using System.ServiceModel.Syndication; using System.ServiceModel.Web; [ServiceContract] public class ZuneFeedService { private static Uri LiveSearchBaseURI = new Uri("http://search.live.com"); private static UriTemplate LiveSearchTemplate = new UriTemplate(@"/results.aspx?q={terms}"); private string MusicPath { get { return @"C:\Users\ricrane\Music\Zune"; } } private SyndicationFeed ZuneFeed { get { SyndicationFeed feed = new SyndicationFeed() { Title = new TextSyndicationContent("My Zune Music Library"), Description = new TextSyndicationContent("My Zune Music Library") }; DirectoryInfo di = new DirectoryInfo(MusicPath); DirectoryInfo[] artists = di.GetDirectories(); List<SyndicationItem> items = new List<SyndicationItem>(); foreach (DirectoryInfo artist in artists) { SyndicationItem item = new SyndicationItem() { Title = new TextSyndicationContent(string.Format("Artist: {0}", artist.Name)), Summary = new TextSyndicationContent(artist.FullName), PublishDate = DateTime.Now, LastUpdatedTime = artist.LastAccessTime, Copyright = new TextSyndicationContent(@"Zune Library (c)") }; Uri searchUri = LiveSearchTemplate.BindByPosition(LiveSearchBaseURI, artist.Name); item.Links.Add(new SyndicationLink(searchUri)); items.Add(item); } feed.Items = items; return feed; } } [OperationContract] [WebGet] [ServiceKnownType(typeof(Atom10FeedFormatter))] [ServiceKnownType(typeof(Rss20FeedFormatter))] public SyndicationFeedFormatter<SyndicationFeed> GetMusic(string format) { SyndicationFeedFormatter<SyndicationFeed> output; if (format == "rss") output = new Rss20FeedFormatter(ZuneFeed); else output = new Atom10FeedFormatter(ZuneFeed); return output; } }
Listing 13.17 shows the code to host the syndication service. The application self-hosts the service using the WebServiceHost class. It then consumes the service and outputs the feed to the display.
Listing 13.17. Zune Music Feed Console Application
using System; using System.Collections.Generic; using System.Diagnostics; using System.ServiceModel; using System.ServiceModel.Description; using System.ServiceModel.Syndication; using System.ServiceModel.Web; namespace ZuneFeed { class Program { static void Main(string[] args) { ServiceHost host = new ServiceHost(typeof(ZuneFeedService), new Uri("http://localhost:8000/zune")); ServiceEndpoint atomEndpoint = host.AddServiceEndpoint(typeof(ZuneFeedService), new WebHttpBinding(), "feed"); atomEndpoint.Behaviors.Add(new WebHttpBehavior()); host.Open(); Console.WriteLine("Service host open"); SyndicationFeed feed = SyndicationFeed.Load( new Uri("http://localhost:8000/zune/feed/?format=rss")); foreach (SyndicationItem item in feed.Items) { Console.WriteLine("Artist: " + item.Title.Text); Console.WriteLine("Summary: " + item.Summary.Text); } Console.WriteLine("Press [Enter] to exit.]"); Console.ReadLine(); } } }