Silverlight application cannot access WCF services on other machines
-
18-09-2019 - |
Question
I have a silverlight application which works perfectly and can access the WCF services which are hosted in silverlight application itself. The port it is using is 1794.
When I deploy to other servers (dev or test or staging), the application is not able to access WCF services.
This is a snippet from my ServiceReference.ClientConfig looks like
<endpoint address="http://localhost:1794/MyWebService.svc"
binding="customBinding" bindingConfiguration="CustomBinding_MyWebService"
contract="ConfigMgmtServiceReference.MyWebService"
name="CustomBinding_MyWebService" />
My root folder contains the clientaccesspolicy.xml file too.
How can I get past this issue?
Solution
I suspect the localhost:1794
would be causing the issue - when the silverlight application executes on a client machine the localhost will not get it back to the server.
The technique i use to eliminate issues like this is to programmatically set the end points at run time. The two pieces of info i need are the location within my web project of the service (which is known ahead of time), and the address (domain) that the silverlight app has been served from (which i can find out).
private void initEndpoint(ServiceEndpoint endPoint, string serviceName)
{
Uri hostUri = Application.Current.Host.Source;
string wcfBaseUri = string.Format("{0}://{1}:{2}/WebServices/", hostUri.Scheme, hostUri.Host, hostUri.Port);
endPoint.Address = new EndpointAddress(new Uri(wcfBaseUri + serviceName));
}
In this piece of code, the folder /WebServices
is where my web services are located within my web app. I then call the function like this:
LoggingServiceClient loggingService = new LoggingServiceClient();
initEndpoint(loggingService.Endpoint, "LoggingService.svc");
my actual setup is slightly more complex than that, because i also want to be able to override that and manually configure the end points, but you get the point. By doing this, i have been able to deploy to all sorts of setups, with webservers running on odd ports, and the silverlight->webservice bit just works every time.