Reading syndication feeds is one scenario in which the Syndication API can be used. Another is to offer a syndication feed to RSS and Atom clients.
For this, Visual Studio 2008 offers the Syndication Service Library template, which you can use to start with. This template defines a reference to the System. Service Model. Web library, and adds an application configuration file to define a WCF endpoint.
To offer data for the syndication feed, the LINQ provider LINQ to SQL is helpful. In the sample application, the Formula database is used, which you can download from the Csharp web site at www.csharpaid.com with the sample applications for the book. The “UNQ to SQL Classes” item with the name Formula is added to the project. Here, the tables Racers, RaceResults, Races, and Circuits are mapped to entity classes Racer, RaceResult, Race, and Circuit, as shown in Figure 48-3.
LINQ to SQL is discussed in Chapter 27.
The project template creates a file IService1.cs that contains the contract of the WCF service. The interface contains the CreateFeed() method, which returns a SyndicationFeedFormatter. Because SyndicationFeedFormatter is an abstract class, and the real types returned are either Atom10FeedFormatter” or Rss 20 Feed Formatter, these types are listed with the Service Known Type Attribute, so that the type is known for serialization.
The attribute Web Get defines that the operation can be called from a simple HTTP GET request that can be used to request syndication feeds. WebMessageBodyStyle. Bare defines that the result (the XML from the syndication feed) is sent as it is without adding an XML wrapper element around it.
The implementation of the service is done in the class FormulalFeed. Here, a Syndication Feed item is created, and various’ properties of this class such as Generator, Language, Title, Categories, and Authors are assigned. The Items property is filled from a LINQ to SQL query that requests the winners of Formula races of the year 2007.With the select clause of this query, a Syndication Item is created for every winner. With Syndication Item, the Title property is assigned to plain text containing the country of the race.,The Content property is filled with the help of LINQ to XML. XElement classes-are used to create XHTML code that can be interpreted by the browser. This content shows the date of the race,the country; and the name of the winner.
Depending on the query string to request the syndication, the SyndicationFeed is formatted with the Atom10FeedFormatter or the Rss20FeedFormatter.
When you start the service from within Visual Studio 2008, the WCF, service Host starts up to host the service, and you can see the feed result formatted in Internet Explorer, as shown in Figure 48-4.
With the default request to the service, the RSS feed is returned. An extract of the RSS feed with the rss root element follows. With RSS,the Title property is translated to the ti tIe element, and the Description property goes to the description element. The Authors property of the SyndicationFeed th’\t,contains Syndication Person just uses the e-mail address to create the managing Editor element. To add more information to the feed, the formatter also places some Atom elements in the RSS feed. Placing Atom elements in an RSS feed is a common practice that provides
information that is not defined by RSS.
An Atom formatted feed is returned with the query ?format=atom with the result shown, The root element now is the feed element; the Description property tums into a subtitIe element; and the values for the Author property are now shown completely differently from the RSS feed shown earlier. Atom allows the content to be unencoded. You can easily find the XHTML elements..