Silverlight Development - 本地开发时的服务URL

时间:2009-02-19 17:21:17

标签: .net silverlight

开发Silverlight应用程序(在单一解决方案中是Silverlight项目和Web应用程序)的正确模式或方法是什么?我的意思是,如果localhost端口号会不断变化,你如何添加服务引用呢?

感谢。

2 个答案:

答案 0 :(得分:3)

喜欢这个: http://forums.silverlight.net/forums/t/19021.aspx

不要依赖ServiceReference.ClientConfig中设置的URL。在代码中设置您的URL。将您的WebSerivice调用代码更改为以下内容:

      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);

答案 1 :(得分:1)

如果在解决方案资源管理器中选择Web项目,则可以在“属性”工具窗口中更改将阻止端口更改的属性。该属性称为"Use dynamic ports",您希望将其设置为false,以使端口保持静态。

您也可以在这些设置中指定端口号。

请注意,这不在项目的属性页面中,而是在“属性”工具窗口中(这可能是为什么它很难找到 - 我花了很长时间自己完成这个工作)。

更新

要在部署和开发之间切换,我倾向于指定两个SOAP绑定,然后使用#ifdef DEBUG根据构建类型切换SOAP客户端的绑定。 DEBUG构建指向开发服务,RELEASE构建指向已部署的服务。

所以,我的ClientConfig就像:

<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>

我像这样创建SOAP客户端的实例:

#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