Welcome to MSDN Blogs Sign in | Join | Help

Days with .NET Framework

.NET Framework 担当のプロダクトマーケティング 新村剛史の日記です。
WCF で実現する RSS/ATOM のフィード

皆さんこんにちは

今回は、DEMOフォローシリーズで RSS/ATOM のフィードを WCF で実現する方法を解説します。今回はRSS/ATOM のフィードということで、通常のサービスとは少し違うと感じる方もいらっしゃるかと思いますが、WCF の基本の部分に関しては同じプログラミングモデルで実現することが可能です。今回の DEMO ではRSS 2.0 形式でのフィードを作成します。

それではコードの解説に入っていきたいと思います。まずはContract です。using で System.ServiceModel.Syndication 名前空間を設定します。この名前空間は Syndication の名前が示すとおり フィード関係の機能を提供します。ServiceContract 属性に加えて、ServiceKnownType 属性で シリアル化するデータの型を指定します。今回は RSS 2.0 形式なので、Rss20FeedFormatter を指定しています。 もし、ATOM 形式を選択する場合は Atom10FeedFormatter を指定します。 また、WebGet 属性で UriTemolate に加えて BodyStyle を 設定しています。これは WebMessageBodyStyle 列挙体 を用いてパラメータやデータをどのようにラップするかを設定するものです。今回は何もラップしないので Bare を指定します。

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Syndication;

namespace WCFFeedDemo
{  
    [ServiceKnownType(typeof(Rss20FeedFormatter))]
    [ServiceContract]
    public interface IFeedService
    {
        [OperationContract]
        [WebGet(UriTemplate = "*", BodyStyle = WebMessageBodyStyle.Bare)]
        SyndicationFeedFormatter<SyndicationFeed> GetFeed();
    }
}

つづいて、web.config です。 今回の DEMO に関しては REST スタイルなWCFの際と同じような web.config になります。

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="rssSyndicationBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="WCFFeedDemo.FeedService">
        <endpoint
          address="/rss/"
          behaviorConfiguration="rssSyndicationBehavior"
          binding="webHttpBinding"
          contract="WCFFeedDemo.IFeedService" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

そして実装ですが、 今回は FeedService.svc という svc を使用します(別に名前は何でもかまいません)ので FeedService.svc.sc に実装を記述します。まず、フィードそのものを表現する型として SyndicationFeed のインスタンスを作成し、フィードの基本情報を設定します。この際、文字列は TextSyndicatonContent クラスを使用して、URLは SyndicationLink クラスを使用して表現されます。また、 SyndicationItem クラスを使用してItem を表現します。この関係がわかりづらい場合は、Blog にたとえるとSyndicationFeed はBlog そのもののフィードで、SyndicationItem はBlog のそれぞれのエントリになります。最終的に、複数の Item を List にしてSyndicationFeed のItems というパラメータに設定することでフィードが完成します。あとは、SyndicationFeed の GetRss20Formatter というメソッドを使用して RSS 2.0 形式のフィードを返すようにします。

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Collections.Generic;
using System.ServiceModel.Syndication; namespace WCFFeedDemo
{
    public class FeedService : IFeedService
    {
        public SyndicationFeedFormatter<SyndicationFeed> GetFeed()
        {
            //フィードの基本情報を設定
            SyndicationFeed feed = new SyndicationFeed();
            feed.Title = new TextSyndicationContent(".NET Framework 3.5 Feed");
            feed.Links.Add(new SyndicationLink(new Uri("http://example.com/")));
 
            //フィードの各アイテムの情報を設定
            SyndicationItem item = new SyndicationItem();
            item.Title = new TextSyndicationContent("WCF で RSS フィード");
            item.Summary = new TextSyndicationContent("RSSフィードについて説明");
            item.Content = new TextSyndicationContent("WCF では System.ServiceModel.Syndication を使って…");
            item.PublishDate = DateTime.Now;
            item.LastUpdatedTime = DateTime.Now;
 
            feed.Items = new List<SyndicationItem> { item };
 
            return feed.GetRss20Formatter();
        }
    }
}

実際にフィードを行う際にはどのような情報をどのように抽出するのかが鍵となるわけですが、少なくとも WCF をつかうことでフィードを配信するという部分は非常に簡単になります。現状では一般的にBlog や ニュースがフィードの対象として見られていますが、その辺は色々と創意工夫をすることでフィードの用途というものは広がっていくと思います。ぜひ、色々と試してみてください。

Posted: Tuesday, October 16, 2007 12:21 PM by tashinmu

Comments

Anonymous comments are disabled
Page view tracker