Alex Rivera | Logout

Silverlight Crossdomain

Asked 2009-01-16T18:17:11.410
25

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 Appl
Edit
Report

3 Answers

1

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

answered 2011-08-12T11:32:23.713
0

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

answered 2009-01-21T14:21:43.467
-1

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

answered 2013-04-19T18:50:38.360

Your Answer