Endpoint address in Silverlight

A silverlight control running in your browser normally loads data from your server via WCF. By default, the endpoint is pulled out of your .config file, which is fine. However, suppose the control is loaded using slightly different domain names (www.foo.com and foo.com). In this case, cross-site scripting prevention will fail the request when the host names doesn’t perfectly match.

The way around this is to programatically set the enpoint URI, which you can do like this:

var client = new FooSoapClient();
client.Endpoint.Address = new System.ServiceModel.EndpointAddress(
   "http://" + System.Windows.Browser.HtmlPage.Document.DocumentUri.Host + "/Foo.asmx");

The control will now work regardless of the domain name.