当您使用 WiX 创建 C# 自定义操作项目时,默认情况下会创建一个名为 CustomAction.config 的 XML 文件。此文件的目的是指示 sfxca.dll 在运行代码时使用哪个版本的 CLR。该文件的默认实现如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<!--
Use supportedRuntime tags to explicitly specify the version(s) of the .NET Framework runtime that
the custom action should run on. If no versions are specified, the chosen version of the runtime
will be the "best" match to what Microsoft.Deployment.WindowsInstaller.dll was built against.
WARNING: leaving the version unspecified is dangerous as it introduces a risk of compatibility
problems with future versions of the .NET Framework runtime. It is highly recommended that you specify
only the version(s) of the .NET Framework runtime that you have tested against.
Note for .NET Framework v3.0 and v3.5, the runtime version is still v2.0.
In order to enable .NET Framework version 2.0 runtime activation policy, which is to load all assemblies
by using the latest supported runtime, @useLegacyV2RuntimeActivationPolicy="true".
For more information, see http://msdn.microsoft.com/en-us/library/bbx34a2h.aspx
-->
<supportedRuntime version="v4.0" />
<supportedRuntime version="v2.0.50727"/>
</startup>
<!--
Add additional configuration settings here. For more information on application config files,
see http://msdn.microsoft.com/en-us/library/kza1yk3a.aspx
-->
</configuration>
基本上,允许用 .NET 2.0/3.0/3.5(所有 CLR 2.0)编写的 CA 对 4.0 运行是一个很好的做法,因为您可能会遇到仅安装了 4.0 的机器。一般来说,您的代码应该适用于 4.0,如果不适用,我会修复它。
或者,您可以更新配置文件以仅允许在 2.0 上运行,然后将所需的检查放入安装程序以确保实际安装了此版本的 CLR。