This should be fairly straight forward as mentioned on nuget.org’s site right? Well, not quite. I’ve spent some time reading through the blog posts and it’s not quite straightforward. Hopefully this post is the simplified version. In my case, the scenario is simply to add entries in the appSettings key node within the app.config file. Nuget.org’s site has the following docs:
Configuration File and Source Code Transformations
https://docs.nuget.org/create/configuration-file-and-source-code-transformations
How to use XDT in NuGet – Examples and Facts
http://blog.nuget.org/20130920/how-to-use-nugets-xdt-feature-examples-and-facts.html
The steps below will hopefully guide you through the initial steps to get your app.config (or web.config) files to be modified during and after installing your nuget packages. After which you can look at all different XDT transformation processes in the following doc:
Web.config Transformation Syntax for Web Project Deployment Using Visual Studio
https://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx
Step 1: Create both app.config.install.xdt and app.config.uninstall.xdt
From Nuget site: “Starting with NuGet 2.6, XML-Document-Transform (XDT) is supported to transform XML files inside a project. The XDT syntax can be utilized in the .install.xdt and .uninstall.xdt file(s) under the package’s Content folder, which will be applied during package installation and uninstallation time, respectively.”
The location of these files don’t quite matter. If these files are located in the same directory as where you have your assemblies for nuget package, even better. You’ll need to reference these 2 files as “content” folder locations in the .nuspec file. Nuspec file is the blue print for creating your nuget package.
app.config.install.xdt
<?xml version="1.0"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings xdt:Transform="InsertIfMissing"> </appSettings> <appSettings> <add key="Key1" xdt:Transform="Remove" xdt:Locator="Match(key)" /> <add key="Key1" value="Value1" xdt:Transform="Insert"/> <add key="Key2" xdt:Transform="Remove" xdt:Locator="Match(key)"/> <add key="Key2" value="Value2" xdt:Transform="Insert" /> </appSettings> </configuration>
Let’s break this down. There are 2 appSettings node in this xml file. One to check if the appSettings node exist (InsertIfMissing) and the 2nd, if it does exist, it will remove the key value pair matching the keyword and then add it again. Why do this 2 step process? This is to ensure that you will only have one entry per key. However, you could probably get away using InsertIfMissing as well.
app.config.uninstall.xdt
<?xml version="1.0"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings xdt:Transform="InsertIfMissing"> </appSettings> <appSettings> <add key="Key1" xdt:Transform="Remove" xdt:Locator="Match(key)" /> <add key="Key2" xdt:Transform="Remove" xdt:Locator="Match(key)"/> </appSettings> </configuration>
Step 2: Modify your nuspec file to include both the .install.xdt and .uninstall.xdt file(s) as content folders.
.nuspec file is the core or blue print for generating your nuget package. Here’s an example of a .nuspec file. For more information, go here: http://docs.nuget.org/Create/Nuspec-Reference
In this example, you’ll need to refer for both .install.xdt and .uninstall.xdt file(s) as target content folders:
<?xml version="1.0" encoding="utf-8"?> <package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"> <metadata> <id>Package1</id> <version>1.1</version> <title>Nuget Package 1</title> <authors>QE Dev</authors> <owners>Don Tan</owners> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>Package 1 Testing</description> <summary>Application Config change</summary> <releaseNotes> - Support for Application Config change </releaseNotes> <copyright>Copy Right</copyright> <language>en-US</language> <dependencies> <dependency id="Microsoft.ApplicationInsights" version="2.1.0" /> </dependencies> <references> <reference file="Package1.dll" /> </references> </metadata> <files> <file src="Package1.dll" target="lib\net45\Package1.dll.dll" /> <!--Add Section to Uninstall and Re-install Application.Config files--> <file src="app.config.install.xdt" target="content" /> <file src="app.config.uninstall.xdt" target="content" /> </files> </package>
Step 3: Test the generated nuget package and verify if your application config (app.config) settings have been modified