Orcas syndication API

Syndication

Syndication is becoming more and more popular. In essence, it is just some form of representation of data, and not only applicable to weblogs and news items. The .NET Framework 3.5 (WCF in particular) comes with a syndication API that allows providing and constructing new RSS or ATOM feeds, and consuming feeds in a client application.

The syndication API consists of an object model that represents an arbitrary feed. The root of the feed is represented by the SyndicationFeed class. Here’s a typical piece of code to construct a feed with some game statistic information:

  List<SyndicationItem> items = new List<SyndicationItem>();

  items.Add(new SyndicationItem

            {

              Title = new TextSyndicationContent(“Counter-Strike”),

              Authors = {

                  new SyndicationPerson(“alexthissen@html.com”,

                      “Alex”, http://blog.alexthissen.nl&#8221;)

                  new SyndicationPerson(“alexthissen@html.com”,

                      “Dennis”, http://bloggingabout.net&#8221;) },

                  },

              Content = new TextSyndicationContent(“<h1>1337 kills</h1>”,

                TextSyndicationContentKind.XHtml)

            });

 

  SyndicationFeed feed = new SyndicationFeed(

          title, “Statistics for players on this server”, null,

          items);

  feed.LastUpdatedTime = DateTime.Now;

A feed consists of items represented by SyndicationItem objects. You can see how in the construction of the (single) item an object initializer is used. Inside the initializer is a collection initializer for the collection of authors (of type SyndicationPerson). The use of these initializers is not related to the syndication API, but still fun to show, see and use.

The object model for syndication is a bit particular. A lot of properties (e.g. on the feed and items) are read-only. The constructors or object initializers are the only way to set these. This means that you need to have everything ready up front, or … use Linq query expressions as part of the construction. Here’s an example, where the boldfaced part is the Linq expression:

  SyndicationFeed feed = new SyndicationFeed(

    title, “Statistics for players on this server”, null,

    from player in server.GetAllPlayers()

    where player.GameType == 1

    select new SyndicationItem(String.Format(“Player ‘{0}'”, player.Name),

      player.GameType.ToString(), null)

  );

Service operations that return a syndication feed are typically WebGet annotated. That way the operation calls can be made by simple HTTP GETs, which is what you would want to line up with popular feed consuming APIs and applications. Here’s a sample on how to create a syndication feed and return it from a service operation:

  public SyndicationFeedFormatter<SyndicationFeed> GetPlayerStats(string player)

  {

    string title = String.Format(“Player ‘{0}’ stats”, player);

 

    List<SyndicationItem> items = new List<SyndicationItem>();

    // … Like above

 

    //SyndicationFeed feed = new SyndicationFeed(

    // … Like above

 

    return new Atom10FeedFormatter(feed);

  }

The service contract does not require you to specify the type of feed. You can decide so in the concrete type of the return statement, just as long as your feed object derives from SyndicationFeedFormatter<SyndicationFeed>. Currently the RSS 2.0 and Atom 1.0 standards are supported by means of the Atom10FeedFormatter and Rss20FeedFormatter. You are free to create your own syndication formats, of course. Both specifications and the object model allow you to add extensions, such as the Simple List Extensions that Microsoft introduced for RSS 2.0 in Internet Explorer 7.0.

Side note: Like in my previous post on Orcas Web Programming, this is new from the June 2007 CTP of .NET 3.5 onwards. It used to be a behavior, added to the endpoint of the service, that took care of that part.

An abundant number of possibilities I would say. Go get the bits and experiment.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s