Web service auto-config
If you put a web service in the host web for your Silverlight application and discover it then when you create the reference the URI is obtained from the debug web server. You can configure this with the ServiceReferences.ClientConfig file but you have to do so prior to building the XAP file.
To change things after the fact, you can rename the XAP to ZIP, extract the ServiceReference.ClientConfig file, edit it, update the ZIP file and rename it back to XAP. Tedious but possible with tools intrinsic to Windows.
Another option is to rewrite the binding address after you instantiate the service client. Here’s a sample from one of my XAML code-behind files.
17 SPClient _spClient = new SPClient();
18
19 public Search()
20 {
21 InitializeComponent();
22 this.Title = ApplicationStrings.HomePageTitle;
23 string baseUri = HtmlPage.Document.DocumentUri.AbsoluteUri;
24 string relativeUri = _spClient.Endpoint.Address.Uri.ToString();
25 int i = relativeUri.IndexOf('/') + 2;
26 i = relativeUri.IndexOf('/', i);
27 relativeUri = relativeUri.Substring(i);
28 i = baseUri.LastIndexOf("#");
29 baseUri = baseUri.Substring(0, i);
30 baseUri.Substring(0, baseUri.LastIndexOf("/"));
31 _spClient.Endpoint.Address =
32 new EndpointAddress(baseUri + relativeUri);
33 _spClient.FindCompleted +=
34 new EventHandler<FindCompletedEventArgs>(_spClient_FindCompleted);
35 }
This code exploits the habit Silverlight applications have of putting a # between the end of the application base URI and the name of the XAML page; the last slash before the hash marks the end of the base URI.
None of this is remotely difficult. What I want to know is why the generated service client doesn’t detect service URIs that begin with “http://localhost:” and perform this simple fix-up automatically.