WCF 第十三章 可编程站点 RSS与ATOM内容聚合
2011-06-09 21:39 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(); } } } |
作者:DanielWise
出处:http://www.cnblogs.com/danielWise/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述