共用方式為


獨立診斷摘要範例

這個範例示範如何使用 Windows Communication Foundation (WCF) 建立新聞訂閱的 RSS/Atom 摘要。它是基本的 "Hello World" 程式,可顯示物件模型的基礎,以及在 Windows Communication Foundation (WCF) 服務上的設定方式。

Bb410776.note(zh-tw,VS.90).gif注意:
要建置和執行這個範例,必須安裝 .NET Framework version 3.5。要開啟專案和方案檔,必須要有 Visual Studio 2008。

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 作業會以 WebGetAttribute 屬性來註解,這個屬性可讓您控制 WCF 將 HTTP GET 要求分派至服務作業的方式,以及指定所傳送訊息的格式。

就像任何的 WCF 服務一樣,新聞訂閱摘要可以自我裝載於任何 Managed 應用程式中。新聞訂閱服務需要指定的繫結 (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 感知用戶端來存取服務。例如,您可以透過使用 RSS 感知瀏覽器 (如 Internet Explorer 7) 巡覽至 https://localhost:8000/diagnostics/feed/?format=atom 或 https://localhost:8000/diagnostics/feed/?format=rss,來檢視這個服務的輸出。

您也可以使用Syndication Object Model讀取新聞訂閱資料,並使用命令式程式碼來處理資料。

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. 確定您有電腦上 HTTP 和 HTTPS 的正確位址註冊權限 (如 Windows Communication Foundation 範例的單次安裝程序中的安裝指示所述)。

  2. 建置方案。

  3. 執行主控台應用程式 (Console Application)。

  4. 執行主控台應用程式時,使用 RSS 感知瀏覽器巡覽至 https://localhost:8000/diagnostics/feed/?format=atom 或 https://localhost:8000/diagnostics/feed/?format=rss。

請參閱

其他資源

Web Programming Model
WCF Syndication

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.