そういえば、WCF で RSS の配信ができるんだったなぁ。
まだ試していなかったので、超簡単なサンプルを作ってみました。
今更ですが。
今回もコンソールアプリで作ります。まずは次の2つを参照に追加。
- System.ServiceModel
- System.ServiceModel.Web
次はサービスコントラクトを定義。
[ServiceContract] public interface IMyRss { [OperationContract] [WebGet] Rss20FeedFormatter GetFeed(); }
そしてサービス実装。
public class MyRss : IMyRss { public Rss20FeedFormatter GetFeed() { SyndicationItem item1 = new SyndicationItem( "最初の記事。", "最初の記事。", new Uri("http://localhost:8000/MyRss/Contents/1")); SyndicationItem item2 = new SyndicationItem( "2番目の記事。", "2番目の記事。", new Uri("http://localhost:8000/MyRss/Contents/2")); SyndicationItem item3 = new SyndicationItem( "3番目の記事。", "3番目の記事。", new Uri("http://localhost:8000/MyRss/Contents/3")); SyndicationFeed feed = new SyndicationFeed("Nakamura Blog", "Nakamura .Text Powered Blog", new Uri("http://localhost:8000/MyRss")); feed.Authors.Add(new SyndicationPerson("nakamura@wankuma.com")); feed.Categories.Add(new SyndicationCategory("雑記")); feed.Description = new TextSyndicationContent("雑記です。"); feed.Items = new List<SyndicationItem> { item1, item2, item3 }; return new Rss20FeedFormatter(feed); } }
ホストを実装。
class Program { static void Main(string[] args) { WebServiceHost host = new WebServiceHost(typeof(MyRss)); host.Open(); Console.WriteLine("サービス起動。終了するには Enter を押して下さい。"); Console.ReadLine(); host.Close(); } }
最後に、構成ファイルでエンドポイントを指定。
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="WcfSyndication.MyRss"> <endpoint name="MyRss" address="http://localhost:8000/MyRss/" binding="webHttpBinding" contract="WcfSyndication.IMyRss" /> </service> </services> </system.serviceModel> </configuration>
おおっ!簡単ですね。