- Serveur WCF
- Contrat décrivant le webservice
- 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.
- Lancement du service
- 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.
- Lecteur de flux RSS
[ServiceContract] public interface IRssService { [OperationContract] [WebGet(UriTemplate = "/flux/{fluxType}")] [ServiceKnownType(typeof(Atom10FeedFormatter))] [ServiceKnownType(typeof(Rss20FeedFormatter))] SyndicationFeedFormatter GetFlux(string fluxType); }
[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; Listitems = 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 }
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();
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();
Les sources de cet exemple sont disponible ici.