2
votes

I'm programming my first Silverlight app and it uses a Silverlight-enabled WCF service to retrieve and send data to my server.

I created a SL application + ASP.NET MVC web page to host the SL app.

In the MVC app I create the WCF service and I consume it on the SL app. So far so good.

I deploy the project using the Web deploy and it works on my remote host but using Fiddler I realize that the remote app is using the WCF service I have on the development server (AKA localhost:port).

I changed the WCF service in VS and it now points to the remote host and if I deploy the solution, so far so good.

But you know, now my service points to a remote server and doesn't work because I have to create the xml for crossdomain access (and fiddler says to me that is looking on domain.com/crossdomain.xml instead of domain.com/virtualdirectory/crossdomain.xml).

So my question is: How I handle this? Would be good to have my project using the local service and when I deploy, it use the remote one.

Do I have to do this manually or there is an automatic way?

Thanks.

2

2 Answers

2
votes

The endpoint address is included in the ServiceReferences.ClientConfig file, which is then part of the files embedded in the XAP package. You have to update that file when you deploy to the live server.

A workaround is to build a factory method for the client proxy class, which builds the service address dynamically from the address of the Silverlight package. Here is a guide, which contains the following code:

public class ServiceUtil {
    public static PeopleServiceClient GetPeopleServiceClient() {
        BasicHttpBinding binding = new BasicHttpBinding(
            Application.Current.Host.Source.Scheme.Equals("https", StringComparison.InvariantCultureIgnoreCase) 
            ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None);
        binding.MaxReceivedMessageSize = int.MaxValue;
        binding.MaxBufferSize = int.MaxValue;
        return new PeopleServiceClient(binding, new EndpointAddress(
            new Uri(Application.Current.Host.Source, "../PeopleService.svc")));
    }
}

Using such a factory you will be able to deploy your app to any server without reconfiguration, as long as the silverlight XAP file and the service are located in the same way relative to eachother.

0
votes

You should add a clientaccesspolicy.xml file in the root of your server. See this MSDN link for more information. The crossdomain.xml can also be used, but Microsoft put it in there because of Flash, clientaccesspolicy.xml is preferred as Silverlight only supports a subset of crossdomain.xml.

It is good practice to use the localhost service until you deploy. When you deploy you can then change the config file or write code to find the service url dynamically. This example assumes that the service in the same url as the Silverlight XAP.

string serviceUrl = Application.Current.Host.Source.AbsoluteUri.Substring(0, Application.Current.Host.Source.AbsoluteUri.IndexOf("ClientBin/")) + "Services/DataService.svc"