Windows Communication Foundation (WCF) を使用すると、配信フィードを公開するサービスを作成できます。 このトピックでは、Atom 1.0 と RSS 2.0 の両方を使用して配信フィードを公開する配信サービスを作成する方法について説明します。 このサービスは、いずれかの配信形式を返すことができる 1 つのエンドポイントを公開します。 わかりやすくするために、このサンプルで使用されるサービスはセルフホステッドです。 運用環境では、この種類のサービスは IIS または WAS でホストされます。 さまざまな WCF ホスティング オプションの詳細については「ホスティング」を参照してください。
基本的なシンジケーション サービスを作成するには
#D0 属性でマークされたインターフェイスを使用して、サービス コントラクトを定義します。 各操作がシンジケートフィードとして公開されると、SyndicationFeedFormatter オブジェクトを返します。 WebGetAttribute のパラメーターに注意してください。
UriTemplate
は、このサービス操作の呼び出しに使用する URL を指定します。 このパラメーターの文字列には、リテラルと中括弧 ({format}) の変数が含まれています。 この変数は、サービス操作の #D0 パラメーターに対応します。 詳細については、UriTemplateを参照してください。BodyStyle
は、このサービス操作が送受信するメッセージの書き込み方法に影響します。 #D0 は、このサービス操作との間で送受信されるデータが、インフラストラクチャで定義された 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 を使用して、このインターフェイスのサービス操作によって返される型を指定します。
サービス コントラクトを実装します。
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
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")
複数の 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)
フィードに 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
要求された形式を返すには、format パラメーターを使用します。
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
サービスをホストするには
WebServiceHost オブジェクトを作成します。 #D0 クラスは、コードまたは構成で指定されていない限り、サービスのベース アドレスにエンドポイントを自動的に追加します。 このサンプルでは、エンドポイントが指定されていないため、既定のエンドポイントが公開されます。
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)
サービス ホストを開き、サービスからフィードを読み込み、フィードを表示して、ユーザーが 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: {item.Title.Text}"); Console.WriteLine($"Content: {((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: {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: {((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 を呼び出すには
ブラウザーを開き、次の URL を入力し、Enterキーを押します:
http://localhost:8000/BlogService/GetBlog
。URL には、サービスのベース アドレス (#A0)、エンドポイントの相対アドレス、および呼び出すサービス操作が含まれています。
コードから GetBlog() を呼び出すには
ベース アドレスと呼び出すメソッドを使って 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")
静的な Load(XmlReader) メソッドを呼び出し、作成した XmlReader を渡します。
SyndicationFeed feed = SyndicationFeed.Load(reader);
Dim feed As SyndicationFeed = SyndicationFeed.Load(atomReader)
これにより、サービス操作が呼び出され、サービス操作から返されたフォーマッタが新しい SyndicationFeed に設定されます。
フィード オブジェクトにアクセスします。
Console.WriteLine(feed.Title.Text); Console.WriteLine("Items:"); foreach (SyndicationItem item in feed.Items) { Console.WriteLine($"Title: {item.Title.Text}"); Console.WriteLine($"Content: {((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: {item.Title.Text}");
Console.WriteLine($"Content: {((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: {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: {((TextSyndicationContent)item.Summary).Text}");
}
Console.WriteLine("Press <ENTER> to quit...");
Console.ReadLine();
svcHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine($"An exception occurred: {ce.Message}");
svcHost.Abort();
}
}
}
}
コードのコンパイル
上記のコードをコンパイルするときは、System.ServiceModel.dll を参照し、System.ServiceModel.Web.dllします。