Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight Crossdomain

Tags:

I've seen a lot of links to MSDN and "works on my machine!" answers so I'd like to ask my question with the exact steps to duplicate what I'm doing. Because we are using an already existing webservice, I'm asking with the context of having a webservice hosted outside of my project, unlike many of the tutorials and videos online. So here goes:

*** Create a new ASP.NET webservice project.

It will come with an existing Service.asmx file exposing a "HelloWorld" web method.

View in browser, hit the "Invoke" button. It should work returning the "Hello World" string.

On my machine, the URL is: "http://localhost:15511/WebSite5/Service.asmx"

*** Start a new instance of Visual Studio, create a Silverlight Web Application Project.

*** Stick a single button on there with an event handler to call the web service. I personally nuke the Grid and use a simple StackPanel. eg.

<UserControl x:Class="SilverlightApplication1.Page"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      Width="400" Height="300">     <StackPanel>         <Button Click="Button_Click">             <Button.Content>                 <TextBlock Text="Test"/>             </Button.Content>         </Button>     </StackPanel> </UserControl> 

Add the web reference, using statement and event handler for the Button_Click:

    private void Button_Click(object sender, RoutedEventArgs e)     {         ServiceSoapClient client = new ServiceSoapClient();         client.HelloWorldCompleted += (object s, HelloWorldCompletedEventArgs ea) => {              MessageBox.Show(ea.Result);          };         client.HelloWorldAsync();     } 
  • Run the Silverlight Application. In my case I'm going to my Silverlight Test Page at: http://localhost:15558/SilverlightApplication1TestPage.aspx

Run and of course it blows up because of crossdomain issues. So next add the clientaccesspolicy.xml file with the following to the root of your web application hosting the service:

<?xml version="1.0" encoding="utf-8" ?> <access-policy>   <cross-domain-access>     <policy>       <allow-from http-request-headers="*">         <domain uri="*"/>       </allow-from>       <grant-to>         <resource include-subpaths="true" path="/"/>       </grant-to>     </policy>   </cross-domain-access> </access-policy> 

This should open things up since it's got a wildcard for headers, uris, and resources, right?

  • Run again and you get an error:

An error occurred while trying to make a request to URI 'http://localhost:15511/WebSite5/Service.asmx'. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent.

So question: is there a secret to the clientaccesspolicy file? One could alternately try with the crossdomain.xml but it gives a similar result.

like image 417
t3rse Avatar asked Jan 16 '09 18:01

t3rse


People also ask

What is Crossdomain xml and why do I need it?

The crossdomain. xml file is a cross-domain policy file. It grants the Flash Player permission to talk to servers other than the one it's hosted on and is required for Flash to use Speedtest servers. Note there are two sources of crossdomain information for a Speedtest Server.

What is Silverlight cross-domain policy?

The Silverlight cross-domain policy controls whether Silverlight client components running on other domains can perform two-way interaction with the domain that publishes the policy. If another domain is allowed by the policy, then that domain can potentially attack users of the application.

What is Clientaccesspolicy xml?

The clientaccesspolicy. xml file is located where the service is being hosted. But Silverlight also supports this file. " clientaccesspolicy.xml" is specific to Silverlight. If we want to grant multiple domains access, an admin simply can modify the clientaccesspolicy.


1 Answers

I've encountered this problem (SL v5.0 and Visual Studio 2010), what fixed it for me is that I went into the Silverlight project properties >> Silverlight tab and selected "Require elevated trust when running in-browser"

like image 196
Socardo Avatar answered Oct 02 '22 20:10

Socardo