Monday, September 7, 2009

How to: Create a Basic Atom Feed

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.
    C#

    [ServiceContract]
    public interface IBlog
    {
        [OperationContract]
        [WebGet]
        Atom10FeedFormatter GetBlog();
    }
    Bb412177.note(en-us,VS.90).gifNote:
    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.
    C#

    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("http://localhost/Content/One"),
                "ItemOneID",
                DateTime.Now);
    
            SyndicationItem item2 = new SyndicationItem(
                "Item Two",
                "This is the content for item two",
                new Uri("http://localhost/Content/Two"),
                "ItemTwoID",
                DateTime.Now);
    
            SyndicationItem item3 = new SyndicationItem(
                "Item Three",
                "This is the content for item three",
                new Uri("http://localhost/Content/three"),
                "ItemThreeID",
                DateTime.Now);
            List items = new List();
    
            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.
    C#

    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.
    C#

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

    List items = new List();
    
    items.Add(item1);
    items.Add(item2);
    items.Add(item3);
    
    feed.Items = items;
  6. Return the feed.
    C#

    return new Atom10FeedFormatter(feed);

To host the service




See full details: http://msdn.microsoft.com/en-us/library/bb412177.aspx

No comments: