Share via


Stand-Alone Diagnostics Feed 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.

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 How the WCF Syndication Object Model Maps to Atom and RSS 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 Setup 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.

Bb410776.Important(en-us,VS.100).gif Note:
The samples may already be installed on your computer. Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.

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

See Also

Other Resources

WCF Web HTTP Programming Model
WCF Syndication