在App.Config中更改Web服务URL

时间:2012-04-18 17:21:14

标签: visual-studio-2010 web-services sharepoint sharepoint-2010

我有一个小型的sharepoint项目,它集成到一个更大的sharepoint项目中。我的项目使用两个Web服务。我使用Web Reference中的VS2010(嵌入在服务参考中)对话框添加了它们。我们有两套Web服务 - 一组用于测试,一组用于生产。当我在本地部署应用程序时,Web服务设置将写入位于本地计算机上C:\inetpub\wwwroot\wss\VirtualDirectories\80\的web.config文件。该部分看起来像这样 -

 <applicationSettings>
    <XXX.YYY.Properties.Settings>
      <setting name="XXX_YYY_ZZZ_WS1" serializeAs="String">
        <value>http://<TEST_IPAddress>/WebService/WS1.asmx</value>
      </setting>
      <setting name="XXX_YYY_ZZZ_WS2" serializeAs="String">
        <value>http://<TEST_IPAddress>/WebService/WS2.asmx</value>
      </setting>
    </XXX.YYY.Properties.Settings>
  </applicationSettings>

测试和生产Web服务之间的区别仅仅是IP地址。当我将IP地址更改为生产时,应用程序未使用新值。我不得不回到VS,将Web Reference URL对话框中的Properties更新为正确的生产网址,然后重新部署该软件包。这是乏味的,因为我一直在不断地从测试网络服务网站切换到生产网络服务网址。我希望能够在app.config中更改IP地址,在浏览器中刷新页面,它应该选择新的URL。

我做错了吗?还有另一种方法吗?

2 个答案:

答案 0 :(得分:1)

我认为如果您在代码中更改了webservice url,那么您将不必重复构建过程。你可以这样改变

WebServiceObjectName webService = new WebServiceObjectName (); 
webService.Uri = [IPaddress or DNS name]

答案 1 :(得分:0)

我们这样做:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IPublicWS"
                openTimeout="00:00:05"
                sendTimeout="00:03:00"
                receiveTimeout="00:10:00"
                closeTimeout="00:00:30"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="262144" maxBufferPoolSize="524288" maxReceivedMessageSize="262144"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
              <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="131072" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
              <security mode="None">
                <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
                <message clientCredentialType="UserName" algorithmSuite="Default" />
              </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <!-- Production -->
        <endpoint name="SvLive" address="http://sv.com/PublicWS/PublicWS.svc/PublicWS" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPublicWS" contract="SV.IPublicWS" />

        <!-- Test -->
        <endpoint name="SvTest" address="http://staging.sv.com/PublicWS/PublicWS.svc/PublicWS" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPublicWS" contract="SV.IPublicWS" />
    </client>
</system.serviceModel>

然后,获取客户端对此Web服务的引用:

public static PublicWSClient Client()
{
#if PRODUCTION
    return new PublicWSClient("SvLive");
#else
    return new PublicWSClient("SvTest");
#endif
}

这叫做:

var sv = PublicWSClient.Client();

这可以防止您描述的任何手动步骤,并允许在准备签入的单个配置文件中捕获测试和实时。