将参数传递给 WiX 自定义操作不起作用

Passing parameter to a WiX custom action not working

我正在尝试通过 wxs 文件将参数传递给 WiX 自定义操作。但我收到以下异常。

Calling custom action CustomActionRemoveFolder!CustomActionRemoveFolder.CustomActions.CreateScheduleTaskForRunningWatchdog
Creating the Scheduled Task for running watch dog
Exception thrown by custom action:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.Deployment.WindowsInstaller.InstallerException: Cannot access session details from a non-immediate custom action
   at Microsoft.Deployment.WindowsInstaller.Session.ValidateSessionAccess()
   at Microsoft.Deployment.WindowsInstaller.Session.get_Item(String property)
   at CustomActionRemoveFolder.CustomActions.CreateScheduleTaskForRunningWatchdog(Session session)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object parameters, Object arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture)
   at Microsoft.Deployment.WindowsInstaller.CustomActionProxy.InvokeCustomAction(Int32 sessionHandle, String entryPoint, IntPtr remotingDelegatePtr)
CustomAction CA_scheduleTaskActionForWatchDog returned actual error code 1603 but will be translated to success due to continue marking

下面是我在我的 wxs 文件中声明和调用参数传递自定义操作的方式。

 <Property Id="UserName" Value="someDefaultValue" />

<CustomAction Id="SetUserName" Property="UserName" Value="[UserName]"/>
 <InstallExecuteSequence>
    <Custom Action="SetUserName" After="InstallInitialize" />
 </InstallExecuteSequence>

我的自定义操作如下所示。

[CustomAction]
public static ActionResult CreateScheduleTaskForRunningWatchdog(Session session)
{
  session.Log("The session value for username is " + session["UserName"]);
}

那我运行微星

 msiexec /i <installer-name> UserName="myName" /l*v log.txt

我在这里做错了什么?任何帮助将不胜感激。

我相信异常说明了这一点:您不能从 deferred 自定义操作访问类似的属性(因为当延迟操作脚本开始执行时,这些属性早已失效) .不要问为什么。 Windows 安装程序 "was designed by most enlightened software astronauts and implemented by most lousy coders" (c) 不是我:)

你可以做什么:

选项 1:将操作 CreateScheduleTaskForRunningWatchdog 设为即时自定义操作。如果不可能/没有意义,请选择选项 2。

方案二:请参考:How to pass CustomActionData to a CustomAction using WiX?