SL Calling same domain WCF located service zero configuration
Nice stuff… in my app’s I use to have a config.xml file associated to my Silverlight application, it included an entry to specify the root url to access the WCF services that I need to call in my app.
It was a pain on the neck swapping between localhost and server mode (remember always to set the right config. Blah blah…), googling a bit I have found nice solution… why do you need to host that root url in a file? You already can grab it from the navigator that is browsing your page !!! this function works quite well:
public string GetServiceProxyURL()
{
string serviceBase = "";
if (Application.Current.Host.Source.Host.ToUpper() == "LOCALHOST")
{
serviceBase = string.Format("{0}://{1}:{2}",
Application.Current.Host.Source.Scheme,
Application.Current.Host.Source.Host,
Application.Current.Host.Source.Port);
}
else
{
// include the appname in the path
string url = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsolutePath;
string[] path = url.Split("/".ToCharArray());
string appname = path[1];
serviceBase = string.Format("{0}://{1}",
Application.Current.Host.Source.Scheme,
Application.Current.Host.Source.Host, appname);
}
//Here you can add any subfolder if needede
//serviceBase += "/WsCommunicator/";
return serviceBase;
}