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





Client Side :


Client :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.ServiceModel.Syndication;

namespace WCF_Syndication_RSSFeed
{
    public partial class FeedReader : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            XmlReader atomReader = XmlReader.Create("http://localhost:50443/Service1.svc/feed/atom");
            Response.Write("-----------Atom feed-------------</br>");
            SyndicationFeed atomFeed = SyndicationFeed.Load(atomReader);
            Response.Write("Title :" + atomFeed.Title.Text + "</br>");
            Response.Write("Description :" + atomFeed.Description.Text + "</br>");
            Response.Write("Authors :" + atomFeed.Authors[0].Email + "</br>");
            Response.Write("Categories :" + atomFeed.Categories[0].Name+ "</br>");
            Response.Write("-------Atom Items------</br>");
            foreach (SyndicationItem item in atomFeed.Items)
            {
                Response.Write("Title:<a target='_blank' href='"+ item.Links[0].Uri.ToString() +"'>" + item.Title.Text + "</a></br>");
                Response.Write("Content:" + ((TextSyndicationContent)item.Content).Text + "</br>");
            }


            Response.Write("</br></br>");
                   
            Response.Write("-----------RSS 2.0 format feed-------------</br>");    
            XmlReader rssReader = XmlReader.Create("http://localhost:50443/Service1.svc/feed/rss");
            SyndicationFeed rssFeed = SyndicationFeed.Load(rssReader);
            Response.Write("Title :" + rssFeed.Title.Text + "</br>");
            Response.Write("Description :" + rssFeed.Description.Text + "</br>");
            Response.Write("Authors :" + rssFeed.Authors[0].Email + "</br>");
            Response.Write("Categories :" + rssFeed.Categories[0].Name + "</br>");

            Response.Write("-------RSS Items------</br>");
            foreach (SyndicationItem item in rssFeed.Items)
            {
                Response.Write("Title:<a target='_blank' href='" + item.Links[0].Uri.ToString() + "'>" + item.Title.Text + "</a></br>");
                Response.Write("Content:" + ((TextSyndicationContent)item.Summary).Text + "</br>");
            }
        }
    }
}
  


Out Put:

1 comment :

  1. RSS feeds means...exposing my articles/news/...through others as a servie url,which is basically xmls..if they consume my feed url they will get updated information...is it right? if it is possible write an article about rss fromats and advantages.

    do u have other options like RSS.
    RSS Data as json is possible

    ReplyDelete