How to: Create a Basic Atom Feed

Windows Communication Foundation (WCF) allows you to create a service that exposes a syndication feed. This topic discusses how to create a syndication service that exposes an Atom syndication feed.

To create a basic syndication service

  1. Define a service contract using an interface marked with the WebGetAttribute attribute. Each operation that is exposed as a syndication feed should return a Atom10FeedFormatter object.

    [ServiceContract]
    public interface IBlog
    {
        [OperationContract]
        [WebGet]
        Atom10FeedFormatter GetBlog();
    }
    

    Note

    All service operations that apply the WebGetAttribute are mapped to HTTP GET requests. To map your operation to a different HTTP method, use the WebInvokeAttribute instead. For more information, see How to: Create a Basic WCF REST Service.

  2. Implement the service contract.

    public class BlogService : IBlog
    {
        public Atom10FeedFormatter GetBlog()
        {
            SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI"), "FeedOneID", new DateTimeOffset(DateTime.Now));
            feed.Authors.Add(new SyndicationPerson("someone@microsoft.com"));
            feed.Categories.Add(new SyndicationCategory("How To Sample Code"));
            feed.Description = new TextSyndicationContent("This is a sample that illistrates how to expose a feed using ATOM with WCF");
    
            SyndicationItem item1 = new SyndicationItem(
                "Item One",
                "This is the content for item one",
                new Uri("https://localhost/Content/One"),
                "ItemOneID",
                DateTime.Now);
    
            SyndicationItem item2 = new SyndicationItem(
                "Item Two",
                "This is the content for item two",
                new Uri("https://localhost/Content/Two"),
                "ItemTwoID",
                DateTime.Now);
    
            SyndicationItem item3 = new SyndicationItem(
                "Item Three",
                "This is the content for item three",
                new Uri("https://localhost/Content/three"),
                "ItemThreeID",
                DateTime.Now);
            List<SyndicationItem> items = new List<SyndicationItem>();
    
            items.Add(item1);
            items.Add(item2);
            items.Add(item3);
    
            feed.Items = items;
            return new Atom10FeedFormatter(feed);
        }
    }
    
  3. Create a SyndicationFeed object and add an author, category, and description.

    SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI"), "FeedOneID", new DateTimeOffset(DateTime.Now));
    feed.Authors.Add(new SyndicationPerson("someone@microsoft.com"));
    feed.Categories.Add(new SyndicationCategory("How To Sample Code"));
    feed.Description = new TextSyndicationContent("This is a sample that illistrates how to expose a feed using ATOM with WCF");
    
  4. Create several SyndicationItem objects.

    SyndicationItem item1 = new SyndicationItem(
        "Item One",
        "This is the content for item one",
        new Uri("https://localhost/Content/One"),
        "ItemOneID",
        DateTime.Now);
    
    SyndicationItem item2 = new SyndicationItem(
        "Item Two",
        "This is the content for item two",
        new Uri("https://localhost/Content/Two"),
        "ItemTwoID",
        DateTime.Now);
    
    SyndicationItem item3 = new SyndicationItem(
        "Item Three",
        "This is the content for item three",
        new Uri("https://localhost/Content/three"),
        "ItemThreeID",
        DateTime.Now);
    
  5. Add the SyndicationItem objects to the feed.

    List<SyndicationItem> items = new List<SyndicationItem>();
    
    items.Add(item1);
    items.Add(item2);
    items.Add(item3);
    
    feed.Items = items;
    
  6. Return the feed.

    return new Atom10FeedFormatter(feed);
    

To host the service

  1. Create a WebServiceHost object.

    Uri baseAddress = new Uri("https://localhost:8000/BlogService/");
    WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), baseAddress);
    
  2. Open the service host, load the feed from the service, display the feed, and wait for the user to press ENTER.

    svcHost.Open();
    Console.WriteLine("Service is running");
    
    XmlReader reader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog");
    SyndicationFeed feed = SyndicationFeed.Load(reader);
    Console.WriteLine(feed.Title.Text);
    Console.WriteLine("Items:");
    foreach (SyndicationItem item in feed.Items)
    {
        Console.WriteLine("Title: {0}", item.Title.Text);
        Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Content).Text);
    }
    Console.WriteLine("Press <ENTER> to quit...");
    Console.ReadLine();
    svcHost.Close();
    

