Wednesday, May 22, 2013

WCF Syndication



Web syndication is a form of syndication in which website material is made available to multiple other sites.

E.g.   latest news or forum posts

·         Most common feed formats is called RSS  2.0 ( "Really Simple Syndication." ) , It is an XML format, considered highly stable, that allows for extensions only through the use of namespaces
·        Second  syndication feed is also called RSS "RDF Site Summary" and it's RSS 1.0
·        Third  common format for syndication feeds is called Atom .It was created using a wiki,


WCF provides support for exposing syndication feeds from a WCF service. 

 Namespace:  System.ServiceModel.Syndication




Interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
namespace WCF_Syndication_RSSFeed
{
    //ServiceKnownType: indicate that what is the return
    //type of our desired feed will be look like . eg:ATOM or RSS.
    [ServiceContract]
    [ServiceKnownType(typeof(Atom10FeedFormatter))]
    [ServiceKnownType(typeof(Rss20FeedFormatter))]
    public interface IService1
    {
         
       [WebGet(UriTemplate = "/feed/{format}")]
       [OperationContract]
        SyndicationFeedFormatter getinfo(string format);
    }
}



Service

<%@ ServiceHost
 Language="C#"
 Debug="true"
 Service="Feeds"
 CodeBehind="Feeds.svc.cs"
 Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;


namespace WCF_Syndication_RSSFeed
{
   
    public class Service1 : IService1
    {
        public SyndicationFeedFormatter getinfo(string format)
        {
            SyndicationItem item1 = new SyndicationItem(
                             "Google News",
                             "see google news",
                             new Uri("https://news.google.com//"),
                             "GGL001",
                             DateTime.Now);
       
            SyndicationItem item2 = new SyndicationItem(
                           "Timesofindia News",
                           "Ser time of india news",
                           new Uri("http://timesofindia.indiatimes.com/"),
                           "TOI001",
                           DateTime.Now);
       
            SyndicationItem item3 = new SyndicationItem(
                "BBC News",
                "see bbc news information",
                new Uri("http://www.bbc.com"),
                "BBC001",
                DateTime.Now);

            //Adding the items to the list of generic syndication items.
            List<SyndicationItem> items = new List<SyndicationItem>();
            items.Add(item1);
            items.Add(item2);
            items.Add(item3);

            SyndicationFeed feed = new SyndicationFeed();
            feed.Title = new TextSyndicationContent("Website Feed");
            feed.Description = new TextSyndicationContent("News Providing Websites");
            feed.Authors.Add(new SyndicationPerson("Newsfeeder@gmail.com"));          
            feed.Categories.Add(new SyndicationCategory("News Websites"));        
         
         
            feed.Items = items;
            if (format == "rss")
                return new Rss20FeedFormatter(feed);
            else if (format == "atom")
                return new Atom10FeedFormatter(feed);
            else return null;           
        
        }

    }
}


Right click on service to view in browser