向app.config添加了新的应用程序设置,但MSI不会安装它(不会覆盖)

问题描述:

我们最近在旧版Winforms应用程序(.Net 4.6.1)的app.config(指向日志服务器的URL)中添加了新的应用程序设置(而非用户设置)。

We recently added a new application setting (not user setting) to the app.config (URL to a logging server) of our legacy winforms application (.Net 4.6.1).

旧版本为1.0.3,我们将所有程序集的版本更改为1.0.4,并在安装项目(Visual Studio 2017安装程序项目)中将版本更改为匹配,从而放弃了弹出窗口以更改产品代码,我们就是这样做的。

Old version was 1.0.3, we changed the version of all assemblies to 1.0.4 and in the Setup project (Visual Studio 2017 Installer Project) changed the version to match which gave up the pop-up to change the product code, which we did.

安装正确运行(顺便说一句,其他内容也已更改,并且这些更改在新版本中正确),但是我们的新产品app.config设置不是。奇怪的是,如果您手动删除配置文件并重新运行应用程序,它将重新创建配置文件,从而导致出现新设置。知道这里发生了什么吗?

The install runs correctly (other things were changed too by the way, and those changes are correctly in the new version), but our new app.config setting is not. Curiously, if you manually delete the config file and re-run the app, it re-creates the config file which causes the new setting to appear. Any idea what's happening here?

谢谢!

不是使用WiX,而是在我没有注意到您没有将WiX添加为标记之前添加我写的内容。学会学习阅读。

You might not be using WiX, but I'll just add what I wrote before I noticed that you didn't add WiX as a tag. Got to learn to read.

这可能是MSI / WiX部署中最常见的问题,而且擦除了大型升级期间退出配置设置。我假设您已在安装过程中将 app.config 文件设置为永久文件,以在重大升级时将其保存?

This might be the most common problem of all for MSI / WiX deployment, along with wiped out config settings during major upgrade. I assume you have set the app.config file permanent during installation to preserve it during major upgrade?

可能发生的事情是将配置文件安装为文件,但应将其作为一堆XML配置设置安装,可以将其合并到目标文件中。

What is likely happening is that you have installed the config file as a file, but it should be installed as a bunch of XML configuration settings that can be merged into the destination file instead.

MSI文件版本控制规则试图保留安装后已修改的非版本文件。因此,如果文件的创建和修改日期在升级时不同,则未版本化的文件将不会被覆盖。没有最新的所需值,文件将保持不变。

The MSI file versioning rules attempt to preserve non-versioned files that have been modified after installation. Accordingly a non-versioned file will not be overwritten if the create and modify dates of the file are different on upgrades. Your file will appear untouched without the most recent, desired values. It has been "preserved".

您可以使用适当的WiX XML元素更新WiX源以设置所需的值。有两个相关的不同元素:

You can update your WiX source to set the required values using the appropriate WiX XML elements. There are two different elements that are relevant:

  • XmlConfig Element
  • XmlFile Element

关于这两个元素之间的区别,我引用 Bob Arnson (WiX开发人员): 您可以做XmlFile支持的一切(以及更多)使用XmlConfig,但需要额外的创作,而XmlFile则不需要。
XmlFile最适合修改正在安装的XML文件(例如,添加反映文件已安装路径的属性);无法删除在卸载时进行修改,但是如果您的安装程序安装了该文件,则无论如何都会将其卸载。XmlConfig最适合修改共享XML文件,因为它支持取消安装
)。

With regards to the differences between these two elements, I quote Bob Arnson (WiX developer): "You can do everything (and more) that XmlFile supports with XmlConfig but it requires extra authoring that's not necessary with XmlFile. XmlFile is best at modifying XML files that you're installing (e.g., to add an attribute reflecting the installed path of a file); it can't remove the modifications at uninstall time but if your setup installed the file, it will be uninstalled anyway. XmlConfig is best at modifying shared XML files because it supports uninstalling the modifications." (source).

我非常喜欢这种XML内容,并且我可能不会使用最新最好的技术,但这是一个快速示例。彻底测试-尤其是卸载和升级方案-有限的测试由我自己完成:

I find this XML stuff quite fiddly, and I may not use the latest and greatest techniques, but here is a quick sample. Test thoroughly - particularly uninstall and upgrade scenarios - limited testing done on my part:

这是我的测试XML文件(实际上是作为文件安装,然后更新)。请检查保存文件的编码-已报告一些编码问题-不确定当前状态是什么:

Here is my test XML file (that is actually installed as a file and then updated). Do check the encoding of the file you save - some issues with encoding have been reported - not sure what the current status is:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <ExistingConfig>
    <bindingRedirect oldVersion="0.0.0" newVersion="0.0.0" />
  </ExistingConfig>
</configuration>

以下是将更新文件(并安装文件)的WiX代码段。将上述XML测试文件放在WXS源文件旁边,或在构建MSI之前指定正确的源路径。

Here is the WiX snippet that will update the file (and install the file). Put the above XML test file next to your WXS source file, or specify the correct source path before building your MSI.

<Component Feature='ProductFeature'>

 <!--Installs the base file-->
 <File Source='app.config' />

 <!--Create New Element-->
 <util:XmlFile Id='XmlSettings1' File='[#app.config]' Action='createElement' 
               Name='MyConfig' ElementPath='//configuration' Sequence='1' />

 <!--Set New Value-->
 <util:XmlFile Id='XmlSettings2' File='[#app.config]' Action='setValue' 
              Name='newVersion' Value='6.6.8' ElementPath='//configuration/MyConfig' Sequence='2' />

<!--Set New Value-->
<util:XmlFile Id='XmlSettings3' File='[#app.config]' Action='setValue' 
              Name='Server' Value='Pusevov' ElementPath='//configuration/MyConfig' Sequence='3' />

<!--Update Existing Value, Existing Element-->
<util:XmlFile Id='XmlSettings4' File='[#app.config]'
  Action='setValue' Name='newVersion' Value='7.7.7' ElementPath='//configuration/ExistingConfig/bindingRedirect' Sequence='4' />

</Component>

我希望这是有道理的。就像我说的那样,我有时会发现这种情况很容易发生并且容易出错,但是其他用于此类更新的工具也是如此。切记首先尝试使用原始测试用例并进行测试安装以测试运行时错误。缩小工作范围,然后再进行构建-这显然是不言而喻的,但是很容易尝试全部尝试。 在洞中开火,寻求掩护!。

I hope that makes some kind of sense. Like I said, I find this fiddly and error prone at times, but so are other tools for this kind of updates. Remember to try first with a primitive test case and do test installs to test for runtime errors. Make something small that works and then build on it - which is obviously self-evident, but it is tempting to try it all in one go. "Fire in the hole, seek cover!".

一些维护方面的链接:

  • Firegiant Tutorial on XML
  • Deleting XML element with XmlConfig extension in WIX using XPath
  • WIX util:xmlfile File name is Source attribute
  • WIX v3 and XmlConfig / XmlFile troubleshooting (resurrected from Wayback Machine)
  • Very step-by-step blog on WiX and XML
  • How do I force wix to update a file before manipulating it on an upgrade
  • Getting app settings for config files during WiX based install