To call GetBlog() with an HTTP GET

  1. Open Internet Explorer, type the following URL, and press ENTER: https://localhost:8000/BlogService/GetBlog

    The URL contains the base address of the service (https://localhost:8000/BlogService), the relative address of the endpoint, and the service operation to call.

To call GetBlog() from code

  1. Create a XmlReader with the base address and the method you are calling.

    XmlReader reader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog");
    
  2. Call the static Load method, passing in the XmlReader you just created.

    SyndicationFeed feed = SyndicationFeed.Load(reader);
    

    This invokes the service operation and populates a new SyndicationFeed with the formatter returned from the service operation.

  3. Access the feed object.

    Console.WriteLine(feed.Title.Text);
    Console.WriteLine("Items:");
    foreach (SyndicationItem item in feed.Items)
    {
        Console.WriteLine("Title: {0}", item.Title.Text);
        Console.WriteLine("Summary: {0}", ((TextSyndicationContent)item.Summary).Text);
    }
    

Example

The following is the full code listing for this example.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.Xml;
using System.ServiceModel.Description;
using System.ServiceModel.Syndication;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;

namespace Service
{
    [ServiceContract]
    public interface IBlog
    {
        [OperationContract]
        [WebGet]
        Atom10FeedFormatter GetBlog();
    }

    public class BlogService : IBlog
    {
        public Atom10FeedFormatter GetBlog()
        {
            SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI"), "FeedOneID", new DateTimeOffset(DateTime.Now));
            feed.Authors.Add(new SyndicationPerson("someone@microsoft.com"));
            feed.Categories.Add(new SyndicationCategory("How To Sample Code"));
            feed.Description = new TextSyndicationContent("This is a sample that illistrates how to expose a feed using ATOM with WCF");

            SyndicationItem item1 = new SyndicationItem(
                "Item One",
                "This is the content for item one",
                new Uri("https://localhost/Content/One"),
                "ItemOneID",
                DateTime.Now);

            SyndicationItem item2 = new SyndicationItem(
                "Item Two",
                "This is the content for item two",
                new Uri("https://localhost/Content/Two"),
                "ItemTwoID",
                DateTime.Now);

            SyndicationItem item3 = new SyndicationItem(
                "Item Three",
                "This is the content for item three",
                new Uri("https://localhost/Content/three"),
                "ItemThreeID",
                DateTime.Now);
            List<SyndicationItem> items = new List<SyndicationItem>();

            items.Add(item1);
            items.Add(item2);
            items.Add(item3);

            feed.Items = items;
            return new Atom10FeedFormatter(feed);
        }
    }

    public class Host
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("https://localhost:8000/BlogService/");
            WebServiceHost svcHost = new WebServiceHost(typeof(BlogService), baseAddress);
            try
            {
                svcHost.Open();
                Console.WriteLine("Service is running");

                XmlReader reader = XmlReader.Create("https://localhost:8000/BlogService/GetBlog");
                SyndicationFeed feed = SyndicationFeed.Load(reader);
                Console.WriteLine(feed.Title.Text);
                Console.WriteLine("Items:");
                foreach (SyndicationItem item in feed.Items)
                {
                    Console.WriteLine("Title: {0}", item.Title.Text);
                    Console.WriteLine("Content: {0}", ((TextSyndicationContent)item.Content).Text);
                }
                Console.WriteLine("Press <ENTER> to quit...");
                Console.ReadLine();
                svcHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                svcHost.Abort();
            }
        }
    }
}

Compiling the Code

When compiling the preceding code, reference System.ServiceModel.dll and System.ServiceModel.Web.dll.

See Also

Reference

WebHttpBinding
WebGetAttribute


© 2007 Microsoft Corporation. All rights reserved.
Last Published: 2010-03-21