如何在WiX中执行平台无关的.NET兼容性检查?
实现WiX平台无关的.NET 6运行时检查
要解决32位系统崩溃、检查逻辑不区分架构的问题,核心思路是针对不同系统架构分别配置独立的.NET检查逻辑,用WiX的系统架构变量控制检查的执行时机,确保32位系统只检查x86运行时,64位系统只检查x64运行时。
方案一:使用DotNetCoreSearch(推荐)
这种方式更稳定,不会在非目标架构系统上触发无效检查。
首先确保WiX项目引用了NetFx扩展(xmlns声明):
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:netfx="http://schemas.microsoft.com/wix/NetFxExtension">
然后添加分架构的搜索逻辑:
<!-- 仅在64位系统上检查x64版.NET 6 Desktop Runtime --> <netfx:DotNetCoreSearch RuntimeType="desktop" Condition="VersionNT64" Platform="x64" MajorVersion="6" MinimumVersion="6.0.0" Variable="NETCORE_X64_INSTALLED" /> <!-- 仅在32位系统上检查x86版.NET 6 Desktop Runtime --> <netfx:DotNetCoreSearch RuntimeType="desktop" Condition="NOT VersionNT64" Platform="x86" MajorVersion="6" MinimumVersion="6.0.0" Variable="NETCORE_X86_INSTALLED" />
最后配置启动条件,根据当前系统架构匹配对应的检查结果:
<Launch Condition="Installed OR (VersionNT64 AND NETCORE_X64_INSTALLED) OR (NOT VersionNT64 AND NETCORE_X86_INSTALLED)" Message="[ProductName] 需要Microsoft .NET 6.0 Desktop Runtime 6.0.0或更高版本。" />
方案二:修复DotNetCompatibilityCheck的崩溃问题
如果你更倾向于用之前的DotNetCompatibilityCheck,只需给检查节点添加Condition属性,限制其仅在对应架构上执行:
<!-- 仅在64位系统上执行x64兼容性检查 --> <netfx:DotNetCompatibilityCheck Id="NetCoreX64Check" Property="NETCORE_X64_STATUS" RollForward="latestMajor" RuntimeType="desktop" Version="6.0.0" Platform="x64" Condition="VersionNT64" /> <!-- 仅在32位系统上执行x86兼容性检查 --> <netfx:DotNetCompatibilityCheck Id="NetCoreX86Check" Property="NETCORE_X86_STATUS" RollForward="latestMajor" RuntimeType="desktop" Version="6.0.0" Platform="x86" Condition="NOT VersionNT64" /> <!-- 分架构判断启动条件 --> <Launch Condition="Installed OR (VersionNT64 AND NETCORE_X64_STATUS="0") OR (NOT VersionNT64 AND NETCORE_X86_STATUS="0")" Message="[ProductName] 需要Microsoft .NET 6.0 Desktop Runtime 6.0.0或更高版本。" />
关键注意事项
- WiX的
VersionNT64变量是核心区分点:64位系统下该变量为真,32位系统下为假 - 必须为每个架构单独配置检查,不能在32位系统上执行x64的Platform检查,否则会引发崩溃
MinimumVersion属性可以精确指定最低要求版本,比仅设置MajorVersion更严谨
内容的提问来源于stack exchange,提问作者Kagemand Andersen
相关产品推荐
相关产品推荐

