如何:作为 Atom 和 RSS 公开源

使用 Windows Communication Foundation (WCF) 可以创建公开联合源的服务。 本主题讨论如何使用 Atom 1.0 和 RSS 2.0 创建公开联合源的联合服务。 此服务公开一个可以返回任一联合格式的终结点。 为了简单起见,本示例中使用的服务为自承载服务。 在生产环境中,此类型的服务将承载在 IIS 或 WAS 下。 有关不同 WCF 托管选项的详细信息,请参阅托管

创建基本联合服务

  1. 使用通过 WebGetAttribute 属性标记的接口定义服务协定。 作为联合源公开的每个操作都会返回一个 SyndicationFeedFormatter 对象。 请注意 WebGetAttribute 的参数。 UriTemplate 指定用于调用此服务操作的 URL。 此参数的字符串包含文本和一个用大括号括起的变量 ({format})。 此变量与服务操作的 format 参数相对应。 有关详细信息,请参阅 UriTemplateBodyStyle 会影响此服务操作所发送和接收的消息的写入方式。 Bare 指定不使用基础结构定义的 XML 元素包装发送到和接收自此服务操作的数据。 有关详细信息,请参阅 WebMessageBodyStyle

    [ServiceContract]
    [ServiceKnownType(typeof(Atom10FeedFormatter))]
    [ServiceKnownType(typeof(Rss20FeedFormatter))]
    public interface IBlog
    {
        [OperationContract]
        [WebGet(UriTemplate = "GetBlog?format={format}")]
        SyndicationFeedFormatter GetBlog(string format);
    }
    
    <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
    

    注意

    在此接口中使用 ServiceKnownTypeAttribute 以指定服务操作返回的类型。

  2. 实现服务协定。

    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("http://localhost/Content/One"),
                "ItemOneID",
                DateTime.Now);
    
            SyndicationItem item2 = new SyndicationItem(
                "Item Two",
                "This is the content for item two",
                new Uri("http://localhost/Content/Two"),
                "ItemTwoID",
                DateTime.Now);
    
            SyndicationItem item3 = new SyndicationItem(
                "Item Three",
                "This is the content for item three",
                new Uri("http://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 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("http://localhost/Content/One"), _
                "ItemOneID", _
                DateTime.Now)
    
            Dim item2 As New SyndicationItem( _
                "Item Two", _
                "This is the content for item two", _
                New Uri("http://localhost/Content/Two"), _
                "ItemTwoID", _
                DateTime.Now)
    
            Dim item3 As New SyndicationItem( _
                "Item Three", _
                "This is the content for item three", _
                New Uri("http://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
    
  3. 创建 SyndicationFeed 对象,并添加作者、类别和说明。

    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");
    
    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")
    
  4. 创建若干 SyndicationItem 对象。

    SyndicationItem item1 = new SyndicationItem(
        "Item One",
        "This is the content for item one",
        new Uri("http://localhost/Content/One"),
        "ItemOneID",
        DateTime.Now);
    
    SyndicationItem item2 = new SyndicationItem(
        "Item Two",
        "This is the content for item two",
        new Uri("http://localhost/Content/Two"),
        "ItemTwoID",
        DateTime.Now);
    
    SyndicationItem item3 = new SyndicationItem(
        "Item Three",
        "This is the content for item three",
        new Uri("http://localhost/Content/three"),
        "ItemThreeID",
        DateTime.Now);
    
    Dim item1 As New SyndicationItem( _
        "Item One", _
        "This is the content for item one", _
        New Uri("http://localhost/Content/One"), _
        "ItemOneID", _
        DateTime.Now)
    
    Dim item2 As New SyndicationItem( _
        "Item Two", _
        "This is the content for item two", _
        New Uri("http://localhost/Content/Two"), _
        "ItemTwoID", _
        DateTime.Now)
    
    Dim item3 As New SyndicationItem( _
        "Item Three", _
        "This is the content for item three", _
        New Uri("http://localhost/Content/three"), _
        "ItemThreeID", _
        DateTime.Now)
    
  5. 向源中添加 SyndicationItem 对象。

    List<SyndicationItem> items = new List<SyndicationItem>();
    items.Add(item1);
    items.Add(item2);
    items.Add(item3);
    
    feed.Items = items;
    
    Dim items As New List(Of SyndicationItem)()
    items.Add(item1)
    items.Add(item2)
    items.Add(item3)
    
    feed.Items = items
    
  6. 使用格式参数返回请求的格式。

    if (format == "rss")
        return new Rss20FeedFormatter(feed);
    else if (format == "atom")
        return new Atom10FeedFormatter(feed);
    else return null;
    
    If (format = "rss") Then
        Return New Rss20FeedFormatter(feed)
    Else
        Return New Atom10FeedFormatter(feed)
    End If
    

承载服务

  1. 创建 WebServiceHost 对象。 除非在代码或配置中指定了一个终结点,WebServiceHost 类会自动在服务的基地址添加终结点。 在此示例中,不指定任何终结点,因此公开默认终结点。

    Uri address = new Uri("http://localhost:8000/BlogService/");
    WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), address);
    
    Dim address As New Uri("http://localhost:8000/BlogService/")
    Dim svcHost As New WebServiceHost(GetType(BlogService), address)
    
  2. 打开服务主机,从服务加载源,显示源,然后等待用户按 Enter。

    svcHost.Open();
    Console.WriteLine("Service is running");
    
    Console.WriteLine("Loading feed in Atom 1.0 format.");
    XmlReader atomReader = XmlReader.Create("http://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("http://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();
    
    svcHost.Open()
    Console.WriteLine("Service is running")
    
    Console.WriteLine("Loading feed in Atom 1.0 format.")
    Dim atomReader As XmlReader = XmlReader.Create("http://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("http://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()
    

使用 HTTP GET 调用 GetBlog

  1. 打开一个浏览器,输入以下 URL,然后按 Enterhttp://localhost:8000/BlogService/GetBlog

    URL 包含服务的基址 (http://localhost:8000/BlogService)、终结点的相对地址,以及要调用的服务操作。

从代码中调用 GetBlog()

  1. 使用基址和调用的方法创建 XmlReader

    XmlReader reader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog?format=atom");
    
    Dim atomReader As XmlReader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog?format=atom")
    
  2. 调用静态 Load(XmlReader) 方法,同时传入刚刚创建的 XmlReader

    SyndicationFeed feed = SyndicationFeed.Load(reader);
    
    Dim feed As SyndicationFeed = SyndicationFeed.Load(atomReader)
    

    这将调用服务操作,并用从服务操作返回的格式化程序填充新的 SyndicationFeed

  3. 访问源对象。

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

示例

下面列出了此示例的完整代码。

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("http://localhost/Content/One"),
                "ItemOneID",
                DateTime.Now);

            SyndicationItem item2 = new SyndicationItem(
                "Item Two",
                "This is the content for item two",
                new Uri("http://localhost/Content/Two"),
                "ItemTwoID",
                DateTime.Now);

            SyndicationItem item3 = new SyndicationItem(
                "Item Three",
                "This is the content for item three",
                new Uri("http://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("http://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("http://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("http://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。

另请参阅