Introduction
A lot of sites use Rss feeds to provide others with information. For security reason Silverlight doesn’t allow cross domain calls. With your own servers you can overcome this by specifying access via a crossdomain.xml file. In most cases you want to read a Rss feed that is not on your domain, and it is not possible to add or change the crossdomain.xml file. Because Silverlight can communicate with Asp.NET, and Asp.NET can access almost any Rss feed, that would be a great option. The new .Net Ria Services in Silverlight 3 use Asp.NET (check the downloads list for the latest version). So easy access to Rss feeds is at hand. Here’s how to get the data from the Rss feed to the Silverlight 3 client using Asp.NET and the .NET RIA Services.
Details
If you haven’t already, download and install Silverlight 3 and the .NET Ria Services. Here’s a “Getting Started”-page for Silverlight 3 where you can find everything you need. Start Visual Studio when you’re ready to go.
First, create a new project and select the Silverlight Application. Give a useful name, like RiaRssReader. Make sure “Link to ASP.NET server project” is checked. Just accept all other defaults.

To enable the Ria services you’ll have to add a Domain Service class. Do this by right click on the RiaRssReader.Web project in the Solution Explorer and click Add…-> new Item…. Find the Domain Service Class entry, name it RssService and click Add.
In the New Domain Service Class window you have the ability to bind the domain service to a data context. In this case just select <empty domain service class> from the dropdown list and click Ok.

The RiaService.cs file is created and opened. Start by adding using System.Xml.Linq; to the list of usings.
The following method reads an xml document from an url and returns this as a string. The ServiceOperation attribute marks the function as a domain service operation and this will make it available to the client.
[ServiceOperation]
public string GetRssFeed(string Url)
{
XDocument RssFeed = XDocument.Load(Url);
return RssFeed.ToString();
}
Now build the solution to generate the necessary files needed by the client. These file are hidden by default. If you want to view them, check the view all files button in the solution explorer when the RiaRSSReader client project is selected. The generated files are located in the Generated_Code folder and in this case the file is named RiaRssReader.Web.g.cs
To view the data returned from the server, add Textbox to mainpage.xaml name it FeedResult.
<TextBox x:Name=”FeedResult”/>
The last thing to do is making the actual call to the server from the client and adding the result to the textbox. Change the code behind file mainpage.xaml.cs so that the code looks like:
public MainPage() {
InitializeComponent();
RssContext context = new RssContext();
context.GetRssFeed("http://stackoverflow.com/feeds/",
GetRssFeedCompleted, null);
}
void GetRssFeedCompleted(InvokeOperation<string> obj)
{
FeedResult.Text = obj.Value;
}
This code creates the context and makes an asynchronous call to the server getting the Rss feed from Stack Overflow.
Build, run, wait and and see the feed appear in the textbox.
What's next…
Showing an Rss feed in a textbox as a string isn’t the best way of dealing with rss feeds. Handling it as actual Xml would be nice. Binding that Xml to a pretty looking and awesome styled ListBox would be even better.
(updated 29th Sept 2009)


Tuesday, April 14, 2009 11:39 AM