【问题标题】:Merging web.config and other .config files合并 web.config 和其他 .config 文件
【发布时间】:2013-07-18 15:21:35
【问题描述】:

我将 web 服务的引用动态添加到项目中,我需要将有关它们的信息添加到 web.config 中。

svcutil 优雅地添加了包含“system.serviceModel”节点和子节点的配置文件。

我正在寻找的是如何将这些文件中的信息合并到现有的 web.config 中。我希望“configSource”属性能有所帮助,但是,它不能用于“system.serviceModel”部分组,而只能用于它的内容。但是,与修改 web.config 本身相比,从所有配置中拆分“system.serviceModel”节点需要相同甚至更多的解析。

我想知道,是否还有其他选项可以重用 web.config 中子配置文件中的数据?特别是当涉及到整个部门组时?

【问题讨论】:

    标签: .net xml web-config


    【解决方案1】:

    由于没有建议其他解决方案,我创建了一个函数来手动修改 web.config 并从较小的配置中复制数据。

    以防万一有人觉得它有用或提出更好的方法:

    # Changes web.config: adds into system.serviceModel group data for binding and for endpoint for the webservice
    Function add-config-source
    {
        Param($configFile)
    
            if(($configFile -eq "") -or ($configFile -eq $null))
            { $errors = $errors + " Error: path to webservice configuration file was not found. "; }
    
            # get data from the web service config file
            $webServiceConfigXml = [xml](get-content $configFile)
            # cloning elements that we need
            $bindingNodeClone = $webServiceConfigXml.SelectSingleNode("//binding").Clone();
            $endpointNodeClone = $webServiceConfigXml.SelectSingleNode("//endpoint").Clone();
            $serviceModelNodeClone = $webServiceConfigXml.SelectSingleNode("//system.serviceModel").Clone();
    
            # reading and modifying web.config
            $webConfigXml = New-Object xml
            # find the Web.config file
            $config = $project.ProjectItems | where {$_.Name -eq "Web.config"}
            # find its path on the file system
            $localPath = $config.Properties | where {$_.Name -eq "LocalPath"}
            # load Web.config as XML
            $webConfigXml.Load($localPath.Value)
    
            # select the node
            $configurationNode = $webConfigXml.SelectSingleNode("configuration")
    
            # check if 'system.serviceModel' node exists
            $serviceModelNode = $configurationNode.SelectSingleNode("system.serviceModel");
    
            if ($serviceModelNode -eq $null) 
            {
                $serviceModelNodeClone = $webConfigXml.ImportNode($serviceModelNodeClone, $true);
                $configurationNode.AppendChild($serviceModelNodeClone);
            }
            else
            {
                $existingBasicHttpBindingNode = $serviceModelNode.SelectSingleNode("//basicHttpBinding");
                $bindingNodeClone  = $webConfigXml.ImportNode($bindingNodeClone, $true);
                $existingBasicHttpBindingNode.AppendChild($bindingNodeClone);
    
                $existingClientNode = $serviceModelNode.SelectSingleNode("//client");
                $endpointNodeClone = $webConfigXml.ImportNode($endpointNodeClone, $true);
                $existingClientNode.AppendChild($endpointNodeClone);
                $configurationNode.AppendChild($serviceModelNode);
            }
    
            # save the Web.config file
            $webConfigXml.Save($localPath.Value)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      相关资源
      最近更新 更多