zoukankan      html  css  js  c++  java
  • WCF 第十三章 可编程站点 RSS与ATOM内容聚合

    RSS和ATOM是网站内容的聚合形式。这些形式为所有类型的内容聚合所使用,比如新闻,视频以及博客。到目前为止这些格式最广泛的应用就是博客。因为它早期的流行,RSS和ATOM已经被每个主要站点所使用。WCF提供很多架构来与RSS和ATOM聚合种子一起使用。一个新的叫做System.ServiceModel.Syndication的命名空间包含了创建,使用以及格式化基于RSS和ATOM聚合种子的类。创建以及使用内容聚合种子的核心类是SyndicationFeed类。列表13.16显示了使用这个类来暴露RSS和ATOM的示例程序。这个应用列举了一个音乐集合并使用一个聚合种子暴露信息。

    列表13.16 Zune 音乐聚合

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    using System.ServiceModel.Syndication;
    using System.IO;
    
    namespace EssentialWCF.ZuneMusicSyndication
    {
        public class ZuneFeedService
        {
            private static Uri LiveSearchBaseUri = new Uri("http://search.live.com");
            private static UriTemplate LiveSearchTemplate = new UriTemplate(@"/result.aspx?q={terms}");
    
            private string MusicPath
            {
                get
                {
                    return @"C:\Users\lenovo\Music\Zune";
                }
            }
             
            private SyndicationFeed ZuneFeed
            {
                get
                {
                    SyndicationFeed feed = new SyndicationFeed()
                    {
                        Title = new TextSyndicationContent("My Zune Music Library"),
                        Description = new TextSyndicationContent("My Zune Music Library")
                    };
    
                    DirectoryInfo di = new DirectoryInfo(MusicPath);
                    DirectoryInfo[] artists = di.GetDirectories();
    
                    List<SyndicationItem> items = new List<SyndicationItem>();
    
                    foreach (DirectoryInfo artist in artists)
                    {
                        SyndicationItem item = new SyndicationItem()
                        {
                            Title = new TextSyndicationContent(string.Format("Artist: {0}", artist.Name)),
                            Summary = new TextSyndicationContent(artist.FullName),
                            PublishDate = DateTime.Now, 
                            LastUpdatedTime = artist.LastAccessTime,
                            Copyright = new TextSyndicationContent(@"Zune Library (c)")
                        };
    
                        Uri searchUri = LiveSearchTemplate.BindByPosition(LiveSearchBaseUri, artist.Name);
                        item.Links.Add(new SyndicationLink(searchUri));
                        items.Add(item);
                    }
                    feed.Items = items;
                    return feed;
                }
            }
    
            [OperationContract]
            [WebGet]
            [ServiceKnownType(typeof(Atom10FeedFormatter))]
            [ServiceKnownType(typeof(Rss20FeedFormatter))]
            public SyndicationFeedFormatter GetMusic(string format)
            {
                SyndicationFeedFormatter output;
                if (format == "rss")
                {
                    output = new Rss20FeedFormatter(ZuneFeed);
                }
                else
                {
                    output = new Atom10FeedFormatter(ZuneFeed);
                }
                return output;
            }
        }
    }
    

      列表13.17 显示了寄宿聚合服务的代码。这个应用程序使用WebServiceHost类进行自我寄宿。它然后调用这个服务并向显示器输出种子。

    列表13.17 Zune音乐种子控制台应用程序

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using EssentialWCF.ZuneMusicSyndication;
    using System.ServiceModel.Description;
    using System.ServiceModel.Syndication;
    using System.Xml;
    
    namespace EssentialWCF.ZuneFeed
    {
        class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ServiceHost(typeof(ZuneFeedService),
                    new Uri("http://localhost:8000/zune"));
    
                ServiceEndpoint atomEndpoint = host.AddServiceEndpoint(typeof(ZuneFeedService),
                    new WebHttpBinding(), "feed");
                atomEndpoint.Behaviors.Add(new WebHttpBehavior());
    
                host.Open();
    
                Console.WriteLine("Service host open");
    
                SyndicationFeed feed = SyndicationFeed.Load(XmlReader.Create("http://localhost:8000/zune/feed/?format=rss"));
    
                foreach (SyndicationItem item in feed.Items)
                {
                    Console.WriteLine("Artist: " + item.Title.Text);
                    Console.WriteLine("Summary: " + item.Summary.Text);
                };
                Console.WriteLine("Service host open");
                Console.ReadLine();
            }
        }
    }
    

  • 相关阅读:
    [算法笔记] 扩展欧几里得算法
    [算法笔记] 数学基础
    [算法] 动态规划 (2)
    [算法笔记] 图论总结
    最简单的数据库入门教程—04—00—关系数据库
    最简单的数据库入门教程—03—数据库系统体系
    最简单的数据库入门教程—02—数据模型
    最简单的数据库入门教程—01—数据库系统概论
    最简单的数据库入门教程—00—数据库导论
    数据可视化分析
  • 原文地址:https://www.cnblogs.com/danielWise/p/2076856.html
Copyright © 2011-2022 走看看