Writing a WCF POX Syndication Service

WCF has received some enhancements in the .NET 3.5 framework. It is now possible to use the WCF service framwork to write POX  services; a service that does not use SOAP, but Plain Old Xml.

Also the Syndication support for WCF is new to .NET 3.5. It has builtin support to produce Rss 2.0 and Atom 1.0 feeds.

The example I like to show you is an Event Log Feed Service. This service produces a Rss 2.0 feed for the Application event log Error entries. It is hosted in IIS and can be called by addressing it over the url:

http://[base url]/Service.svc/GetLogEntries

Notice the GetLogEntries after the Service.svc. The GetLogEntries maps to the GetLogEntries method of the service and takes two optional parameters: an entryType (Error, Warning, Information) and a feedType (Rss1, Atom1).

http://[base url]/Service.svc/GetLogEntries?feedType=atom1
http://[base url]/Service.svc/GetLogEntries?entryType=Warning&feedType=atom1

To look at this for yourself download the source code here and install the service in IIS. Make sure the service dll is in the web’s bin folder.

The Service interface is declared using WFC’s ServiceContract attribute.

public interface IEventFeedService
    SyndicationFeedFormatter<SyndicationFeed> GetLogEntries(string eventType, string feedType);

The return types are declare with using the ServiceKnownType attribute. The WebGet attribute makes it possible to call this service using the url (GET). The UriTemplate declares what possible variation are supported on the url: the method name and its optional parameters. Note that the parameter names of the method match the parameter names in the UriTemplate.

The Service implementation class implements the method of the Service interface (refer to the dowload for complete source code). Creating the Syndication Feed is a matter of creating SyndicationFeedItem instances and adding them to a SyndicationFeed instance. Finally the method returns either a Rss20FeedFormatter or a Atom10FeedFormatter instance depending on the requested feed format.

The Service.svc file is used by IIS to determine how to host the service.

<%@ServiceHost Factory=”System.ServiceModel.Web.WebServiceHostFactory” Service=”EventLogFeedService.EventFeedService” %>

Besides specifying which service class should be hosted, the file also specifies a Factory to create a WCF service Host instance. Note that this is a new class that supports the url addressing of our WebGet enabled service.

Disclaimer: Exposing event log information to external sources can expose a security hole!