3

system.serviceModel如果 web.config 中尚不存在该部分,如何将其添加到该部分?

前:

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

后:

<system.serviceModel>
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
 <bindings>
      <basicHttpBinding>
        <binding name="ValidationServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="Transport">
            <transport clientCredentialType="Ntlm" />
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
 <bindings>
</system.serviceModel>
4

1 回答 1

5

在 PowerShell 中执行此操作还不错:

$origXml = [xml]@'
<configuration>
<system.serviceModel> 
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
</system.serviceModel> 
</configuration>
'@

$newXml = @'
 <bindings> 
      <basicHttpBinding> 
        <binding name="ValidationServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> 
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
          <security mode="Transport"> 
            <transport clientCredentialType="Ntlm" /> 
            <message clientCredentialType="UserName" /> 
          </security> 
        </binding> 
      </basicHttpBinding> 
 </bindings> 
'@

if (!$origXml.configuration.'system.serviceModel'.bindings)
{
    $tempXmlDoc = new-object System.Xml.XmlDocument
    $tempXmlDoc.LoadXml($newXml)
    $newNode = $origXml.ImportNode($tempXmlDoc.DocumentElement, $true)
    $origXml.configuration.'system.serviceModel'.AppendChild($newNode)
}

请注意,这种方法之所以有效,是因为您要注入的 XML 有一个根元素<bindings>

于 2012-04-27T00:21:04.247 回答