views:

193

answers:

2

What is the correct pattern or method for developing a Silverlight application (which is the Silverlight project and Web Application in a single solution)? I mean, how do you add the Service Reference if the localhost port number will be constantly changing?

Thanks.

+1  A: 

If you select the web project in the Solution Explorer, you can change a property in the Properties tool window that will stop the port from changing. The property is called "Use dynamic ports" and you want to set it to false so that the port remains static.

You can also specify the port number in these settings.

Note that this is not in the project's property pages, but in the Properties tool window (which is probably why it's so hard to find - took me quite some time to work this one out myself).

Update

To switch between deployment and development, I tend to specify two SOAP bindings and then use #ifdef DEBUG to switch the binding of my SOAP client based on the type of build. The DEBUG build points to the development services, and the RELEASE build points to the deployed services.

So, my ClientConfig is something like:

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="Soap_Debug" maxBufferSize="2147483647"
          maxReceivedMessageSize="2147483647">
          <security mode="None" />
        </binding>
        <binding name="Soap_Release" maxBufferSize="2147483647"
          maxReceivedMessageSize="2147483647">
          <security mode="None" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://www.mymadeupurl.com/myservices.asmx"
        binding="basicHttpBinding" bindingConfiguration="Soap_Release"
        contract="MyServices.MyServicesSoap" name="Soap_Release" />
      <endpoint address="http://localhost:1929/mydservices.asmx"
        binding="basicHttpBinding" bindingConfiguration="Soap_Debug"
        contract="MyServices.MyServicesSoap" name="Soap_Debug" />
    </client>
  </system.serviceModel>
</configuration>

I create instances of the SOAP client like this:

#if DEBUG
   // localhost hosts the web services in debug builds.
   soapClient = new MyServicesSoapClient("Soap_Debug");
#else
   // The web services are hosted in a different location for release builds.
   soapClient = new MyServicesSoapClient("Soap_Release");
#endif
Jeff Yates
+1 thanks. And then long term how do you solve moving between production server and local development - do you use some code like this: http://stackoverflow.com/questions/539754/silverlight-getting-the-domain-information to pick up the changing domain at the start of your silverlight app?
tyndall
Thanks for the update. Cool.
tyndall
No problem. I struggled with the dynamic port problem for quite some time so I know how frustrating it is.
Jeff Yates
+2  A: 

Like this one: http://forums.silverlight.net/forums/t/19021.aspx

Do not rely on the URL set in the ServiceReference.ClientConfig. Set your URL in the code. Change your WebSerivice Calling code to the following:

      var webService = new YourWebService.YourWebServiceClient() // This is the default constructor, url will be read from the clientconfig file.

      Uri address = new Uri(Application.Current.Host.Source, "../YourService.svc"); // this url will work both in dev and after deploy.

      var webService = new YourWebService.YourWebServiceClient("YourServiceEndPointName", address.AbsolutePath);
Asif