How to pass parameters to the custom action?

陌路散爱 提交于 2019-12-27 15:43:09

问题


I'm trying to create a custom action with "Value" attribute, I want to pass parameters to the C# code (the TARGETDIR and the version).

However, I get an error stating that DLLENtry and Value cannot coexist. But the custom action without dllentry is invalid.

This is the code:

 <CustomAction Id="SetMAWPrefferences"
                Value="InstallDir=[TARGETDIR];Version=2.0.0.1"
                Return="check"
                Execute="commit"
                BinaryKey="ImportExportBinary"                    
                />

And for it I get this error:

Error 9 ICE68: Invalid custom action type for action 'SetMAWPrefferences'.

Any ideas how to do it?


回答1:


Note, you're using Value attribute in the wrong way:

...this attribute must be used with the Property attribute to set the property...Source


Based on the Creating WiX Custom Actions in C# and Passing Parameters article you should:

  1. Create properties with desired values:

    <Property Id="InstallDir" Value="someDefaultValue" />
    <Property Id="Version" Value="2.0.0.1" />
    
  2. Create custom action to set the InstallDir property:

    <CustomAction Id="SetDirProp" Property="InstallDir" Value="[TARGETDIR]" />
    
  3. Create custom action:

    <CustomAction Id="SetMAWPrefferences" 
        Return="check" 
        Execute="commit" 
        BinaryKey="ImportExportBinary" 
        DllEntry="YourCustomAction" />
    
  4. Schedule custom actions for execution during installation process:

    <InstallExecuteSequence>
        <Custom Action="SetDirProp" After="CostFinalize" />
        <Custom Action="SetMAWPreferences" ... />
        ...
    </InstallExecuteSequence>
    
  5. Access those properties from your custom action as follows:

    [CustomAction]
    public static ActionResult YourCustomAction(Session session)
    {
        // session["InstallDir"]
        // session["Version"]
    }
    



回答2:


There are two ways to pass parameters to the custom actions, one will work for the immediate execution CA and the other one will work for the deferred custom actions.

Immediate CA (Can't be rolled back):

In order to pass arguments to the immediate CA you can set a property with the required name and access it from your session.

In Wix:

<Property Id="MyProp" Value="MyValue" />

In CA:

[CustomAction]
public static ActionResult NameOfMyCA(Session session)
{
    string myArg = session["MyProp"];
}   

Deferred CA:

In order to pass arguments to the deferred CA you need to use the CustomActionData Property, this property is the only one you can access from a deferred CA.

In the case of WIX, the DTF includes a CustomActionData class which is a key/value dictionary, and you can access it using:

In Wix:

<CustomAction Id="MyCustomAction" .../>

<Property Id="MyCustomAction" Value="Arg1=value1;Arg2=value2;Arg3=value3;Arg4=[MyProperty]" />

In CA:

[CustomAction]
public static ActionResult NameOfMyCA(Session session)
{
    CustomActionData data = session.CustomActionData;

    //Access each argument like this:

    string arg1 = data["Arg1"];
    string arg2 = data["Arg2"];
    string arg3 = data["Arg3"];
}    

Immediate CA + CustomActionData:

If you want to use the CustomActionData for your Immediate CA you can do something like this:

In Wix:

<Property Id="MyCustomAction" Value="Arg1=value1;Arg2=value2;Arg3=value3;Arg4=[MyProperty]" />

In CA:

[CustomAction]
public static ActionResult NameOfMyCA(Session session)
{
    CustomActionData data = new CustomActionData(session["MyCustomAction"]);

    //Access each argument like this:

    string arg1 = data["Arg1"];
    string arg2 = data["Arg2"];
    string arg3 = data["Arg3"];
    string arg4 = session.Format(data["Arg4"]);
}

In the case of Arg4 since it contains the value of a property you will need to access it like this:

string arg4 = session.Format(data["Arg4"]);

Unfortunately this will work in immediate CA only, this means that if you want to use the value of this property in a deferred CA you will need to have two custom actions:

  • CA 1 to set the CustomActionData for the CA executed as immediate. (Remember to name the property with the same name defined for your CustomAction.

  • CA 2 the CA with the specific logic that consumes CustomActionData.

I suggest you to use the CustomActionData for all the cases, this way is easier to convert you CA from Immediate to Deferred and the code is easier to read.

References:

session.Format CustomActionData



来源:https://stackoverflow.com/questions/27101579/how-to-pass-parameters-to-the-custom-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!