Visual Studio 2010 Silverlight/WCF Port Number problem

I’ve been developing a Silverlight 3 App with a data supplied through WCF service in VS2010. I set the app up using the Silverlight template in VS2010 at selected ‘Web Site’ as the Project Type, then added a Silverlight enabled WCF service to the solution’s web site project. So the Silverlight App & the WCF service are running from the same web site, which means I shouldn’t have any cross domain issues.

Everything has been fine for a week or so and my app has been growing in complexity as I try out more and more Silverlight 3 functionality.  Today however, it all went horribly wrong and my WCF call failed with the following error …

   System.ServiceModel.CommunicationException was unhandled by user code

The problem boils down to Visual Studio had arbitrarily decided to start using a different port number. The client was still using the old port number in the endpoint address and never the twain shall meet. Updating the Service reference doesn’t fix-up the endpoint address although deleting the service reference and recreating it, though a bit of a pain, does. The best way to fix the problem is to change the endpoint address that the client uses manually. There’s a file in the Silverlight Application called ‘ServiceReferences.ClientConfig’ …

<configuration>
   <system.serviceModel>
       <bindings>
            <customBinding>
                <binding name="CustomBinding_Rpt50Service">
                    <binaryMessageEncoding />
                    <httpTransport
                          maxReceivedMessageSize="2147483647"
                          maxBufferSize="2147483647" />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint
                address=http://localhost:54434/myApp.Web/Service.svc
                binding="customBinding"
                bindingConfiguration="CustomBinding_Service"
                contract="ServiceReference.Service"
                name="CustomBinding_Rpt50Service" />
       </client>
    </system.serviceModel>
</configuration>

… Just load it into the VS text editor and change the port number to the same one shown in the URL shown in the browser address bar when your Silverlight app fires up.

As a footnote there doesn’t seem to be a way to specify the port number that the development web server uses, I can’t find a Web tab in my web project properties – hopefully it will be added in the final version of VS2010.