TechBubbles Microsoft Technology BLOG

Publishing Feed on URI using WCF

Introduction

In My previous article “Creating Web feeds with WCF” , I have explained about building a web feed with WCF. This post explains how to build a simple WCF service that exposes the feed data using both RSS and Atom.

We are using both formats to emphasize the separation in WCF between the data [SyndicationFeed] and the formatting [SyndicationFeedFormatter].We can write the methods to return the Atom10FeedFormatter or the Rss20FeedFormatter.

The service contract is:

   1: [ServiceContract]
   2:     public interface IEventLogFeed
   3:     {
   4:         [OperationContract]
   5:         [WebGet(UriTemplate = "/{log}/feed.rss" ) ]
   6:         Rss20FeedFormatter GetRSSData(string log);
   7:  
   8:         [OperationContract]
   9:         [WebGet(UriTemplate = "/{log}/feed.atom")]
  10:         Atom10FeedFormatter GetRSSData(string log);
  11:     }

Notice that we only need to specify WebGetAttribute, which enables the WCF to route the GET requests to these methods.This feed functionality is built on the WCF 3.5 Web programming model. UriTemplateAttribute attribute is used to create different URI’s. Each URI indicates the format of the response.
The implementation of the service is straightforward where we can write a method to create SyndicationFeed object, then wraps that object into appropriate formatter.
Formatter is returned to the WCF where the infrastructure in WCF serializes the formatter to the appropriate feed type, as shown below
 
 
   1: public class EventLogFeed : IEventLogFeed
   2: {
   3:  
   4:  public Rss20FeedFormatter GetRssData(string log)
   5:  {
   6:    SyndicationFeed feed = GetFeed(log);
   7:    Rss20FeedFormatter formatter = new Rss20FeedFormatter(feed);
   8:    return formatter;
   9:  }
  10:  
  11: public Atom10FeedFormatter GetRssData(string log)
  12: {
  13:   SyndicationFeed feed = GetFeed(log);
  14:   Atom10FeedFormatter formatter = new Atom10FeedFormatter(feed);
  15:   return formatter;
  16: }

Now you can host this service in any of the WCF Hosting options.

The base URI of this service will look like /feed.atom">/feed.atom">http://localhost/EventLogFeed/<Application>/feed.atom or /feed.rss">/feed.rss">http://localhost/EventLogFeed/<Application>/feed.rss

The content in this article is based on Jon Flander’s article in visual systems journal.

Once we decided which formatted data that we want to use for the feed URI then rest of the things are taken care by WCF infrastructure.

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

Add Comment

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud