Search code examples
c#wixwindows-installercustom-actionwix3.10

Passing parameter to a WiX custom action not working


I am trying to pass a parameter to a WiX custom action through wxs file. But I am getting the below exception.

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

Below is how I am declaring and calling the parameter passing custom action in my wxs file.

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

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

And my custom action looks like this.

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

Then I am running the msi as

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

What am I doing wrong here? Any help would be much appreciated.


Solution

  • I believe the exception says it: you cannot access properties like that from a deferred custom action (because those are long dead by the time when a deferred action script starts executing). Don't ask why. Windows Installer "was designed by most enlightened software astronauts and implemented by most lousy coders" (c) not me :)

    What you could do:

    Option 1: Make action CreateScheduleTaskForRunningWatchdog an immediate custom action. If not possible / does not make sense, go for option 2.

    Option 2: Please refer to: How to pass CustomActionData to a CustomAction using WiX?