代码之家  ›  专栏  ›  技术社区  ›  BuddyJoe

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

  •  1
  • BuddyJoe  · 技术社区  · 15 年前

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

    谢谢。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Jeff Yates    15 年前

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

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

    请注意,这不在项目的属性页中,而是在属性工具窗口中(这可能就是为什么很难找到的原因——我花了相当长的时间自己解决这个问题)。

    更新

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

    所以,我的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
    
        2
  •  3
  •   Asif    15 年前

    像这样: http://forums.silverlight.net/forums/t/19021.aspx

    不要依赖servicereference.clientconfig中设置的URL。在代码中设置URL。将Web服务调用代码更改为以下代码:

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