HOW TO:建立基本 RSS 摘要
Windows Communication Foundation (WCF) 可以讓您建立公開新聞訂閱摘要的服務。本主題討論如何建立可公開 RSS 新聞訂閱摘要的新聞訂閱服務。
若要建立基本新聞訂閱服務
使用以 WebGetAttribute 屬性標記的介面來定義服務合約。每個公開為新聞訂閱摘要的作業都應該傳回 Rss20FeedFormatter 物件。
<ServiceContract()> _ Public Interface IBlog <OperationContract()> _ <WebGet> _ Function GetBlog() As Rss20FeedFormatter End Interface
[ServiceContract] public interface IBlog { [OperationContract] [WebGet] Rss20FeedFormatter GetBlog(); }
注意: 所有套用 WebGetAttribute 屬性的服務作業都會對應至 HTTP GET 要求。若要將您的作業對應至不同的 HTTP 方法,請改用 WebInvokeAttribute。如需詳細資訊,請參閱 HOW TO:建立基本 WCF Web HTTP 服務. 實作服務合約。
Public Class BlogService Implements IBlog Public Function GetBlog() As Rss20FeedFormatter Implements IBlog.GetBlog Dim feed As SyndicationFeed = 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 how to sample that demonstrates how to expose a feed using RSS with WCF") Dim item1 As SyndicationItem = New SyndicationItem( _ "Item One", _ "This is the content for item one", _ New Uri("https://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As SyndicationItem = New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("https://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As SyndicationItem = 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 Return New Rss20FeedFormatter(feed) End Function End Class
public class BlogService : IBlog { public Rss20FeedFormatter GetBlog() { 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 how to sample that demonstrates how to expose a feed using RSS 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; return new Rss20FeedFormatter(feed); } }
建立 SyndicationFeed 物件並加入作者、分類和描述。
Dim feed As SyndicationFeed = 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 how to sample that demonstrates how to expose a feed using RSS 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 how to sample that demonstrates how to expose a feed using RSS with WCF");
建立幾個 SyndicationItem 物件。
Dim item1 As SyndicationItem = New SyndicationItem( _ "Item One", _ "This is the content for item one", _ New Uri("https://localhost/Content/One"), _ "ItemOneID", _ DateTime.Now) Dim item2 As SyndicationItem = New SyndicationItem( _ "Item Two", _ "This is the content for item two", _ New Uri("https://localhost/Content/Two"), _ "ItemTwoID", _ DateTime.Now) Dim item3 As SyndicationItem = 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;
傳回摘要。
Return New Rss20FeedFormatter(feed)
return new Rss20FeedFormatter(feed);
若要裝載服務
建立 WebServiceHost 物件。
Dim baseAddress As New Uri("https://localhost:8000/BlogService") Dim svcHost As New WebServiceHost(GetType(BlogService), baseAddress)
Uri baseAddress = new Uri("https://localhost:8000/BlogService"); WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), baseAddress);
開啟服務主機並等候使用者按下 ENTER。
svcHost.Open() Console.WriteLine("Service is running") Dim reader As XmlReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog") Dim feed As SyndicationFeed = SyndicationFeed.Load(reader) 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("Summary: {0}", item.Summary.Text) Next Console.WriteLine("Press <enter> to quit...") Console.ReadLine() svcHost.Close()
svcHost.Open(); Console.WriteLine("Service is running"); XmlReader reader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog"); SyndicationFeed feed = SyndicationFeed.Load(reader); Console.WriteLine(feed.Title.Text); Console.WriteLine("Items:"); foreach (SyndicationItem item in feed.Items) { Console.WriteLine("Title: {0}", item.Title.Text); Console.WriteLine("Summary: {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 serviceAddress As New Uri("https://localhost:8000/BlogService/GetBlog")
Uri serviceAddress = new Uri("https://localhost:8000/BlogService/GetBlog");
呼叫靜態 Load 方法,傳入剛才建立的 XmlReader。
Dim reader As XmlReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog") Dim feed As SyndicationFeed = SyndicationFeed.Load(reader)
XmlReader reader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog"); 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("Summary: {0}", item.Summary.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("Summary: {0}", ((TextSyndicationContent)item.Summary).Text); }
範例
以下是這個範例的完整程式碼清單。
Imports System
Imports System.Xml
Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.ServiceModel.Syndication
Imports System.ServiceModel.Web
Imports System.Collections.ObjectModel
Imports System.Collections.Generic
<ServiceContract()> _
Public Interface IBlog
<OperationContract()> _
<WebGet> _
Function GetBlog() As Rss20FeedFormatter
End Interface
Public Class BlogService
Implements IBlog
Public Function GetBlog() As Rss20FeedFormatter Implements IBlog.GetBlog
Dim feed As SyndicationFeed = 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 how to sample that demonstrates how to expose a feed using RSS with WCF")
Dim item1 As SyndicationItem = New SyndicationItem( _
"Item One", _
"This is the content for item one", _
New Uri("https://localhost/Content/One"), _
"ItemOneID", _
DateTime.Now)
Dim item2 As SyndicationItem = New SyndicationItem( _
"Item Two", _
"This is the content for item two", _
New Uri("https://localhost/Content/Two"), _
"ItemTwoID", _
DateTime.Now)
Dim item3 As SyndicationItem = 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
Return New Rss20FeedFormatter(feed)
End Function
End Class
Module Program
Sub Main()
Dim baseAddress As New Uri("https://localhost:8000/BlogService")
Dim svcHost As New WebServiceHost(GetType(BlogService), baseAddress)
Try
svcHost.Open()
Console.WriteLine("Service is running")
Dim reader As XmlReader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog")
Dim feed As SyndicationFeed = SyndicationFeed.Load(reader)
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("Summary: {0}", item.Summary.Text)
Next
Console.WriteLine("Press <enter> to quit...")
Console.ReadLine()
svcHost.Close()
Catch ce As CommunicationException
Console.WriteLine("An exception occurred: {0}", ce.Message)
svcHost.Abort()
End Try
End Sub
End Module
using System;
using System.Xml;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.Collections.ObjectModel;
using System.Collections.Generic;
namespace Service
{
[ServiceContract]
public interface IBlog
{
[OperationContract]
[WebGet]
Rss20FeedFormatter GetBlog();
}
public class BlogService : IBlog
{
public Rss20FeedFormatter GetBlog()
{
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 how to sample that demonstrates how to expose a feed using RSS 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;
return new Rss20FeedFormatter(feed);
}
}
public class Host
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("https://localhost:8000/BlogService");
WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), baseAddress);
try
{
svcHost.Open();
Console.WriteLine("Service is running");
XmlReader reader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog");
SyndicationFeed feed = SyndicationFeed.Load(reader);
Console.WriteLine(feed.Title.Text);
Console.WriteLine("Items:");
foreach (SyndicationItem item in feed.Items)
{
Console.WriteLine("Title: {0}", item.Title.Text);
Console.WriteLine("Summary: {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。