25
votes

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 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.

13
I have a Firestarter lab app that fails with cross-domain every time UNLESS I run Fiddler. If Fiddler is running, it works every time! (it appears to have something to do with the proxy server at my company. It also works if I clear the proxy information from IE). I cannot replicate the problem with a service/silverlight combo that I roll myself. How weird is that?JMarsch
@Ian Kemp: I think that's the answer! Thank you.JMarsch

13 Answers

8
votes

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"

5
votes

I've had this same problem a couple of times. In the past I've solved this by using the Web App as start up, but it looks like you've already done that.

My post on the subject: http://www.donnfelker.com/silverlight-cross-domain-issue/

4
votes

Make sure that you put the clientaccesspolicy.xml file in the root of IIS web directory e.g.

C:\Inetpub\wwwroot\clientaccesspolicy.xml

This will make sure that it is accessible directly at http:///clientaccesspolicy.xml

I was getting the same error and I resolved it doing the above steps.

2
votes

Client Configuration

Maybe your ServiceReferences.ClientConfig for your Silverlight client is pointing to the wrong URL?

Also, check the location of your cross-domain policy file. This MSDN article has more information.

Locally Running Silverlight

Additional note for running Silverlight locally (Vista Sidebar, for example). As reported in this blog entry, "Silverlight cannot use any network provider, when running locally." The workaround is to use javascript to interface to the web service in this situation.

2
votes

Something that worked for me began with what I found on the silverlight forums here. It essentially asked if I could even get to my clientaccesspolicy.xml or crossdomain.xml from localhost (http://localhost/clientaccesspolicy.xml). When I tried to navigate there, I couldn't so I simply found the code for both of them (also within the aforementioned thread), and copy-pasted over the code inside of those files existing in my inetpub\wwwroot\ directory (I opened them up using Notepad++). The weird part was the code didn't change at all , and yet, it works! Hope that helps someone! This was extremely strange.

clientaccesspolicy.xml

<?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 path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

crossdomain.xml

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>

Be blessed!

-sf

1
votes

Have you tried fiddler when using this through IE, you might be able to see the traffic silverlight is causing, such as which cross policy files it is looking for?

1
votes

I modified my Internet explorer settings. The website runs on a intern webserver so I added it in the Iexplorer Trusted Sites list. (Tools->Internet Options->Security->Sites).

Then I changed the security level and enabled cross domains. Done, Works but took me while to find the solution.

Best, Jeppen

1
votes

You have to be aware of situation that when you reference to your service inside your project there 'll be created file "Reference.ClienConfig" and there is:

    **

<endpoint address="http://localhost:57675/Servis.asmx" binding="basicHttpBinding"
            bindingConfiguration="ServisSoap" contract="ServiceReference1.ServisSoap"
            name="ServisSoap" />

**

Make sure that your page is still using the same port (for example here is 57675). By default your localhost 'll get random port, so you have to change it to be static number and not Dynamic. (Right click on asp.net project/Tab Web/Specific port /type number Hope it helps

0
votes

I ran into something like this and adding a ServiceHostFactory fixed my issue. The cross-domain policy file alone did not fix it.

class MyHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        MyHost customServiceHost =
          new MyHost(serviceType, new Uri("[Your URL goes here]",UriKind.Absolute));

        return customServiceHost;
    }
}

class MyHost : ServiceHost
{
    public MyHost(Type serviceType, params Uri[] baseAddresses)   base(serviceType, baseAddresses)
    { }

    protected override void ApplyConfiguration()
    {
        base.ApplyConfiguration();
    }
}

You also have to add Factory="MyHostFactory" in the tag that defines your service

0
votes

The problem could be that your development server is not able to serve the xml file, try this - explicitly make it available through WebGet

[ServiceContract]
    public interface ICrossDomainService
    {
        [OperationContract]
        [WebGet(UriTemplate = "ClientAccessPolicy.xml")]
        Message ProvidePolicyFile();
    }

and then the ProvidePolicyFile() can be

public System.ServiceModel.Channels.Message ProvidePolicyFile()
        {
            FileStream filestream = File.Open(@"ClientAcessPolicy.xml", FileMode.Open);
            // Either specify ClientAcessPolicy.xml file path properly
            // or put that in \Bin folder of the console application
            XmlReader reader = XmlReader.Create(filestream);
            System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None, "", reader);
            return result;
        }
0
votes

It worked in my pc successfully, you can place your

clientaccesspolicy.xml

<?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 path="/" include-subpaths="true"/>
       </grant-to>
     </policy>
   </cross-domain-access> 
</access-policy>

both your project directory and your webservices root.

0
votes

I was facing the same problem and take more than 3 days to figure out the Problem. I notice also when I was calling the internet Cloud Service WCF from a Silverlight app hosted in another web server it just shows Cross-Domain Errors. After looking into some posts I didn't solve the problem, even putting cross-domain.xml and clientaccesspolice.xml files in service root directory. So I just try to instead of using http://example.com I just change it to secure https://example.com and it just worked fine. The cross-domain errors disappears. service was called without problems.

-1
votes

Check which project is set default. It should be the web project that should be set default and not the silverlight project.