Monday, September 7, 2009

Syndication Extensibility

Syndication Extensibility
The Syndication API is designed to provide a format-neutral programming model that allows syndicated content to be written to the wire in a variety of formats. The abstract data model consists of the following classes:
These classes map closely to the constructs defined in the Atom 1.0 specification, although some of the names are different.
A key feature of syndication protocols is extensibility. Both Atom 1.0 and RSS 2.0, add attributes and elements to syndication feeds that are not defined in the specifications. The Windows Communication Foundation (WCF) syndication programming model provides the following ways of working with custom attributes and extensions, loosely-typed access and deriving a new class.

Loosely Typed Access

Adding extensions by deriving a new class requires writing additional code. Another option is accessing extensions in a loosely-typed way. All of the types defined in the syndication abstract data model contain properties named AttributeExtensions and ElementExtensions (with one exception, SyndicationContent has an AttributeExtensions property but no ElementExtensions property). These properties are collections of extensions not processed by the TryParseAttribute and TryParseElement methods respectively. You can access these unprocessed extensions by calling System.ServiceModel.Syndication.SyndicationElementExtensionCollection.ReadElementExtensions.String,System.String) on the ElementExtensions property of SyndicationFeed, SyndicationItem, SyndicationLink, SyndicationPerson, and SyndicationCategory. This set of methods finds all extensions with the specified name and namespace, deserializes them individually into instances of TExtension and returns them as a collection of TExtension objects.

Deriving a New Class

You can derive a new class from any of the existing abstract data model classes. Do this when implementing an application in which most of the feeds you are working with have a particular extension. In this topic, most of the feeds that the program works with contain a MyExtension extension. To provide an improved programming experience, do the following steps:
  • Create a class to hold the extension data. In this case, create a class called MyExtension.

  • Derive a class called MyExtensionItem from SyndicationItem to expose a property of type MyExtension for programmability purposes.

  • Override TryParseElement in the MyExtensionItem class to instantiate a new MyExtension instance when a MyExtension is read in.

  • Override WriteElementExtensions in the MyExtensionItem class to write the contents of the MyExtension property to an XML writer.

  • Derive a class called MyExtensionFeed from SyndicationFeed.

  • Override CreateItem in the MyExtensionFeed class to instantiate a MyExtensionItem instead of the default SyndicationItem. A series of methods are defined in SyndicationFeed and SyndicationItem that can create SyndicationLink, SyndicationCategory, and SyndicationPerson objects (for example, CreateLink, CreateCategory, and CreatePerson). All of which can be overridden to create a custom derived class.



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

How to: Expose a Feed as both Atom and RSS

How to: Expose a Feed as both Atom and RSS
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 a syndication feed using both Atom 1.0 and RSS 2.0. This service exposes one endpoint that can return either syndication format. For simplicity the service used in this sample is self hosted. In a production environment a service of this type would be hosted under IIS or WAS. For more information about the different WCF hosting options, see Hosting of Windows Communication Foundation Services.

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 returns a SyndicationFeedFormatter object. Note the parameters for the WebGetAttribute. UriTemplate specifies the URL used to invoke this service operation. The string for this parameter contains literals and a variable in braces ({format}). This variable corresponds to the service operation's format parameter. For more information, see UriTemplate. BodyStyle affects how the messages that this service operation sends and receives are written. Bare specifies that the data sent to and from this service operation are not wrapped by infrastructure-defined XML elements. For more information, see WebMessageBodyStyle.
    Visual Basic

     _
    GetType(Atom10FeedFormatter))> _
    GetType(Rss20FeedFormatter))> _
    Public Interface IBlog
         _
        "GetBlog?format={format}")> _
        Function GetBlog(ByVal format As String) As SyndicationFeedFormatter
    End Interface
    C#

    [ServiceContract]
    [ServiceKnownType(typeof(Atom10FeedFormatter))]
    [ServiceKnownType(typeof(Rss20FeedFormatter))]
    public interface IBlog
    {
        [OperationContract]
        [WebGet(UriTemplate = "GetBlog?format={format}")]
        SyndicationFeedFormatter GetBlog(string format);
    }
    Bb412205.note(en-us,VS.90).gifNote:
    Use the ServiceKnownTypeAttribute to specify the types that are returned by the service operations in this interface.
  2. Implement the service contract.
    Visual Basic

    Public Class BlogService
        implements IBlog
    
        Public Function GetBlog(ByVal format As String) As SyndicationFeedFormatter Implements IBlog.GetBlog
            Dim feed As New SyndicationFeed("My Blog Feed", "This is a test feed", New Uri("http://SomeURI"))
            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 demonstrates how to expose a feed through RSS and 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
    
            If (format = "rss") Then
                Return New Rss20FeedFormatter(feed)
            Else
                Return New Atom10FeedFormatter(feed)
            End If
        End Function
    End Class
    C#

    public class BlogService : IBlog
    {
        public SyndicationFeedFormatter GetBlog(string format)
        {
            SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI"));
            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 demonstrates how to expose a feed through RSS and 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;
    
            if (format == "rss")
                return new Rss20FeedFormatter(feed);
            else if (format == "atom")
                return new Atom10FeedFormatter(feed);
            else return null;
        }
    }
  3. Create a SyndicationFeed object and add an author, category, and description.
    Visual Basic

    Dim feed As New SyndicationFeed("My Blog Feed", "This is a test feed", New Uri("http://SomeURI"))
    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 demonstrates how to expose a feed through RSS and Atom with WCF")
    C#

    SyndicationFeed feed = new SyndicationFeed("My Blog Feed", "This is a test feed", new Uri("http://SomeURI"));
    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 demonstrates how to expose a feed through RSS and Atom with WCF");
  4. Create several SyndicationItem objects.
    Visual Basic

    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)
    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.
    Visual Basic

    Dim items As New List(Of SyndicationItem)()
    items.Add(item1)
    items.Add(item2)
    items.Add(item3)
    
    feed.Items = items
    C#

    List items = new List();
    items.Add(item1);
    items.Add(item2);
    items.Add(item3);
    
    feed.Items = items;
  6. Use the format parameter to return the requested format.
    Visual Basic

    If (format = "rss") Then
        Return New Rss20FeedFormatter(feed)
    Else
        Return New Atom10FeedFormatter(feed)
    End If



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