App.config XML transform from NuGet Package

我的未来我决定 提交于 2019-12-11 10:52:02

问题


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.


回答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>

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.



来源:https://stackoverflow.com/questions/49654426/app-config-xml-transform-from-nuget-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!