title: Silverlight Crossdomain
body: <p>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:</p>
  
  <p>*** Create a new ASP.NET webservice project.</p>
  
  <blockquote>
    <p>It will come with an existing Service.asmx file exposing a "HelloWorld" web method.</p>
    
    <p>View in browser, hit the "Invoke" button. It should work returning the "Hello World" string.</p>
    
    <p>On my machine, the URL is: "<a href="http://localhost:15511/WebSite5/Service.asmx" rel="noreferrer">http://localhost:15511/WebSite5/Service.asmx</a>"</p>
  </blockquote>
  
  <p>*** Start a new instance of Visual Studio, create a Silverlight Web Application Project. </p>
  
  <p>*** 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.</p>
  
  <pre><code>&lt;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"&gt;
      &lt;StackPanel&gt;
          &lt;Button Click="Button_Click"&gt;
              &lt;Button.Content&gt;
                  &lt;TextBlock Text="Test"/&gt;
              &lt;/Button.Content&gt;
          &lt;/Button&gt;
      &lt;/StackPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Add the web reference, using statement and event handler for the Button_Click:</p>
  
  <pre><code>    private void Button_Click(object sender, RoutedEventArgs e)
      {
          ServiceSoapClient client = new ServiceSoapClient();
          client.HelloWorldCompleted += (object s, HelloWorldCompletedEventArgs ea) =&gt; { 
              MessageBox.Show(ea.Result); 
          };
          client.HelloWorldAsync();
      }
  </code></pre>
  
  <ul>
  <li>Run the Silverlight Application. In my case I'm going to my Silverlight Test Page at: 
  <a href="http://localhost:15558/SilverlightApplication1TestPage.aspx" rel="noreferrer">http://localhost:15558/SilverlightApplication1TestPage.aspx</a></li>
  </ul>
  
  <p>Run and of course it blows up because of crossdomain issues. So next add the <strong>clientaccesspolicy.xml</strong> file with the following to the root of your web application hosting the service: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;access-policy&gt;
    &lt;cross-domain-access&gt;
      &lt;policy&gt;
        &lt;allow-from http-request-headers="*"&gt;
          &lt;domain uri="*"/&gt;
        &lt;/allow-from&gt;
        &lt;grant-to&gt;
          &lt;resource include-subpaths="true" path="/"/&gt;
        &lt;/grant-to&gt;
      &lt;/policy&gt;
    &lt;/cross-domain-access&gt;
  &lt;/access-policy&gt;
  </code></pre>
  
  <p>This should open things up since it's got a wildcard for headers, uris, and resources, right?</p>
  
  <ul>
  <li>Run again and you get an error: </li>
  </ul>
  
  <p>An error occurred while trying to make a request to URI '<a href="http://localhost:15511/WebSite5/Service.asmx" rel="noreferrer">http://localhost:15511/WebSite5/Service.asmx</a>'. 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.</p>
  
  <p>So question: is there a secret to the clientaccesspolicy file? One could alternately try with the crossdomain.xml but it gives a similar result.</p>