如何:作为 Atom 和 RSS 公开源
Windows Communication Foundation (WCF) 允许您创建公开联合源的服务。本主题讨论如何使用 Atom 1.0 和 RSS 2.0 创建公开联合源的联合服务。此服务公开一个可以返回任一联合格式的终结点。为了简单起见,本示例中使用的服务为自承载服务。在生产环境中,此类型的服务将承载在 IIS 或 WAS 下。有关不同 WCF 承载选项的更多信息,请参见 承载。
创建基本联合服务
使用通过 WebGetAttribute 属性标记的接口定义服务协定。作为联合源公开的每个操作都会返回一个 SyndicationFeedFormatter 对象。请注意,WebGetAttribute.
UriTemplate
的参数指定了用于调用此服务操作的 URL。此参数的字符串包含文本和一个用大括号括住的变量 ({format})。此变量与服务操作的 format 参数相对应。有关更多信息,请参见 UriTemplate.BodyStyle
会影响此服务操作所发送和接收的消息的写入方式。Bare 指定不使用基础结构定义的 XML 元素包装发送到和接收自此服务操作的数据。有关更多信息,请参见 WebMessageBodyStyle.<ServiceContract()> _ <ServiceKnownType(GetType(Atom10FeedFormatter))> _ <ServiceKnownType(GetType(Rss20FeedFormatter))> _ Public Interface IBlog <OperationContract()> _ <WebGet(UriTemplate:="GetBlog?format={format}")> _ Function GetBlog(ByVal format As String) As SyndicationFeedFormatter End Interface
[ServiceContract] [ServiceKnownType(typeof(Atom10FeedFormatter))] [ServiceKnownType(typeof(Rss20FeedFormatter))] public interface IBlog { [OperationContract] [WebGet(UriTemplate = "GetBlog?format={format}")] SyndicationFeedFormatter GetBlog(string format); }
注意: 在此接口中使用 ServiceKnownTypeAttribute 以指定服务操作返回的类型。 实现服务协定。
Public Class BlogService implements IBlog Public Function GetBlog(ByVal format As String) As SyndicationFeedFormatter Implements IBlog.GetBlog Dim feed As New SyndicationFeed("My Blog Feed", "This is a test feed", New Uri("http://SomeURI")) feed.Authors.Add(New SyndicationPerson("someone@microsoft.com")) feed.Categories.Add(New SyndicationCategory("How To Sample Code")) feed.Description = New TextSyndicationContent("This is a sample that demonstrates how to expose a feed through RSS and Atom with WCF") Dim item1 As New SyndicationItem( _ "Item One", _ "This is the content for item one", _ New Uri("https://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("https://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As New SyndicationItem( _ "Item Three", _ "This is the content for item three", _ New Uri("https://localhost/Content/three"), _ "ItemThreeID", _ DateTime.Now) Dim items As New List(Of SyndicationItem)() items.Add(item1) items.Add(item2) items.Add(item3) feed.Items = items If (format = "rss") Then Return New Rss20FeedFormatter(feed) Else Return New Atom10FeedFormatter(feed) End If End Function End Class
public class BlogService : IBlog { public SyndicationFeedFormatter GetBlog(string format) { SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI")); feed.Authors.Add(new SyndicationPerson("someone@microsoft.com")); feed.Categories.Add(new SyndicationCategory("How To Sample Code")); feed.Description = new TextSyndicationContent("This is a sample that demonstrates how to expose a feed through RSS and Atom with WCF"); SyndicationItem item1 = new SyndicationItem( "Item One", "This is the content for item one", new Uri("https://localhost/Content/One"), "ItemOneID", DateTime.Now); SyndicationItem item2 = new SyndicationItem( "Item Two", "This is the content for item two", new Uri("https://localhost/Content/Two"), "ItemTwoID", DateTime.Now); SyndicationItem item3 = new SyndicationItem( "Item Three", "This is the content for item three", new Uri("https://localhost/Content/three"), "ItemThreeID", DateTime.Now); List<SyndicationItem> items = new List<SyndicationItem>(); items.Add(item1); items.Add(item2); items.Add(item3); feed.Items = items; if (format == "rss") return new Rss20FeedFormatter(feed); else if (format == "atom") return new Atom10FeedFormatter(feed); else return null; } }
创建 SyndicationFeed 对象,并添加作者、类别和说明。
Dim feed As New SyndicationFeed("My Blog Feed", "This is a test feed", New Uri("http://SomeURI")) feed.Authors.Add(New SyndicationPerson("someone@microsoft.com")) feed.Categories.Add(New SyndicationCategory("How To Sample Code")) feed.Description = New TextSyndicationContent("This is a sample that demonstrates how to expose a feed through RSS and Atom with WCF")
SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI")); feed.Authors.Add(new SyndicationPerson("someone@microsoft.com")); feed.Categories.Add(new SyndicationCategory("How To Sample Code")); feed.Description = new TextSyndicationContent("This is a sample that demonstrates how to expose a feed through RSS and Atom with WCF");
创建若干 SyndicationItem 对象。
Dim item1 As New SyndicationItem( _ "Item One", _ "This is the content for item one", _ New Uri("https://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("https://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As New SyndicationItem( _ "Item Three", _ "This is the content for item three", _ New Uri("https://localhost/Content/three"), _ "ItemThreeID", _ DateTime.Now)
SyndicationItem item1 = new SyndicationItem( "Item One", "This is the content for item one", new Uri("https://localhost/Content/One"), "ItemOneID", DateTime.Now); SyndicationItem item2 = new SyndicationItem( "Item Two", "This is the content for item two", new Uri("https://localhost/Content/Two"), "ItemTwoID", DateTime.Now); SyndicationItem item3 = new SyndicationItem( "Item Three", "This is the content for item three", new Uri("https://localhost/Content/three"), "ItemThreeID", DateTime.Now);
向源中添加 SyndicationItem 对象。
Dim items As New List(Of SyndicationItem)() items.Add(item1) items.Add(item2) items.Add(item3) feed.Items = items
List<SyndicationItem> items = new List<SyndicationItem>(); items.Add(item1); items.Add(item2); items.Add(item3); feed.Items = items;
使用格式参数返回请求的格式。
If (format = "rss") Then Return New Rss20FeedFormatter(feed) Else Return New Atom10FeedFormatter(feed) End If
if (format == "rss") return new Rss20FeedFormatter(feed); else if (format == "atom") return new Atom10FeedFormatter(feed); else return null;
承载服务
创建一个 WebServiceHost 对象。除非在代码或配置中指定了一个终结点,WebServiceHost 类会自动在服务的基地址添加终结点。在此示例中,不指定任何终结点,因此公开默认终结点。
Dim address As New Uri("https://localhost:8000/BlogService/") Dim svcHost As New WebServiceHost(GetType(BlogService), address)
Uri address = new Uri("https://localhost:8000/BlogService/"); WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), address);
打开服务主机,从服务加载源,显示源,然后等待用户按 Enter。
svcHost.Open() Console.WriteLine("Service is running") Console.WriteLine("Loading feed in Atom 1.0 format.") Dim atomReader As XmlReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=atom") Dim atomFeed As SyndicationFeed = SyndicationFeed.Load(atomReader) Console.WriteLine(atomFeed.Title.Text) Console.WriteLine("Items:") For Each item As SyndicationItem In atomFeed.Items Console.WriteLine("Title: {0}", item.Title.Text) Console.WriteLine("Content: {0}", CType(item.Content, TextSyndicationContent).Text) Next Console.WriteLine("Loading feed in RSS 2.0 format.") Dim rssReader As XmlReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=rss") Dim rssFeed As SyndicationFeed = SyndicationFeed.Load(rssReader) Console.WriteLine(rssFeed.Title.Text) Console.WriteLine("Items:") For Each item As SyndicationItem In rssFeed.Items Console.WriteLine("Title: {0}", item.Title.Text) Console.WriteLine("Content: {0}", CType(item.Content, TextSyndicationContent).Text) Next Console.WriteLine("Press <ENTER> to quit...") Console.ReadLine() svcHost.Close()
svcHost.Open(); Console.WriteLine("Service is running"); Console.WriteLine("Loading feed in Atom 1.0 format."); XmlReader atomReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=atom"); SyndicationFeed atomFeed = SyndicationFeed.Load(atomReader); Console.WriteLine(atomFeed.Title.Text); Console.WriteLine("Items:"); foreach (SyndicationItem item in atomFeed.Items) { Console.WriteLine("Title: {0}", item.Title.Text); Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Content).Text); } Console.WriteLine("Loading feed in RSS 2.0 format."); XmlReader rssReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=rss"); SyndicationFeed rssFeed = SyndicationFeed.Load(rssReader); Console.WriteLine(rssFeed.Title.Text); Console.WriteLine("Items:"); foreach (SyndicationItem item in rssFeed.Items) { Console.WriteLine("Title: {0}", item.Title.Text); // Notice we are using item.Summary here instead of item.Content. This is because // of the differences between Atom 1.0 and RSS 2.0 specs. Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Summary).Text); } Console.WriteLine("Press <ENTER> to quit..."); Console.ReadLine(); svcHost.Close();
使用 HTTP GET 调用 GetBlog
打开 Internet Explorer,键入下面的 URL 并按 Enter:https://localhost:8000/BlogService/GetBlog
URL 包含服务的基址 (https://localhost:8000/BlogService)、终结点的相对地址,以及要调用的服务操作。
从代码中调用 GetBlog()
使用基址和调用的方法创建 XmlReader。
Dim atomReader As XmlReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=atom")
XmlReader reader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=atom");
调用静态 Load 方法,同时传入刚刚创建的 XmlReader。
Dim feed As SyndicationFeed = SyndicationFeed.Load(atomReader)
SyndicationFeed feed = SyndicationFeed.Load(reader);
这将调用服务操作,并用从服务操作返回的格式化程序填充新的 SyndicationFeed。
访问源对象。
Console.WriteLine(feed.Title.Text) Console.WriteLine("Items:") For Each item As SyndicationItem In feed.Items Console.WriteLine("Title: {0}", item.Title.Text) Console.WriteLine("Content: {0}", (CType(item.Content, TextSyndicationContent).Text)) Next
Console.WriteLine(feed.Title.Text); Console.WriteLine("Items:"); foreach (SyndicationItem item in feed.Items) { Console.WriteLine("Title: {0}", item.Title.Text); Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Content).Text); }
示例
下面列出了此示例的完整代码。
using System;
using System.Xml;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
namespace Service
{
[ServiceContract]
[ServiceKnownType(typeof(Atom10FeedFormatter))]
[ServiceKnownType(typeof(Rss20FeedFormatter))]
public interface IBlog
{
[OperationContract]
[WebGet(UriTemplate = "GetBlog?format={format}")]
SyndicationFeedFormatter GetBlog(string format);
}
public class BlogService : IBlog
{
public SyndicationFeedFormatter GetBlog(string format)
{
SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI"));
feed.Authors.Add(new SyndicationPerson("someone@microsoft.com"));
feed.Categories.Add(new SyndicationCategory("How To Sample Code"));
feed.Description = new TextSyndicationContent("This is a sample that demonstrates how to expose a feed through RSS and Atom with WCF");
SyndicationItem item1 = new SyndicationItem(
"Item One",
"This is the content for item one",
new Uri("https://localhost/Content/One"),
"ItemOneID",
DateTime.Now);
SyndicationItem item2 = new SyndicationItem(
"Item Two",
"This is the content for item two",
new Uri("https://localhost/Content/Two"),
"ItemTwoID",
DateTime.Now);
SyndicationItem item3 = new SyndicationItem(
"Item Three",
"This is the content for item three",
new Uri("https://localhost/Content/three"),
"ItemThreeID",
DateTime.Now);
List<SyndicationItem> items = new List<SyndicationItem>();
items.Add(item1);
items.Add(item2);
items.Add(item3);
feed.Items = items;
if (format == "rss")
return new Rss20FeedFormatter(feed);
else if (format == "atom")
return new Atom10FeedFormatter(feed);
else return null;
}
}
public class Host
{
static void Main(string[] args)
{
Uri address = new Uri("https://localhost:8000/BlogService/");
WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), address);
try
{
svcHost.Open();
Console.WriteLine("Service is running");
Console.WriteLine("Loading feed in Atom 1.0 format.");
XmlReader atomReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=atom");
SyndicationFeed atomFeed = SyndicationFeed.Load(atomReader);
Console.WriteLine(atomFeed.Title.Text);
Console.WriteLine("Items:");
foreach (SyndicationItem item in atomFeed.Items)
{
Console.WriteLine("Title: {0}", item.Title.Text);
Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Content).Text);
}
Console.WriteLine("Loading feed in RSS 2.0 format.");
XmlReader rssReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog?format=rss");
SyndicationFeed rssFeed = SyndicationFeed.Load(rssReader);
Console.WriteLine(rssFeed.Title.Text);
Console.WriteLine("Items:");
foreach (SyndicationItem item in rssFeed.Items)
{
Console.WriteLine("Title: {0}", item.Title.Text);
// Notice we are using item.Summary here instead of item.Content. This is because
// of the differences between Atom 1.0 and RSS 2.0 specs.
Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Summary).Text);
}
Console.WriteLine("Press <ENTER> to quit...");
Console.ReadLine();
svcHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
svcHost.Abort();
}
}
}
}
编译代码
编译前面的代码时,请引用 System.ServiceModel.dll 和 System.ServiceModel.Web.dll。