NuGet app.config / web.config Transformation Trouble

I recently hit a road bump when developing a NuGet package. Mid-way through making the package, I decided I wanted to include couple of transforms to add some default connection strings to an app.config or a web.config file.  Try as I may, I couldn’t get NuGet to apply the transforms to my project’s web.config file even though I was following NuGet documentation’s instructions to a tee.

It turns out that my problem was related to about 3 things all working together in tandem to defeat me:

First, the project I was testing the installation of this NuGet package on was already checked into source control (TFS).  To test out the package, I would install it and when things didn’t work how I wanted, I’d just “undo pending changes” to get everything back to the last stable state.  Or so I thought (more on this in a moment)…

Second, I had the idea to add app.config.transform and web.config.transform files to my NuGet package Content folder after I’d already installed “version 1” of the package in my project, and rather than bump the version of the package with the transforms in it up, I left it the same because I was still technically editing and troubleshooting the first version of the package (in my mind anyways).

Third, my solution was configured for “NuGet Package Restore”:

Enable NuGet Package Restore

So what was going on?

Choosing “Enable NuGet Package Restore” keeps your NuGet packages folder contents from being checked into source control by adding a NuGet config file with a setting disabling source control integration.  We’d rather just let NuGet restore them if they’re missing when we build, rather than check in the packages folder to source control.

What this meant for me, however, is that all my ”undo pending changes” actions were doing absolutely nothing to undo the installation of the NuGet package I was developing and troubleshooting.  My troubleshooting workflow of [Install package -> Check web.config -> :[ Still missing connection strings -> Undo pending changes -> Fiddle with NuGet Package -> Rinse and repeat] could have been infinite, because the “undo pending changes” step wasn’t undoing the NuGet package installation, because _the NuGet package files weren’t part of the pending change set _due to my “Enable NuGet Package Restore” setting.  The NuGet package was still in the packages folder on my hard disk, which had another hidden consequence: each subsequent “installation” of the NuGet package effectively did nothing because it was already there.  Remember, I didn’t bump the version number of the package, so I’m guessing NuGet already saw the files there and just updated the config file and called it good.  None of the updates I was making to the package were ever updated.


I went back to the last stable checked-in state for my solution.
Next, I navigated to my solution folder by right-clicking my solution and choosing “Open Folder in File Explorer”:

Open Folder in File Explorer

I double-clicked on the packages folder:

Packages Folder

Finally, I deleted the folder for the package I was developing.

After these steps were performed, installing the NuGet package again worked perfectly – the config transforms were applied and life was good.

Alternatively, I suppose I could have upped the version number of my package and things would have been fine as well.  But I always tend to make things more complicated than they need to be. :]


comments powered by Disqus