次の方法で共有


Stand-Alone 診断フィードのサンプル

DiagnosticsFeed サンプルでは、Windows Communication Foundation (WCF) を使用して配信用の RSS/Atom フィードを作成する方法を示します。 これは、オブジェクト モデルの基本と、Windows Communication Foundation (WCF) サービスで設定する方法を示す基本的な "Hello World" プログラムです。

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("http://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 対応クライアントを使用してサービスにアクセスできます。 たとえば、RSS 対応ブラウザーで http://localhost:8000/diagnostics/feed/?format=atom または http://localhost:8000/diagnostics/feed/?format=rss に移動して、このサービスの出力を表示できます。

WCF シンジケーション オブジェクト モデルを Atom および RSS にマップして配信データを読み取り、命令型コードを使用して処理する方法を使用することもできます。

XmlReader reader = XmlReader.Create( "http://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 サンプルのセットアップ手順のセットアップ手順の説明に従って、コンピューター上の HTTP と HTTPS に対する適切なアドレス登録アクセス許可One-Time 持っていることを確認します。

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

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

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

こちらも参照ください