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;
}
One of the SL app’s that I have developed (http://www.dbschemaeditor.com) calls services that are located on the sames domain… so far so good.
Some weeks ago a user was complaining that in some occasions the application thrown a “Connection failed” message whenever he tried to login, … researching a bit on the issue I found that the user was typing in the url address dbschemaeditor.com (no WWW), what happens in this case? It’s treated as a cross domain call !! (request for clientaccesspolicy.xml and ding dong … error !).
Maybe to solve this you can configure something at IIS level (redirect or whatever), in my case I’m running the app in a shared hosting… I found a quick (and dirty?) solution… add a clientaccesspolicy file (site root) enabling explicitly access to the url dbschemaeditor.com (no WWW):
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="SOAPAction" >
<!-- Just include the same domain without wwww-->
<domain uri="http://dbschemaeditor.com/"/>
</allow-from>
<grant-to>
<resource include-subpaths="true" path="/"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
That worked out for the SL service calls.