0

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.

AnOldSoul
  • 3,565
  • 7
  • 38
  • 90

1 Answers1

1

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?

Community
  • 1
  • 1
Nikolay
  • 7,487
  • 2
  • 18
  • 38
  • After changing the custom action CreateScheduleTaskForRunningWatchdog to immediate, that exception is gone. But now I am getting the default value for session["UserName"] inside the custom action CreateScheduleTaskForRunningWatchdog. Do you see any other issues here? – AnOldSoul May 15 '17 at 04:05
  • You probably need to set proper sequence.. first set the property, then get it. Make use of "After" attribute in the ExecuteSequence... Means, CreateScheduleTaskForRunningWatchdog should go after SetUserName – Nikolay May 15 '17 at 04:22
  • NOT Installed – AnOldSoul May 15 '17 at 04:24
  • Above is how I'm having it. Still its not having the passed in value. I can see the passed value in the logs. After the "Doing action: SetUserName" log, the value I see is the default value :( – AnOldSoul May 15 '17 at 04:25
  • Wait a sec - isn't SetUserName assigning property to itself (isn't it a do-nothing action)? – Nikolay May 15 '17 at 04:30
  • That property won't be accessible inside the other custom action? – AnOldSoul May 15 '17 at 04:32
  • I was following the answer of this question btw http://stackoverflow.com/questions/27101579/how-to-pass-parameters-to-the-custom-action – AnOldSoul May 15 '17 at 04:33
  • All properties should be accessible in all immediate custom actions.. you don't need to assign them to self specifically. – Nikolay May 15 '17 at 04:34
  • still getting the same thing even after removing the SetUserName custom action :( – AnOldSoul May 15 '17 at 05:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144216/discussion-between-nikolay-and-mayooran). – Nikolay May 15 '17 at 05:15