Exposing Product Catalog as Syndication Feed Using WCF

In this example I will demonstrate how to expose product catalog as syndication feed using either atom or rss. I will start of with creating a new syndication library as shown below.

image

The code generated by Syndication library has a working example that you can immediately run and see the exact behavior. The output generated when you run the example is shown below.

image

Output contains a single feed that has a title and content. You confirm its an rss feed by doing view source.

The project consists of WCF service. Service has a contract, an interface attributed with ServiceContract. It has one method called CreateFeed attributed as OperationContract. There is another class Feed1 that implements the contract. The output is shown below.

image

In addition to the service contract attribute, there are two other attribute that define that this service can return either atom or rss. CreateFeed method return a SyndicationFeedFormatter that could either be rss or atom depending upon what is requested based on the querystring. SyndicationFeedFormatter  is a complex type that is defined inside System.ServiceModel.Syndication namespace. This is the namespace where you will find all the classes necessary to create syndication feed. The method is also attributed with WebGet attribute with UriTemplate set to *. What this indicates is, if the request is made to root url of the service, it needs to map to CreateFeed method. The implementation for the contract is as shown below.

image

In the above example we start with creating a syndication feed based on title and description. Next we are creating a list of syndication items and than assigning it to the items property of SyndicationFeed. We are hard coding two items but we will change that soon with a LINQ query that returns products from NorthWind database. The next step is checking to see, based on querystring if user is looking for rss or atom feed. Depending on the querystring passed, we are returning either atom10 or rss20 feed formatter. Let's go ahead and modify the the code above to return feed for the products table.

image

From the code below, you can see that I am generating SyndicationItem based on linq query for products.

Another intresting place to look at is the App.config and the end point that gets generated.

image

In the configuration above, you would notice that we are using webHttpBinding which is the new binding that got added in .net 3.5 that does all the magic of exposing rest based services. 

No Comments