2

I am attempting to follow these instructions to perform a simple XML transform on a target App.config file via a custom NuGet package.

The app.config.transform file contains the following content:

<configuration>
  <appSetttings>
    <add key="foo" value="bar" />
  </appSetttings>
</configuration>

The .nuspec file contains the following content:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>Foo.Bar</id>
    <version>1.0.0</version>
    <title>Foo Bar</title>
    <authors>Foo</authors>
    <description>Bar</description>
  </metadata>
  <files>
    <file src="app.config.transform" target="content" />
  </files>
</package>

This correctly inserts the file into the content folder in the .nupkg file when running nuget pack.

In Visual Studio 2017 (15.6.4) when adding the NuGet package to a brand new Console Application with an existing vanilla App.config file, no transforms take place (i.e. the content of App.config is unchanged). This is also the case with XDT transforms.

Can anybody advise why the transformation fails to take place?

UPDATE

I am using PackageReference as my package management format in the target .NET Framework 4.7.1 project.

wolfyuk
  • 746
  • 1
  • 8
  • 26

1 Answers1

1

In Visual Studio 2017 (15.6.4) when adding the NuGet package to a brand new Console Application with an existing vanilla App.config file, no transforms take place.

What do you mean "no transforms take place"? The new elements and attributes under the appSetttings section not merged into the App.config?

I have created the nuget package with your .nuspec and app.config.transform files:

nuget package: https://1drv.ms/u/s!Ai1sp_yvodHf1QFNsI3ybdw2nanD

Then set it to my local feed, create a new .net console application and add that nuget package to the test project, you will find the new elements and attributes under the appSetttings section are merged into the App.config, so the App.config would be like this after install that nuget package:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
    </startup>

   <appSetttings>
    <add key="foo" value="bar" />
   </appSetttings>

</configuration>

enter image description here

So it should be works fine. One possibility is you have not clean the cache when you install this package, to confirm this, please delete the packages folder under the solution folder.

Note:

Notice that NuGet didn't replace the startup section, it just merged the new entry into it by adding only new elements and attributes. NuGet will not change any existing elements or attributes.

Update:

This does not work for me when performing exactly the same steps as you, but I am using PackageReference instead of packages.config. Is this a bug with PackageReference?

Not, it is not a bug with PackageReference. According to doc Transforming source code and configuration files, the top two lines:

For projects using packages.config, NuGet supports the ability to make transformations to source code and configuration files at package install and uninstall times. Only Source code transformations are applied when a package is installed in a project using PackageReference.

We could to know packages.config support the ability to make transformations to source code and configuration files, However, Only Source code transformations are applied to the PackageReference. That is reason why you could not get that work with PackageReference.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • This does not work for me when performing exactly the same steps as you, but I am using `PackageReference` instead of `packages.config`. When switching back to `packages.config` this does indeed work as it has done for you. Is this a bug with `PackageReference`? – wolfyuk Apr 05 '18 at 11:20
  • @WolfyUK, Not, it is not a bug with `PackageReference`. That because only Source code transformations are applied to the `PackageReference`, it not support transforming configuration files currently. Check my updated answer for details. – Leo Liu Apr 05 '18 at 11:37
  • Thanks. I have found a sample way of achieving transforms for `PackageReference` with MSBuild targets on [NuGet Samples](https://github.com/NuGet/Samples/tree/master/XDTransformExample) – wolfyuk Apr 06 '18 at 08:01
  • @wolfyuk that sample was made to work but looks like it does not work for me. No transformation takes place at all. – Hopeless Jul 21 '22 at 10:58