独立诊断源示例

此示例演示如何创建一个 RSS/Atom 源,以便与 Windows Communication Foundation (WCF) 整合。它是一个基本的“Hello World”程序,演示了对象模型的基础知识以及如何在 Windows Communication Foundation (WCF) 服务上设置对象模型。

提示

此示例需要安装 .NET Framework 3.5 版才能生成和运行。若要打开项目和解决方案文件,需要使用 Visual Studio 2008。

WCF 以服务操作的形式建立整合源的模型,这些服务操作返回一个特殊的数据类型:SyndicationFeedFormatterSyndicationFeedFormatter 的实例可以将源序列化为 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);
    }

使用 WebGetAttribute 属性对 GetProcesses 操作进行批注,该属性使您能够控制 WCF 如何向服务操作发出 HTTP GET 请求并指定所发送消息的格式。

如同任何 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 的客户端来访问此服务。例如,可以通过在识别 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. Windows Communication Foundation 示例的一次性安装过程中的安装说明中介绍的计算机上,确保您对于 HTTP 和 HTTPS 具有正确的地址注册权限。

  2. 生成解决方案。

  3. 运行控制台应用程序。

  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.