Partilhar via


Stand-Alone Diagnostics Feed Sample

Download sample

This sample demonstrates how to create an RSS/Atom feed for syndication with Windows Communication Foundation (WCF). It is a basic "Hello World" program that shows the basics of the object model and how to set it up on a Windows Communication Foundation (WCF) service.

Note

This sample requires that .NET Framework version 3.5 is installed to build and run. Visual Studio 2008 is required to open the project and solution files.

WCF models syndication feeds as service operations that return a special data type, SyndicationFeedFormatter. Instances of SyndicationFeedFormatter can serialize a feed into both the RSS 2.0 and Atom 1.0 formats. The following sample code shows the contract used.

[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);
    }

The GetProcesses operation is annotated with the WebGetAttribute attribute that enables you to control how WCF dispatches HTTP GET requests to service operations and specify the format of the messages sent.

Like any WCF service, syndication feeds can be self hosted in any managed application. Syndication services require a specific binding (the WebHttpBinding) and a specific endpoint behavior (the WebHttpBehavior) to function correctly. The new WebServiceHost class provides a convenient API for creating such endpoints without specific configuration.

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)

Alternatively, you can use WebServiceHostFactory from within an IIS-hosted .svc file to provide equivalent functionality (this technique is not demonstrated in this sample code).

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

Because this service receives requests using the standard HTTP GET, you can use any RSS or ATOM-aware client to access the service. For example, you can view the output of this service by navigating to https://localhost:8000/diagnostics/feed/?format=atom or https://localhost:8000/diagnostics/feed/?format=rss in an RSS-aware browser such as Internet Explorer 7.

You can also use the Syndication Object Model to read syndicated data and process it using imperative code.

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());
}

To set up, build, and run the sample

  1. Ensure that you have the right address registration permission for HTTP and HTTPS on the computer as explained in the set up instructions in One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. Build the solution.

  3. Run the console application.

  4. While the console application is running, navigate to https://localhost:8000/diagnostics/feed/?format=atom or https://localhost:8000/diagnostics/feed/?format=rss using an RSS-aware browser.

See Also

Other Resources

Web Programming Model
WCF Syndication

© 2007 Microsoft Corporation. All rights reserved.