Mike Taulty's Blog
Bits and Bytes from Microsoft UK
WCF 3.5 and SyndicationFeed in Beta 2 (and comparison with ASP.NET and LINQ to XML)

Blogs

Mike Taulty's Blog

Elsewhere

Archives

Argh! I pulled a bit of hair out today over trying to get a simple demo program around SyndicationFeed that worked in Beta 1 of VS 2008 across to Beta 2 so I thought I'd share.

(Those of you who have seen me will know that I cannot afford to lose much more hair).

Let's say that I've got some simple pieces of data that I want to get out as items on an RSS feed and let's look at doing this with WCF V3.5.

Firstly, it's File->New Project.

image

to make a WCF library. This throws in a lot of boiler-plate code that I don't actually want (I wish VS would give me less boiler-plate code as I spend ages deleting it but maybe that's just me).

I add a reference to System.ServiceModel.Web and go ahead and build out my service contract.

namespace SyndicationLibrary
{
  [ServiceContract(Namespace="")]
  [ServiceKnownType(typeof(Rss20FeedFormatter))]
  public interface IProvideItems
  {
    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "")]
    SyndicationFeedFormatter<SyndicationFeed> GetFeed();
  }  
}

 

once I've got a contract, I need an implementation;

namespace SyndicationLibrary
{
  public class ItemProvider : IProvideItems
  {
    public SyndicationFeedFormatter<SyndicationFeed> GetFeed()
    {
      // Generate some items...
      SyndicationFeed feed = new SyndicationFeed()
      {
        Title = new TextSyndicationContent("Mike's Feed"),
        Description = new TextSyndicationContent("Mike's Feed Description")
      };

      feed.Items = from i in new int[] { 1, 2, 3, 4, 5 }
                   select new SyndicationItem()
                   {
                     Title = new TextSyndicationContent(string.Format("Feed item {0}", i)),
                     Summary = new TextSyndicationContent("Not much to see here"),
                     PublishDate = DateTime.Now,
                     LastUpdatedTime = DateTime.Now,
                     Copyright = new TextSyndicationContent("MikeT!"),
                   };

      return (new Rss20FeedFormatter(feed));
    }
  }
}

And I need to set up some configuration for this;

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="SyndicationLibrary.ItemProvider">
        <endpoint address="http://localhost:9091/feeds" binding="webHttpBinding" 
                  contract="SyndicationLibrary.IProvideItems" 
                  behaviorConfiguration="foo"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="foo">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

 

And with that in place, I can test what I've written because VS 2008 has this new ability to host up a class library like this using WCF without you having to write the standard hosting test harness. You just press F5 and it runs up a little hosting environment in a process called WcfSvcHost.exe. This little executable even has its own help file right now :-) From reading that, it looks like the service host looks at any services that it finds in your project and attempts to host them up using your config file. It can also, optionally, run a client to test against your service. You can switch this on/off (or change to a different client) from the project properties page;

image

So, with all that in place, I can spring up a browser and I can get an RSS feed by simply requesting;

http://localhost:9091/feeds

and that just "works" for me.

Now, I must admit that at this point I'm not sure what's happened in the object model because in Beta 1 you simply dealt with SyndicationFeed and returned that from your service whereas we now have some extra complexity in terms of SyndicationFeedFormatter<T>. Also, in beta 1 I think I could choose between RSS/Atom by making a setting in my configuration file whereas here it looks like that has disappeared (i.e. I haven't found it yet). As it turns out, because you return SyndicationFeedFormatter<SyndicationFeed> and, because both Rss20FeedFormatter and Atom10FeedFormatter look to be derived from that base class then it shouldn't be too difficult to check a switch somewhere and decide whether to return RSS or Atom.

So, that's all fine but I then had a thought.

How difficult is it to acheive this without WCF given the rather nice world we have in VS 2008 and .NET Framework V3.5? So, how hard would it be to (say) write an ASP.NET handler to do the same sort of thing? Let's have a look...

It starts with File->New->Web Site...

image

Notice that I've gone for VB....added in an ASP.NET handler;

image

and then gone ahead and written a handler;

Imports Microsoft.VisualBasic
Imports System.Xml

Public Class Handler
  Implements IHttpHandler


  Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
    Get
      Return False
    End Get
  End Property

  Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) _
    Implements System.Web.IHttpHandler.ProcessRequest

    Dim rss = <rss version="2.0"> _
                    <channel> _
                        <title>Mike's Feed</title> _
                        <description>Mike's Feed Description</description> _
                        <pubDate><%= DateTime.Now %></pubDate> _
                        <lastBuildDate><%= DateTime.Now %></lastBuildDate> _
                        <%= From i In New Integer() {1, 2, 3, 4, 5} _
                          Select _
                          <item> _
                              <title><%= String.Format("Item number {0}", i) %></title> _
                              <description>Nothing much to see here</description> _
                              <pubDate><%= DateTime.Now %></pubDate> _
                            </item> _
                        %>
                </channel>
              </rss>

    context.Response.ContentType = "text/xml"

    Dim writer As XmlWriter = XmlWriter.Create(context.Response.OutputStream)
    rss.WriteTo(writer)
    writer.Close()
    
  End Sub
End Class

and plugged it in to my web.config;

<httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="GET" path="*.rss" type="Handler"/>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
    </httpHandlers>

 

and then created an empty .rss file for it to access and run it up. So, a quick "compare and contrast";

Using WCF;

  1. Have to write an interface
  2. Have to write an implementation
  3. Have to configure up System.ServiceModel
  4. Have to get the IIS hosting right as well (I didn't do that here as I just used the test service hosting)
  5. Pretty easy though to switch from RSS to Atom and vice versa.

Using ASP.NET and LINQ to XML

  1. Have to write one method on a handler.
  2. Have to configure up web.config
  3. Have to go read what an RSS feed looks like in terms of XML and may well get that wrong (mine is probably wrong above)
  4. Have to write more code to switch from RSS to Atom and vice versa.

So there's not a huge amount between these things in terms of how much code/config you'd write but there are differences in level of abstraction versus "barrier to entry" (i.e. WCF gives you more choices but, first, you have more to figure out about WCF :-)).


Posted Mon, Aug 6 2007 8:48 AM by mtaulty

Comments

Kirk Allen Evans' Blog wrote Creating a JSON Service with WebGet and WCF 3.5
on Tue, Sep 4 2007 4:31 PM
I couldn't let it go. Tim's post had me intrigued about how to convert RSS to JSON using some of the
Noticias externas wrote Creating a JSON Service with WebGet and WCF 3.5
on Tue, Sep 4 2007 4:43 PM
I couldn&amp;#39;t let it go. Tim&amp;#39;s post had me intrigued about how to convert RSS to JSON using some
Ghillie Suits » WCF 3.5 and SyndicationFeed in Beta 2 (and comparison with ASP.NET and LINQ to XML) wrote Ghillie Suits &raquo; WCF 3.5 and SyndicationFeed in Beta 2 (and comparison with ASP.NET and LINQ to XML)
on Wed, Oct 24 2007 8:46 AM
Christian Weyer: Smells like service spirit wrote Beyond the Obvious: New Features and Fixes for WCF in .NET 3.5
on Mon, Oct 29 2007 7:48 AM
With .NET 3.5 and Visual Studio 2008 being quite close to RTM
these days, I thought it would be time...
Web Log di Raffaele Rialdi wrote Ma che c'
on Thu, Nov 22 2007 12:59 AM
Ma che c'