0

I have a Visual Studio 2019 solution that looks like this and I want to package it into a nuget repo:

-Solution
  +Project1
     (no web.config file)
  +Project2
    -web.config.transform
       <appSettings> 
           <add key="Project2Key" value="p2value" />
        </appSettings>
  +Project3
    -web.config.transform
       <appSettings> 
          <add key="Project3Key" value="p3value" />
        </appSettings>

        <customSettingsForProject3> 
          .....
        </customSettingsForProject3>
  +MainUtilsProject
    -web.config.transform
       <appSettings> 
           <add key="MainUtilsProjectKey" value="mainValue" />
        </appSettings>

When it compiles it runs the nuget pack command to generate a nuget package (all that works fine). The problem is that only the web.config.transform in the MainUtilsProject gets packaged. The other web.configs are either not copied, overwritten or not transformed. The "Copy Always" is set on those files.

The nuspec has

<file src="web.config.transform" target="Content" />

in the files node.

I know that they are all the same name and that the last one in will be the only one that exists after the build, but if I rename the web.configs in the other projects they all get copied to the bin folder but the nuget pack command fails if I try to reference them saying "file not found".

The ultimate aim is to have the consuming project that downloads the nuget package install the relevant sections of the web.configs into it's own web.config. And this works perfect but only for the MainUtilsProject file (In the example below it only has the key for the MainUtilsProject inserted)

So how can I get the web.configs for all the projects merged into one file that gets packed into the nuget package? Bearing in mind that the projects can also exist as independent projects with their own web.configs.

In the consuming project, after I install the nuget package, I would like to see this in the web.config file:

<appSettings> 
  <add key="Project2Key" value="p2value" />
  <add key="Project3Key" value="p3value" />
  <add key="MainUtilsProjectKey" value="mainValue" />
</appSettings>

<customSettingsForProject3> 
  .....
</customSettingsForProject3>

Is it even possible?

Thanks

DeclanMcD
  • 1,518
  • 4
  • 22
  • 41

1 Answers1

1

How to Nuget package/transform multiple web.config files

I think you should first merge all the web.config files into a single file in your project and then pack this new single web.config file into nuspec which will meet your requirements.

1) first install MSBuildTasks nuget package into MainUtilsProject

2) add these custom targets into MainUtilsProject to merge these web config files into a new single file:

<Target Name="merge1" BeforeTargets="Build">

    <XmlMassUpdate
               ContentFile="web.config.transform"
               ContentRoot="configuration"
               SubstitutionsFile="..\Project2\web.config.transform"
               SubstitutionsRoot="configuration/appSettings" 
               MergedFile="web.new1.config.transform"
            />
  </Target>

  <Target Name="merge2" AfterTargets="merge1">
    <XmlMassUpdate
           ContentFile="web.new1.config.transform"
           ContentRoot="configuration"
           SubstitutionsFile="..\Project3\web.config.transform"
           SubstitutionsRoot="configuration"
           MergedFile="web.new2.config.transform"
            />
    <Delete Files="web.new1.config.transform"></Delete>

  </Target>
  <Target Name="merge3" AfterTargets="merge2">
    <XmlMassUpdate
           ContentFile="web.new2.config.transform"
           ContentRoot="configuration/appSettings"
           SubstitutionsFile="App.config.transform"
           SubstitutionsRoot="configuration"
           MergedFile="web.new.finally.config.transform"
            />
    <Delete Files="web.new2.config.transform"></Delete>

  </Target>

3) then pack web.new.finally.config.transform into nuspec file

<file src="web.new.finally.config.transform" target="Content" />

Note: if you use PackageReference format to install this package, you should use contentfiles node to declare this file. See this issue.

In addition, these target files have some logic based on your requirements, so the methods I provide are specific to your specific project file structure.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • When you say "add these custom targets into MainUtilsProject" is that a file or a section of a file in that project? Also, the consuming project could be either packageReference or packageConfig does that make a difference? – DeclanMcD Apr 07 '20 at 15:52
  • Scratch above, found it. That xml gets added to the project file. Thanks – DeclanMcD Apr 07 '20 at 16:26