0

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

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

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

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

4

1 回答 1

-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)
}
于 2013-07-22T14:02:06.193 回答