You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wix延迟操作访问即时操作属性时出现键不存在异常

延迟自定义操作读取属性失败:KeyNotFoundException错误

我参考多篇资料、Stack Overflow帖子及Wix安装程序书籍,尝试通过即时自定义操作设置属性,再在延迟自定义操作中读取,但操作失败并回滚,日志中持续出现**System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.**错误。

.wxs文件片段

<!-- Custom Actions -->
<!-- Reference library for custom actions-->
<Binary Id="myCustomActions" SourceFile="$...something.CA.dll"/>

<!-- Set my properties that will be passed on to InsertPluginData as its a deferred CA and not able to read properties -->
<CustomAction
  Id="CA_SetProperties"
  BinaryKey="myCustomActions"
  DllEntry="SetProperties"
  Execute="immediate"
  />

<!-- This modifies the CSI File to insert the plugin into the interface -->
<!-- Eliminates the user having to do this manually -->
<CustomAction
  Id="CA_InsertPluginData"
  BinaryKey="myCustomActions"
  DllEntry="InsertPluginData"
  Execute="deferred"
  Return="check"
  />

<!-- Custom Actions Sequence -->
<InstallExecuteSequence>
  <Custom Action="CA_SetProperties" After="InstallInitialize" />
  <Custom Action="CA_InsertPluginData" Before="InstallFinalize"/>
</InstallExecuteSequence>

CustomActions.cs代码

[CustomAction]
public static ActionResult SetProperties(Session session)
{
    session.Log("Begin SetProperties");
    
    CustomActionData data = new CustomActionData();
    data["Test"] = "1";

    session["InsertPluginData"] = data.ToString();
    session.Log("End SetProperties");
    return ActionResult.Success;
}


[CustomAction]
public static ActionResult InsertPluginData(Session session)
{
    session.Log("Begin InsertPluginData");
    CustomActionData data = session.CustomActionData;
    string property1 = data["Test"];
    session.Log("Begin InsertPluginData: Test" + property1);

    return ActionResult.Success;
}

日志信息

Begin SetProperties
MSI (s) (D4!EC) [10:30:17:941]: PROPERTY CHANGE: Adding InsertPluginData property. Its value is 'Test=1'.
End SetProperties

问题原因及解决方案

核心问题

延迟自定义操作无法直接访问普通MSI属性,必须通过与该自定义操作ID同名的属性来传递CustomActionData。你当前在即时操作中设置的属性名是InsertPluginData(对应DllEntry的方法名),但延迟自定义操作的ID是CA_InsertPluginData,两者不匹配,导致延迟操作读取session.CustomActionData时找不到对应数据,抛出KeyNotFoundException。

修复步骤

  1. 修正即时自定义操作的属性名
    将SetProperties方法中的属性名改为延迟自定义操作的ID:
session["CA_InsertPluginData"] = data.ToString();
  1. 推荐:给延迟自定义操作添加Impersonate属性
    延迟操作通常需要系统权限,建议在.wxs的延迟自定义操作配置中添加Impersonate="no",避免权限问题:
<CustomAction
  Id="CA_InsertPluginData"
  BinaryKey="myCustomActions"
  DllEntry="InsertPluginData"
  Execute="deferred"
  Return="check"
  Impersonate="no"
  />

验证

修改后重新编译安装包,运行时日志会显示PROPERTY CHANGE: Adding CA_InsertPluginData property,延迟操作即可正常读取Test属性的值。

内容的提问来源于stack exchange,提问作者MElSawy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 22:21:38