Bridging Web Service Calls in Silverlight

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

Technorati Tags: ,,

posted @ Friday, June 06, 2008 12:57 PM

Print

Comments on this entry:

# Sample business plan for a dairy farm.

Left by Business plan example. at 7/26/2008 5:38 PM
Gravatar
Business plan example. Example of business plan for sports bar and grill. Frozen yogurt shop business plan. Sample business plan. Business plan format. Global business plan. Golf course business plan. Metal casting business plan.

# Online casino gambling and online slots guide.

Left by Internet casino gambling online. at 7/27/2008 2:52 PM
Gravatar
Betting casino gambling links online.

# Blackjack online casino.

Left by Blackjack online casino. at 7/28/2008 2:19 AM
Gravatar
Online casino blackjack.

# acai berry cleanse

Left by acai berry cleanse at 3/2/2009 6:11 PM
Gravatar
That was a great time! Hope to be back in August 2009. Ask for our...

# Percocet alieve.

Left by Percocet. at 3/19/2009 6:49 PM
Gravatar
Half life of percocet. Percocet.

# Xanax.

Left by Buy xanax online. at 3/20/2009 2:23 AM
Gravatar
Xanax drug test. Xanax dosage. Buy generic xanax information. Xanax.

# Cheap tramadol prescriptions online.

Left by Comparative potencies of opioids tramadol. at 3/21/2009 2:29 AM
Gravatar
Tramadol cheap no rx free overnight shipping. Tramadol. Buy tramadol online save wholesale price yep. White tramadol with 377 on the side.

# Phentermine florida.

Left by Phentermine ingredient. at 3/21/2009 11:27 PM
Gravatar
Cheap phentermine. Phentermine. Mg buy phentermine. Phentermine on line. Phentermine online pharmacy. Phentermine forum. Phentermine diet.

# Xanax no prescription.

Left by Buying xanax online. at 3/23/2009 12:35 AM
Gravatar
Lethal blood levels of xanax. Xanax. Generic xanax 2 mg no prescription. Cheap xanax online buy cheap xanax buy cheap xanax. Xanax effect. Xanax weight gain.

# What happens when you crush xanax xr.

Left by Side effects of xanax. at 3/26/2009 4:52 AM
Gravatar
Ijijiji xanax hompage.

# How does phentermine work.

Left by Phentermine diet pills. at 3/27/2009 9:05 AM
Gravatar
Buy phentermine cod. Phentermine. Buy cheap phentermine online. Best price for phentermine. Herbal phentermine. Free phentermine. Phentermine information. Buy cheap phentermine.
Comments have been closed on this topic.
«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678