Issue with app.config transformation upon installing on new solution

1.6k views Asked by At

I have a package which applies app.config transformations upon project. Transform file looks like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="handlerId" value="$assemblyname$"/> <!--populate from project params-->
    <--other params-->
  </appSettings>
</configuration>

The case is: add package to a new project with no app.config, manually update app.config in project, and then update package.

If I include app.config.transform to my package nuget creates app.config in project, but if i change value of one of parameters and update/reinstall package it creates copy of that parameter with value of package instead of skipping it.

For example: Installed package upon clean project

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="handlerId" value="MyApp"/>
  </appSettings>
</configuration>

Then changed handlerId value to MyApp1 and update package. The result of transformation is

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="handlerId" value="MyApp1"/>
    <add key="handlerId" value="MyApp"/> <!--shouldn't appear!-->
  </appSettings>
</configuration>

I tried to use xdt transformations instead. They work great upon package updating or installing package on project with app.config, but if there is no app.config nuget doesn't create it.

2

There are 2 answers

1
Bo TX On

The .transform process doesn't distinguish the key or value attribute. They are both just attributes. Therefore, it doesn't realize that your particular change is inconsequential, and that it doesn't need to add the defined element again.

http://docs.nuget.org/docs/creating-packages/configuration-file-and-source-code-transformations

When NuGet merges a transform file into a project's configuration file, it only adds elements or adds attributes to existing elements in the configuration file; it does not change existing elements or attributes in any other way.

For all nuGet knows, the value="MyApp" may define something that is required for its particular package, and if that element + attribute(s) aren't there verbatim, the package wouldn't function.

0
Yogi Valani On

Facing the same problem. In my case, the duplicated key is added when our our CI server build the project thus causing tests to fail.

I have worked around this commenting out each element. So my app.config.transform file is:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
     <!--add key="handlerId" value="MyApp"/-->
  </appSettings>
 </configuration>

When you install the package it is up to the user to uncomment the new settings.

Not a great solution, but it works.