For security reasons, the Silverlight version 2 runtime restricts access to certain classes of URLs from the WebClient and HTTP classes in the System.Net namespace.
The main requirement is that the services you want to use should implement either crossdomain.xml (which is the Flash policy file) or clientaccesspolicy.xml (which is the Silverlight one). If the service you want to use does not implement one of these then you can't use Silverlight to access it.
However such restrictions are not present when using WebClient on the server, so you can easily create a bridge service that your Silverlight client can use.
For example you could create a very simple WCF Service in the same web site that will host your Silverlight app as follows:
using System;
using System.ServiceModel;
[ServiceContract]
public interface IFeeds
{
[OperationContract]
string GetFeed(Uri uri);
}
using System;
using System.Net;
public class Feeds : IFeeds
{
#region IFeeds Members
public string GetFeed(Uri uri)
{
WebClient client = new WebClient();
return client.DownloadString(uri);
}
#endregion
}
Note: You need to change the Wcf service binding in web.config so that you use a basicHttpBinding rather than the wsHttpBinding (which is not supported by Silverlight)
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="FeedsBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="FeedsBehavior" name="Feeds">
<endpoint address="" binding="basicHttpBinding" contract="IFeeds">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
Now you can add a service reference from your Silverlight application to this Wcf service and use it something like as follows:
private void Button_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.FeedsClient client = new ServiceReference1.FeedsClient();
client.GetFeedCompleted += client_GetFeedCompleted;
client.GetFeedAsync(new Uri("http://timesonline.typepad.com/sports_commentary/atom.xml"));
}
void client_GetFeedCompleted(object sender, ServiceReference1.GetFeedCompletedEventArgs e)
{
TextBlock1.Text = e.Result;
}
Cheers
Ian