Cross Domain Web Service Reference in Silverlight 2
I just spent the last 2 hours trying to get my Silverlight 2 application to call a simple webservice. I included the Service Reference in my application, it was detected and the new namespace was constructed. I coded the following simple test application:
public partial class Page : UserControl {
public Page() {
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
approverManager.ApproversSelectRecordSetSoapClient client = new ApproversSelectRecordSetSoapClient();
client.approversListRecordSetByVendorCompleted += new EventHandler<approversListRecordSetByVendorCompletedEventArgs>(client_approversListRecordSetByVendorCompleted);
client.approversListRecordSetByVendorAsync();
}
void client_approversListRecordSetByVendorCompleted(object sender, approversListRecordSetByVendorCompletedEventArgs e) {
Console.WriteLine(e.Result.ToString());
_textbox_one.Text = e.Result.ToString();
}
}
But every time this executed I got an error about "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."
The solution for this is as simple as it is annoying. The problem has nothing to do with your application, but rather the service call is expecting to find a file on the root of the server where your service is located called clientaccesspolicy.xml. This is not in the root directory of the service itself, but on the root of the server. If you can't store files on your server root, you're apparently out of luck.
The contents of the file should be something like:
<?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>
That is the simplest form and will get your service up and running. If you want to get down and dirty with access allowances and denials, you can read more at the msdn article:
http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx
