DCSIMG
Creating a Syndication Feed with WCF 3.5 - Gady Elkarif's Blog

Gady Elkarif's Blog

Creating a Syndication Feed with WCF 3.5

Creating a Syndication Feed with WCF 3.5

The WCF 3.5 Syndication API, include rich support of creating and consuming syndicated content.

In this post I'm going to create a service , which provides information about articles both in RSS 2.0 and ATOM 1.0 formats.

Service Contract

The following code described the service contract, which include two methods with the same uri, but different query parameter format (rss/atom).

    [ServiceContract]

    public interface IArticleService

    {

        [OperationContract, WebGet(UriTemplate = "feeds?format=rss", ResponseFormat = WebMessageFormat.Xml)]

        Rss20FeedFormatter GetArticleInfoRss();

 

        [OperationContract, WebGet(UriTemplate = "feeds?format=atom", ResponseFormat = WebMessageFormat.Xml)]

        Atom10FeedFormatter GetArticleInfoAtom();

    }

Service Implementation

The following web service implementation expose the two feed formats, one using Rss20FeedFormatter and the second Atom10FeedFormatter which resides on System.ServiceModel.Syndication namespace (in System.ServiceModel.Web.dll).

  • Rss20FeedFormatter - A class that serializes SyndicationFeed to and from RSS 2.0 format.
  • Atom10FeedFormatter - A class that serializes SyndicationFeed to and from ATOM 1.0 format.

    public class ArticleService : IArticleService

    {

        public Rss20FeedFormatter GetArticleInfoRss()

        {

            SyndicationFeed syndicationFeed = GetSyndicationFeed();

 

            Rss20FeedFormatter formatter =

                new Rss20FeedFormatter(syndicationFeed);

 

            return formatter;

        }

 

        public Atom10FeedFormatter GetArticleInfoAtom()

        {

            SyndicationFeed syndicationFeed = GetSyndicationFeed();

 

            Atom10FeedFormatter formatter =

                new Atom10FeedFormatter(syndicationFeed);

 

            return formatter;

        }

 

        private SyndicationFeed GetSyndicationFeed()

        {

            Uri uri = OperationContext.Current.IncomingMessageHeaders.To;

 

            SyndicationFeed syndicationFeed = new SyndicationFeed(

                "WCF 3.5 Articles",

                "WCF - Windows Communication Foundation Articles ",

                uri,

                "FeedID",

                DateTime.Now);

 

            SyndicationItem item1 = new SyndicationItem(

                "Article 1",

                "This is the content for Article 1",

                new Uri("http://www.wcf.com/wcfSyndication1.aspx"),

                "ItemID1",

                DateTime.Now);

 

            SyndicationItem item2 = new SyndicationItem(

                "Article 2",

                "This is the content for Article 2",

                new Uri("http://www.wcf.com/wcfSyndication2.aspx"),

                "ItemID2",

                DateTime.Now);

 

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

            items.Add(item1);

            items.Add(item2);

 

            syndicationFeed.Items = items;

 

            return syndicationFeed;

        }

    }

Service Host

The Web.Config of the service include webHttpBinding, as described in the following service model:

 <system.serviceModel>

   <services>

      <service name="ArticleService.ArticleService"

              behaviorConfiguration="ArticleService.ArticleServiceBehavior">

        <endpoint address=""

                  binding="webHttpBinding"

                  contract="ArticleService.IArticleService"

                  behaviorConfiguration="webHttpBehavior">

          <identity>

            <dns value="localhost"/>

          </identity>

        </endpoint>

        <endpoint address="mex"

                  binding="mexHttpBinding"

                  contract="IMetadataExchange"/>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="ArticleService.ArticleServiceBehavior">

          <serviceMetadata httpGetEnabled="true"/>

          <serviceDebug includeExceptionDetailInFaults="false"/>

        </behavior>

      </serviceBehaviors>

      <endpointBehaviors>

        <behavior name="webHttpBehavior">

          <webHttp/>

        </behavior>

      </endpointBehaviors>

    </behaviors>

  </system.serviceModel>

Transforming a Feed

The following code sample transform the feed content into RSS 2.0 and prints it into the console.

            String feedUri = "http://localhost:1329/ArticleService.svc/feeds?format=rss";

            XmlReader reader = XmlReader.Create(feedUri);

 

            SyndicationFeed feed = SyndicationFeed.Load(reader);

 

            // transform it to RSS

            Rss20FeedFormatter formatter = new Rss20FeedFormatter(feed);

            XmlWriter writer = XmlWriter.Create(Console.Out, null);

 

            // write it to the Console

            formatter.WriteTo(writer);

            writer.Flush();

When you type the same url into a browser, the result for RSS 2.0 is:

WCF 3.5 Syndication API

 

And for ATOM 2.0 format:

WCF 3.5 Syndication API

 

Conclusion

WCF 3.5 supports creating and consuming both RSS 2.0 and ATOM 1.0 formats which is very easy to use. You can serve and consume syndicated content over any transport.

This is very powerfull, since with those formats you can express any set of data.

The source code WCF Syndication.
פורסם: Mar 18 2008, 11:36 PM by egady | with no comments
תגים:, ,
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 1 and 4 and type the answer here:


Enter the numbers above: