Jin-Wook Chung
Jin-Wook Chung

Reputation: 4324

NuGet app.config XDT when target project has no app.config

I have tried to pack a nuget-packge which has a file 'app.config.intall.xdt'. The xdt file is to support XML-Document-Transform (XDT).

If a target project which is going to install the nuget-package has app.config, XDT will correctly be performed, but the problem is that if the target project has no app.config, XDT will do nothing.

Are there any ways to create app.config file if the target project does not have the file, when the XDT operation is required?

The code below shows you my xdt file 'app.config.intall.xdt', and if you want to try to install the nuget package causing the problem, run the following command in the nuget-packager console.

To install the nuget-package

install-package Experiment.Xunit -version 2.0.0-pre08

app.config.intall.xdt

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <runtime xdt:Transform="InsertIfMissing">
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly xdt:Transform="InsertIfMissing"
                         xdt:Locator="Condition(_defaultNamespace:assemblyIdentity/@name='xunit.extensions')">
        <assemblyIdentity name="xunit.extensions" publicKeyToken="8d05b1bb7a6fdb6c" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.9.0.1566" newVersion="1.9.0.1566" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Upvotes: 4

Views: 1442

Answers (1)

giammin
giammin

Reputation: 18958

You can use the Install.ps1 or Init.ps1 hook provided by NuGet.

Create a powershell script that if app.config does not exist it creates it with default value

Another way could be (I cannot test it now) to add an app.config file with the desired value in the content folder of nuget packages.

I think that nuget will not overwrite the file if present in the project root.

---Update---

Nuget documentation: Allow users to overwrite content files that already exist

---Update 2----

I made some tests: you can use Configuration File Transformations instead XDT to have the desired behavior

Upvotes: 1

Related Questions