次の方法で共有


スタンドアロン診断フィードのサンプル

このサンプルでは、Windows Communication Foundation (WCF) を使用して配信用の RSS フィードおよび Atom フィードを作成する方法を示します。このサンプルは基本の "Hello World" プログラムであり、オブジェクト モデルの基本とオブジェクト モデルを Windows Communication Foundation (WCF) サービスにセットアップする方法を示しています。

WCF は、特殊なデータ型 (SyndicationFeedFormatter) を返すサービス操作として配信フィードをモデル化します。SyndicationFeedFormatter のインスタンスは、フィードを RSS 2.0 形式および Atom 1.0 形式の両方にシリアル化できます。使用するコントラクトを次のサンプル コードに示します。

[ServiceContract(Namespace = "")]
    interface IDiagnosticsService
    {
        [OperationContract]
        //The [WebGet] attribute controls how WCF dispatches
        //HTTP requests to service operations based on a URI suffix
        //(the part of the request URI after the endpoint address)
        //using the HTTP GET method. The UriTemplate specifies a relative
        //path of 'feed', and specifies that the format is
        //supplied using a query string. 
        [WebGet(UriTemplate="feed?format={format}")]
        [ServiceKnownType(typeof(Atom10FeedFormatter))]
        [ServiceKnownType(typeof(Rss20FeedFormatter))]
        SyndicationFeedFormatter GetProcesses(string format);
    }

GetProcesses 操作には、WCF が HTTP GET 要求をサービス操作にディスパッチする方法を制御し、送信されるメッセージの形式を指定できるようにする WebGetAttribute 属性で注釈が付けられています。

あらゆる WCF サービスと同様に、配信フィードは、任意のマネージ アプリケーション内での自己ホストが可能です。配信サービスが適切に機能するには、特定のバインディング (WebHttpBinding) と、特定のエンドポイント動作 (WebHttpBehavior) が必要です。新しい WebServiceHost クラスには、特定の構成を使用せずにこのようなエンドポイントを作成する際に便利な API が用意されています。

WebServiceHost host = new WebServiceHost(typeof(ProcessService), new Uri("https://localhost:8000/diagnostics"));

            //The WebServiceHost will automatically provide a default endpoint at the base address
            //using the proper binding (the WebHttpBinding) and endpoint behavior (the WebHttpBehavior)

または、IIS でホストされる .svc ファイルから WebServiceHostFactory を使用して、同等の機能を用意することもできます (この手法は、このサンプル コードでは示されません)。

<%@ ServiceHost Language="C#|VB" Debug="true" Service="ProcessService" %>

このサービスは標準の HTTP GET を使用して要求を受け取るので、サービスへのアクセスには、RSS または ATOM に対応している任意のクライアントを使用できます。たとえば、Internet Explorer 7 などの RSS 対応のブラウザで、https://localhost:8000/diagnostics/feed/?format=atom または https://localhost:8000/diagnostics/feed/?format=rss に移動することで、このサービスの出力を表示できます。

命令型コードを使用して、配信データの読み取りと処理にWCF 配信オブジェクト モデルを Atom や RSS に割り当てる方法を使用することもできます。

XmlReader reader = XmlReader.Create( "https://localhost:8000/diagnostics/feed/?format=rss",
new XmlReaderSettings()
{
//MaxCharactersInDocument can be used to control the maximum amount of data 
//read from the reader and helps prevent OutOfMemoryException
MaxCharactersInDocument = 1024 * 64
} );

SyndicationFeed feed = SyndicationFeed.Load( reader );

foreach (SyndicationItem i in feed.Items)
{
        XmlSyndicationContent content = i.Content as XmlSyndicationContent;
        ProcessData pd = content.ReadContent<ProcessData>();

        Console.WriteLine(i.Title.Text);
        Console.WriteLine(pd.ToString());
}

サンプルを設定、ビルド、および実行するには

  1. Windows Communication Foundation サンプルの 1 回限りのセットアップの手順」のセットアップ手順の説明に従って、コンピュータ上で HTTP および HTTPS の正しいアドレス登録アクセス許可があることを確認します。

  2. ソリューションをビルドします。

  3. コンソール アプリケーションを実行します。

  4. コンソール アプリケーションの実行中に、RSS 対応のブラウザーを使用して https://localhost:8000/diagnostics/feed/?format=atom または https://localhost:8000/diagnostics/feed/?format=rss に移動します。

Bb410776.Important(ja-jp,VS.100).gif 注 :
サンプルは、既にコンピューターにインストールされている場合があります。続行する前に、次の (既定の) ディレクトリを確認してください。

<InstallDrive>:\WF_WCF_Samples

このディレクトリが存在しない場合は、「.NET Framework 4 向けの Windows Communication Foundation (WCF) および Windows Workflow Foundation (WF) のサンプル」にアクセスして、Windows Communication Foundation (WCF) および WF のサンプルをすべてダウンロードしてください。このサンプルは、次のディレクトリに格納されます。

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Syndication\DiagnosticsFeed

参照

その他のリソース

WCF Web HTTP プログラミング モデル
WCF 配信