【问题标题】:Passing parameter to a WiX custom action not working将参数传递给 WiX 自定义操作不起作用
【发布时间】:2017-05-15 03:09:47
【问题描述】:

我正在尝试通过 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"]);
}

然后我将 msi 运行为

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

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

【问题讨论】:

    标签: c# wix windows-installer custom-action wix3.10


    【解决方案1】:

    我相信异常说明了这一点:您无法从 deferred 自定义操作中访问类似的属性(因为在延迟操作脚本开始执行时,这些属性早已失效)。不要问为什么。 Windows 安装程序“由大多数开明的软件宇航员设计,并由大多数糟糕的编码人员实施”(c)不是我 :)

    你能做什么:

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

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

    【讨论】:

    • 将自定义操作 CreateScheduleTaskForRunningWatchdog 更改为立即后,该异常就消失了。但是现在我在自定义操作 CreateScheduleTaskForRunningWatchdog 中获得了 session["UserName"] 的默认值。您在这里看到任何其他问题吗?
    • 您可能需要设置正确的顺序..首先设置属性,然后获取它。在 ExecuteSequence 中使用“After”属性...意味着 CreateScheduleTaskForRunningWatchdog 应该在 SetUserName 之后进行
    • 未安装
    • 以上是我的感受。它仍然没有传入的值。我可以在日志中看到传递的值。在“Doing action: SetUserName”日志之后,我看到的值是默认值:(
    • 等一下 - 不是 SetUserName 将属性分配给自己(这不是什么都不做的操作)吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多