Geeks With Blogs

News


Timmy Kokke's Blog

↑ Grab this Headline Animator

Timmy Kokke at Blogged
Timmy Kokke …just sorting my bubbles…

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.

new project

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.

 

new domain service class

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)

 

Shout it

Posted on Tuesday, April 14, 2009 11:39 AM Silverlight , .NET Ria Services | Back to top


Comments on this post: Using Silverlight Ria Services to overcome Cross Domain issues with RSS

# re: Using Silverlight Ria Services to overcome Cross Domain issues with RSS
Requesting Gravatar...
I am currently trying to recreate this in a VB Silverlight project.

However, RSSContext is undefined.
Left by cj on May 28, 2009 4:32 PM

# re: Using Silverlight Ria Services to overcome Cross Domain issues with RSS
Requesting Gravatar...
I tried your solution, but I am missing the event handler 'GetRssFeedCompleted'.
How do I implement it?
Left by MK on Jul 31, 2009 5:00 PM

# re: Using Silverlight Ria Services to overcome Cross Domain issues with RSS
Requesting Gravatar...
This example has been written with the May 2009 preview of the .NET Ria Services. This event has been changed in the new July Preview. You should handle the Completed event now. I'll update this article in the next few days to match the latest release.
Left by Timmy Kokke on Jul 31, 2009 5:12 PM

# re: Using Silverlight Ria Services to overcome Cross Domain issues with RSS
Requesting Gravatar...
I am getting:
"The type or namespace name 'RssContext' could not be found (are you missing a using directive or an assembly reference?)"
Can't find the assembly I need to add.
Thanks
Left by Will on Oct 13, 2009 4:32 PM

# re: Using Silverlight Ria Services to overcome Cross Domain issues with RSS
Requesting Gravatar...
The designs of Ruud-Jan Kokke (1965 Velp) are characterized by a personal style and design with special attention to detail and functionality.
Left by Huntsville OB GYN on Oct 08, 2010 8:38 AM

Your comment:
 (will show your gravatar)


Copyright © Timmy Kokke | Powered by: GeeksWithBlogs.net