方法 : 基本的な RSS フィードを作成する

Windows Communication Foundation (WCF) では、配信フィードを公開するサービスを作成できます。ここでは、RSS 配信フィードを公開する配信サービスを作成する方法について説明します。

基本的な配信サービスを作成するには

  1. WebGetAttribute 属性でマークされたインターフェイスを使用して、サービス コントラクトを定義します。配信フィードとして公開される各操作は、Rss20FeedFormatter オブジェクトを返す必要があります。

    <ServiceContract()> _
    Public Interface IBlog
        <OperationContract()> _
    <WebGet> _
    Function GetBlog() As Rss20FeedFormatter
    End Interface
    
    [ServiceContract]
    public interface IBlog
    {
        [OperationContract]
        [WebGet]
        Rss20FeedFormatter GetBlog();
    }
    
    Bb412174.note(ja-jp,VS.100).gif注 :
    WebGetAttribute 属性を適用するすべてのサービス操作は、HTTP GET 要求にマッピングされます。他の HTTP メソッドに操作をマッピングするには、代わりに WebInvokeAttribute を使用します。詳細については、次のトピックを参照してください。、「方法 : 基本的な WCF Web HTTP サービスを作成する」を参照してください。

  2. サービス コントラクトを実装します。

    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);
        }
    }
    
  3. 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");
    
  4. 複数の 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);
    
  5. 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;
    
  6. フィードを返します。

    Return New Rss20FeedFormatter(feed)
    
    return new Rss20FeedFormatter(feed);
    

サービスをホストするには

  1. 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);
    
  2. サービス ホストを開き、ユーザーが 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() を呼び出すには

  1. Internet Explorer を開いて「https://localhost:8000/BlogService/GetBlog」という URL を入力し、Enter キーを押します。この URL には、サービスのベース アドレス (https://localhost:8000/BlogService)、エンドポイントの相対アドレス、および呼び出すサービス操作が含まれます。

コードから GetBlog() を呼び出すには

  1. ベース アドレスと呼び出すメソッドを使用して XmlReader を作成します。

    Dim serviceAddress As New Uri("https://localhost:8000/BlogService/GetBlog")
    
    Uri serviceAddress = new Uri("https://localhost:8000/BlogService/GetBlog");
    
  2. 静的な 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 に設定されます。

  3. フィード オブジェクトにアクセスします。

    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 が参照されます。

参照

リファレンス

WebHttpBinding
WebGetAttribute