代码改变世界

WCF 第十三章 可编程站点 RSS与ATOM内容聚合

  DanielWise  阅读(619)  评论(0编辑  收藏  举报

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();
        }
    }
}

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示