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> _
        Function GetBlog() As Atom10FeedFormatter
    End Interface
    

    System_CAPS_ICON_note.jpg 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, seeHow to: Create a Basic WCF Web HTTP Service.

  2. Implement the service contract.

    Public Class BlogService
        Implements IBlog
        Public Function GetBlog() As Atom10FeedFormatter Implements IBlog.GetBlog
            Dim feed As 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 imports ATOM with WCF")
    
            Dim item1 As New SyndicationItem( _
                "Item One", _
                "This is the content for item one", _
                New Uri("http://localhost/Content/One"), _
                "ItemOneID", _
                DateTime.Now)
    
            Dim item2 As New SyndicationItem( _
                "Item Two", _
                "This is the content for item two", _
                New Uri("http://localhost/Content/Two"), _
                "ItemTwoID", _
                DateTime.Now)
    
            Dim item3 As New SyndicationItem( _
                "Item Three", _
                "This is the content for item three", _
                New Uri("http://localhost/Content/three"), _
                "ItemThreeID", _
                DateTime.Now)
            Dim items As New List(Of SyndicationItem)()
    
            items.Add(item1)
            items.Add(item2)
            items.Add(item3)
    
            feed.Items = items
            Return New Atom10FeedFormatter(feed)
        End Function
    End Class
    

  3. Create a SyndicationFeed object and add an author, category, and description.

            Dim feed As 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 imports ATOM with WCF")
    

  4. Create several SyndicationItem objects.

            Dim item1 As New SyndicationItem( _
                "Item One", _
                "This is the content for item one", _
                New Uri("http://localhost/Content/One"), _
                "ItemOneID", _
                DateTime.Now)
    
            Dim item2 As New SyndicationItem( _
                "Item Two", _
                "This is the content for item two", _
                New Uri("http://localhost/Content/Two"), _
                "ItemTwoID", _
                DateTime.Now)
    
            Dim item3 As 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.

            Dim items As New List(Of 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.

            Dim baseAddress As New Uri("http://localhost:8000/BlogService/")
            Dim svcHost As New WebServiceHost(GetType(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")
    
                Dim reader As XmlReader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog")
                Dim feed As SyndicationFeed = SyndicationFeed.Load(reader)
                Console.WriteLine(feed.Title.Text)
                Console.WriteLine("Items:")
    
                For Each item As SyndicationItem In feed.Items
                    Console.WriteLine("Title: {0}", item.Title.Text)
                    Console.WriteLine("Content: {0}", item.Title.Text)
                Next
    
                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: http://localhost:8000/BlogService/GetBlog

    The URL contains the base address of the service (http://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.

            Dim serviceAddress As New Uri("http://localhost:8000/BlogService/GetBlog")
    

  2. Call the static Load(XmlReader) method, passing in the XmlReader you just created.

            Dim feed As SyndicationFeed = SyndicationFeed.Load(serviceAddress)
    

    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:")
            For Each item As SyndicationItem In feed.Items
                Console.WriteLine("Title: {0}", item.Title.Text)
                Console.WriteLine("Summary: {0}", item.Summary.Text)
            Next
    

The following is the full code listing for this example.

Imports System
Imports System.Xml
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.ServiceModel.Syndication
Imports System.ServiceModel.Channels
Imports System.ServiceModel.Web

<ServiceContract()> _
Public Interface IBlog
    <OperationContract()> _
    <WebGet> _
    Function GetBlog() As Atom10FeedFormatter
End Interface

Public Class BlogService
    Implements IBlog
    Public Function GetBlog() As Atom10FeedFormatter Implements IBlog.GetBlog
        Dim feed As 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 imports ATOM with WCF")

        Dim item1 As New SyndicationItem( _
            "Item One", _
            "This is the content for item one", _
            New Uri("http://localhost/Content/One"), _
            "ItemOneID", _
            DateTime.Now)

        Dim item2 As New SyndicationItem( _
            "Item Two", _
            "This is the content for item two", _
            New Uri("http://localhost/Content/Two"), _
            "ItemTwoID", _
            DateTime.Now)

        Dim item3 As New SyndicationItem( _
            "Item Three", _
            "This is the content for item three", _
            New Uri("http://localhost/Content/three"), _
            "ItemThreeID", _
            DateTime.Now)
        Dim items As New List(Of SyndicationItem)()

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

        feed.Items = items
        Return New Atom10FeedFormatter(feed)
    End Function
End Class

Module Program

    Sub Main()
        Dim baseAddress As New Uri("http://localhost:8000/BlogService/")
        Dim svcHost As New WebServiceHost(GetType(BlogService), baseAddress)
        Try
            svcHost.AddServiceEndpoint(GetType(IBlog), New WebHttpBinding(), "")
            svcHost.Open()
            Console.WriteLine("Service is running")

            Dim reader As XmlReader = XmlReader.Create("http://localhost:8000/BlogService/GetBlog")
            Dim feed As SyndicationFeed = SyndicationFeed.Load(reader)
            Console.WriteLine(feed.Title.Text)
            Console.WriteLine("Items:")

            For Each item As SyndicationItem In feed.Items
                Console.WriteLine("Title: {0}", item.Title.Text)
                Console.WriteLine("Content: {0}", item.Title.Text)
            Next

            Console.WriteLine("Press <ENTER> to quit...")
            Console.ReadLine()
            svcHost.Close()

        Catch ce As CommunicationException
            Console.WriteLine("An exception occured: {0}", ce.Message)
            svcHost.Abort()
        End Try
    End Sub

End Module

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

WebHttpBinding
WebGetAttribute

Show: