1. Serveur WCF
    • Contrat décrivant le webservice
    • [ServiceContract]
      public interface IRssService
      {
          [OperationContract]
          [WebGet(UriTemplate = "/flux/{fluxType}")]
          [ServiceKnownType(typeof(Atom10FeedFormatter))]
          [ServiceKnownType(typeof(Rss20FeedFormatter))]
          SyndicationFeedFormatter GetFlux(string fluxType);
      }
      
    • Implémentation du contrat
    • Ce service est "stateless" donc il faut spécifier comment en REST un SingleInstance. De même comme pour chaque item du flux, il est possible d'écrire les données en Html, PlainText, Url, XHTML ou Xml.
      [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
      public class ServiceRssAtom : IRssService
      {
          #region IRssService Members
      
          public SyndicationFeedFormatter GetFlux(string fluxType)
          {
              switch (fluxType)
              {
                  case "rss":
                      return CreateFeed().GetRss20Formatter(false);
      
                  case "atom":
                      return CreateFeed().GetAtom10Formatter();
      
                  default:
                      throw new NotSupportedException("Type syndication not supported");
              }
          }
      
          #endregion
      
          #region Membres privés
      
          private static SyndicationFeed CreateFeed()
          {
              SyndicationFeed feed = new SyndicationFeed();
      
              feed.Title = new TextSyndicationContent("Lelibre RSS");
              feed.Description = new TextSyndicationContent("Tu n'as pas tout vu en .Net");
              feed.Generator = "Lelibre RSS Generator";
              feed.Copyright = new TextSyndicationContent("Lelibre (c) 2008");
              feed.Authors.Add(new SyndicationPerson("titi@lelibre.net", "Thierry Thoua", "http://www.lelibre.net"));
              feed.Language = "fr-fr";
              feed.LastUpdatedTime = DateTime.Now;
      
              List items = new List();
              for(int i = 0; i < 10; ++i)
              {
                  SyndicationItem item = new SyndicationItem();
                  item.Content = SyndicationContent.CreatePlaintextContent("Blabla bla");
                  item.Title = TextSyndicationContent.CreatePlaintextContent("Un titre " + i);
                  item.Summary = TextSyndicationContent.CreatePlaintextContent("Un résumé");
                  items.Add(item);
              }
      
              feed.Items = items;
      
              return feed;
          }
      
          #endregion
      }
      
    • Lancement du service
    • ServiceRssAtom service = new ServiceRssAtom();
      WebHttpBinding binding = new WebHttpBinding();
      WebHttpBehavior behavior = new WebHttpBehavior();
      
      WebServiceHost host = new WebServiceHost(service, new Uri("http://localhost:8000"));
      host.AddServiceEndpoint(typeof(IRssService), binding, "");
      host.Open();
      
      Console.WriteLine("Rss/atom service is running at http://localhost:8000");
      Console.ReadLine();
      host.Close();
      
    • Lecture de ce flux créé
    • Il est possible de lire ce flux en accédant soit via http://localhost:8000/flux/rss pour RSS 2.0 ou via http://localhost:8000/flux/atom pour avoir le flux en Atom 1.0.


  2. Lecteur de flux RSS
  3. using (XmlReader reader = XmlReader.Create("http://weblogs.asp.net/scottgu/rss.aspx"))
    {
        SyndicationFeed feed = SyndicationFeed.Load(reader);
        Console.WriteLine("Title: {0}", feed.Title.Text);
        foreach (SyndicationItem item in feed.Items)
        {
            Console.WriteLine("----------------------------");
            Console.WriteLine("Item title: {0}", item.Title.Text);
        }
    }
    Console.ReadKey();
    
Voici donc un petit exemple sans beaucoup d'explications mais il est préférable d'avoir une petite démonstration qu'un long texte sans exemple ;-). En cas de questions, vous pouvez m'envoyer un email ou écrire un commentaire.... La documentation sur les classes SyndicateFeed etc est également disponible sur le site du MSDN.
Les sources de cet exemple sont disponible ici.