1

I am following several sources/SO posts and even the Wix installer book and this is how I am currently setting two properties in an immediate custom action then trying to read it in a deferred action. However, its not working (fails and goes to roll back) and I keep getting System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. in my log.

Here is a portion of my .wxs file:

<!-- 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>

My 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;
    }

To confirm my immediate action is happening I ran it with logging on:

Begin SetProperties

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

End SetProperties

MElSawy
  • 17
  • 5

2 Answers2

0

I think I will just point you to some samples for what you need and hope that this resolves your specific problems. Also want to mention this ad-how list of WiX links and resources.

Please find these samples for deferred mode C# custom actions on github:

The DTF sample uses an approach that I have never used in a live package. Just a disclaimer. It seems to work well and uses more advanced constructs than the first sample (which does things "manually").

Similar Answers: Please skim the following two answers for more details on deferred mode custom actions (I don't think there is any point rewriting this):

Custom Actions:


Links:

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
0

I fixed it by modifying this line in SetProperties session["InsertPluginData"] = data.ToString(); to session["CA_InsertPluginData"] = data.ToString(). The key here is for the string inside the session to match the CustomAction Id set in .wxs file!

MElSawy
  • 17
  • 5
  • Sounds good. [Maybe have a look here too](https://stackoverflow.com/a/35412960/129130). Could be good to have some more error checking in there? And in case you haven't gotten debugging properly set up: [MSI Custom Action Debugging](https://stackoverflow.com/a/52880033/129130). Maybe go straight to [the video demonstration of the Visual Studio debugger used with WiX / MSI](https://www.youtube.com/watch?v=ayeBB97_NwA). – Stein Åsmul Feb 24 '23 at 01:40
  • Interactive debugging is the key. And for the record: [be careful with custom actions](https://stackoverflow.com/a/46179779/129130). Only use them if there are no built-in features that do a better job. – Stein Åsmul Feb 24 '23 at 19:26