2

我有一个使用 WCF 服务的应用程序。我需要从应用程序中编辑 baseAddress。如何从 c# 访问 baseAddress。app.config 的部分附在下面请帮助!目标是在代码中获取 baseAddresses 和http://192.xx.xx.xx/Api/TEST_API.asmx值。

<system.serviceModel>
        <services>
          <service name="WCF.Service1" behaviorConfiguration=".Service1.Service1Behavior">
            <endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBinding" contract="WCF.IService1" name="NetTcpBinding">
            </endpoint>
            <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" contract="WCF.IService1" name="BasicHttpBinding">
            </endpoint>
            <!--<endpoint address="rest"
                      binding="webHttpBinding"
                      contract="WCF.IService1" behaviorConfiguration="webby">
            </endpoint>-->
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8084/api/" />
                <add baseAddress="net.tcp://localhost:8083/api/" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding">
            </binding>
          </basicHttpBinding>
          <netTcpBinding>
            <binding name="NetTcpBinding">
              <security mode="None" />
            </binding>
          </netTcpBinding>
        </bindings>
        <behaviors>
          <endpointBehaviors>
            <behavior name="webby">
              <webHttp />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="WCF.Service1.Service1Behavior">
              <!-- To avoid disclosing metadata information, 
              set the value below to false and remove the metadata endpoint above before deployment -->
              <!-- To receive exception details in faults for debugging purposes, 
              set the value below to true.  Set to false before deployment 
              to avoid disclosing exception information -->
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="True" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
      <startup>
        <supportedRuntime version="v2.0.50727" />
      </startup>
      <applicationSettings>
        <TEST.Properties.Settings>
          <setting name="APP_WebReference_API" serializeAs="String">
            <value>http://192.xx.xx.xx/Api/TEST_API.asmx</value>
          </setting>
        </TEST.Properties.Settings>
      </applicationSettings>
    </configuration>
4

1 回答 1

0

像这样的东西:

// read the old value
XmlDocument doc = new XmlDocument();
doc.Load(pathToConfig + "app.config");
XmlNode root = doc.DocumentElement;
XmlNode myNode = root.SelectSingleNode(@"applicationSettings/TEST.Properties.Settings/setting[@name='APP_WebReference_API']");
var oldValue = myNode.InnerText;

// save the new value
myNode.Value = @"http://blah.blah.com/Api/TEST_API.asmx";
doc.Save(pathToConfig + "app.config");
于 2013-06-05T14:36:53.247 回答