来自NuGet包的App.config XML转换 [英] App.config XML transform from NuGet Package

查看:133
本文介绍了来自NuGet包的App.config XML转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循

.nuspec文件包含以下内容:

<?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>

这在运行nuget pack时将文件正确地插入.nupkg文件中的content文件夹中.

在Visual Studio 2017(15.6.4)中,当将NuGet包添加到具有现有原始App.config文件的全新控制台应用程序时,不会进行任何转换(即App.config的内容不变). XDT转换也是如此.

任何人都可以建议为什么转换失败吗?

更新

我在目标.NET Framework 4.7.1项目中将PackageReference用作程序包管理格式.

解决方案

在Visual Studio 2017(15.6.4)中,当将NuGet程序包添加到具有现有原始App.config文件的全新控制台应用程序时,不会进行任何转换.

"不进行转换"是什么意思? appSetttings部分下的新元素和属性未合并到App.config?

我已经用您的.nuspecapp.config.transform文件创建了nuget包:

裸包: https://1drv.ms/u/s!Ai1sp_yvodHf1QFNsI3ybdw2nanD /p>

然后将其设置为我的本地提要,创建一个新的.net控制台应用程序并将该nuget包添加到测试项目中,您会发现appSetttings部分下的新元素和属性被合并到App.config中,因此App.config在安装该nuget软件包后将如下所示:

<?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>

所以应该没问题.一种可能是您在安装此软件包时没有清理缓存,要确认这一点,请删除解决方案文件夹下的packages文件夹.

注意:

请注意,NuGet不会替换startup部分,它只是通过添加新元素和属性将新条目合并到其中. NuGet不会更改任何现有元素或属性.

更新:

当执行与您完全相同的步骤时,这对我不起作用,但是我使用PackageReference而不是packages.config.这是PackageReference的错误吗?

不是,这不是PackageReference的错误.根据文档转换源代码和配置文件,前两行:

对于使用packages.config的项目,NuGet支持在软件包安装和卸载时对源代码和配置文件进行转换的功能.使用PackageReference在项目中安装软件包时,仅应用源代码转换.

我们可能知道packages.config支持对源代码和配置文件进行转换的功能,但是仅将源代码转换应用于PackageReference .这就是为什么您无法使用PackageReference进行此工作的原因.

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.

解决方案

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.

这篇关于来自NuGet包的App.config XML转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