简体   繁体   中英

Merging web.config and other .config files

I dynamically add references to webservices into a project and I need to add information about them into web.config.

svcutil gracefully adds config files containing a "system.serviceModel" node with and subnodes.

What I'm looking for is to how to merge information from these files into the existing web.config. I hoped that 'configSource' attribute could help, however, it cannot be used on the "system.serviceModel" section group, but only on content of it. However, splitting "system.serviceModel" nodes from all configs will require the same or even more parsing, comparing to modifying web.config itself.

I wonder, if there is another options to reuse data from child config files in the web.config? Especially, when a whole section group is involved?

As no other solutions were suggested, I created a function to modify web.config manually and copy data from smaller configs.

Just in case someone finds it useful or suggests a better approach:

# 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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM