How To: Create a RSS Syndication Service with WCF
This post is a step by step guide for creating a Syndication Service with WCF. In this guide, I will create a WCF Service that returns my blogs posts as RSS 2.0 and ATOM items, that can be consumed with an RSS Reader.
Creating the Service Contract
1. Create a new Class Library, and add references to the System.Runtime.Serialization.dll which contains the basic attributes used for serialization, System.ServiceModel.dll which contains the WCF components that shipped with .Net Framework 3.0, and System.ServiceModel.Web.dll which is a .Net Framework 3.5 assembly that contains the components needed for syndication support.
2. Create a new interface called ISyndicationService that has a single method:
public interface ISyndicationService
[WebGet(UriTemplate = "*", BodyStyle = WebMessageBodyStyle.Bare)]
This method returns a SyndicationFeedFormatter, which is an abstract class, that is the base class of Atom10FeedFormatter and Rss20FeedFormatter.
Notice that the service contract is decorated with the ServiceKnownType attribute that allows the service to return subclasses of the SyndicationFeedFormatter.
Implement the Syndication Service
3. Follow the instructions in my recent post: Manipulate Your Blog with MetaBlog API for C# in order to get your blog posts.
4. Create the service implementation class – inherit from ISyndicationService and implement the GetFeeds() method.
In this sample Implementation, I am using the MetaBlog API to get the last 10 posts from my blog, and using the LINQ syntax, I am creating the SyndicationFeed and its list of SyndicationItems.
IBlogger blog = MetaBlogAPIFactory.Create();
Post posts = blog.getRecentPosts("bursteg", "username", "password", 10);
SyndicationFeed feed = new SyndicationFeed
Title = new TextSyndicationContent("Guy Burstein's Blog"),
Description = new TextSyndicationContent("The blog of Guy Burstein"),
Items = from p in posts
select new SyndicationItem
Title = new TextSyndicationContent(p.title),
Content = new TextSyndicationContent(p.description)
return new Rss20FeedFormatter(feed);
Sure, you can implement a more sophisticated service, that takes parameters from the query string and chooses whether to return the Rss20FeedFormatter to the AtomFeed10Formater, selects how many posts to return, of which categories and so on.
Host the Syndication Service
5. There are many ways to host a WCF service, such as self hosting, using IIS, WAS and more. In this tutorial I will use self hosting.
6. Create a new Console Application project in the solution, called Host, and add references to the Syndication Service library and to System.ServiceModel.dll.
7. Set this project to be the startup project of the solution.
8. Add an application configuration file to your solution, and add it with the WCF Service Configuration Editor (Tools -> WCF Service Configuration Editor ).
In the editor, create a new service using the wizard. Browse and select the assembly that contains the service implementation, and select the service implementation type. Then, select the type of the service contract, and select HTTP as the communication mode, with Basic Web Services Interoperability. Specify the URL of the service (such as http://localhost:8080/bursteg/rss) and click finish to end the wizard operation.
To support the simple HTTP GET requests that this service will receive, add a new Endpoint Behavior (under the Advanced Node in the Configuration Pane). Call it WebGetBehavior and in the element extensions list, pick webHttp.
Go back to the service node in the designer, and change the Binding from basicHttpBinding to webHttpBinding. Additionally, provide the name of the behavior you have just created (WebGetBehavior).
9. Implement the host program. This logic creates a new instance of ServiceHost class and opens it for listening.
ServiceHost host = new ServiceHost(typeof(SyndicationServiceImplementation));
Console.WriteLine("Press any key to close host…");
10. Run the host, and navigate to the service URL. Now you can see the RSS Feed.
You can download a sample project from here.