2009-05-07 5 views
26

Ho un file MSI creato con Wxs 3.0. Il mio MSI fa riferimento a un'azione personalizzata C#, scritta usando il nuovo C# Custom Action project.Come si passano le proprietà msiexec a un'azione personalizzata di WiX C#?

voglio passare un argomento per msiexec viene instradato verso la mia azione personalizzata - per esempio:

msiexec/i MyApp.msi AMBIENTE = TEST #

Nel mio file .wxs, mi riferisco a la mia azione personalizzato come questo:

<Property Id="ENVIRONMENT"/> 
<Binary Id="WixCustomAction.dll" SourceFile="$(var.WixCustomAction.Path)" /> 
<CustomAction Id="WixCustomAction" BinaryKey="WixCustomAction.dll" DllEntry="ConfigureSettings"/> 
<InstallExecuteSequence> 
    <Custom Action="WixCustomAction" After="InstallFiles"></Custom> 
</InstallExecuteSequence> 

mio C# azione personalizzato è impostato in questo modo:

[CustomAction] 
public static ActionResult ConfigureSettings(Session session) 
{ 

} 

Mi aspettavo di poter accedere alla proprietà in questo modo:

string environmentName = session.Property ["ENVIRONMENT"];

ma questo non sembra funzionare.

Come accedere alla proprietà passata a msiexec nella mia azione personalizzata?

risposta

29

Se invece di

<CustomAction Id="SetCustomActionDataValue" 
       Return="check" 
       Property="Itp.Configurator.WixCustomAction" 
       Value="[ENVIRONMENT],G2,[CONFIGFILE],[TARGETDIR]ITP_v$(var.VERSION_MAJOR)" /> 

si scrive questo:

<CustomAction Id="SetCustomActionDataValue" 
       Return="check" 
       Property="Itp.Configurator.WixCustomAction" 
       Value="Environment=[ENVIRONMENT];G=G2;ConfigFile=[CONFIGFILE];TargetDir=[TARGETDIR]ITP_v$(var.VERSION_MAJOR)" /> 

allora si sarà in grado di riferire le variabili in questo modo:

string env=session.CustomActionData["Environment"]; 
+0

Una cosa che non proviene dagli esempi, non dovrebbe contenere spazi attorno al punto e virgola. – epotter

8

L'azione personalizzata deve essere un'azione personalizzata posticipata per poter essere eseguita dopo InstallFiles. Le azioni personalizzate differite non hanno accesso alle proprietà, ma hanno accesso a CustomActionData. Vedi this blog post per una discussione su come ottenere cosa fare al riguardo. (Questo esempio è un'azione personalizzata VBScript, ma sarà possibile recuperare il valore attraverso la sessione. Raccolta CustomActionData.)

+2

Nota il collegamento al post del blog non funziona più. Il nuovo collegamento è http://blogs.claritycon.com/sajojacob/2008/02/29/customactiondata-in-wix-with-deferred-custom-actions/ –

+3

@ SébastienNussbaumer: bene, che puntano ora è morto, anche. .. jlew: dovresti postare il codice attuale qui, in opposizione ai link. – woohoo

14

Solo per completezza; utilizzando il metodo descritto da Jeremy Lew, nel blog sopra permette le seguenti:

Calling:

msiexec /i ITP.Platform.2.msi ENVIRONMENT=QA CONFIGFILE=EnvironmentConfig.xml 

Con questo nel file .wxs:

<Property Id="ENVIRONMENT" Secure="yes" /> 
<Property Id="CONFIGFILE" Secure="yes" /> 
<Binary Id="Itp.Configurator.WixCustomAction.dll" 
     SourceFile="$(var.Itp.Configurator.WixCustomAction.Path)" /> 

<CustomAction Id="SetCustomActionDataValue" 
       Return="check" 
       Property="Itp.Configurator.WixCustomAction" 
       Value="[ENVIRONMENT],G2,[CONFIGFILE],[TARGETDIR]ITP_v$(var.VERSION_MAJOR)" /> 

<CustomAction Id="Itp.Configurator.WixCustomAction" 
       Return="check" 
       Execute="deferred" 
       BinaryKey="Itp.Configurator.WixCustomAction.dll" 
       DllEntry="ConfigureItpBrandSettings" /> 

<InstallExecuteSequence> 
    <Custom Action="SetCustomActionDataValue" After="InstallFiles"></Custom> 
    <Custom Action="Itp.Configurator.WixCustomAction" After="SetCustomActionDataValue"></Custom> 
</InstallExecuteSequence> 

Con un'azione personalizzata:

/// <summary> 
    /// CustomAction keys should be Environment,BrandId,ConfigPath,itpBasePath 
    /// </summary> 
    /// <param name="session"></param> 
    /// <returns></returns> 
    [CustomAction] 
    public static ActionResult ConfigureItpBrandSettings(Session session) 
    { 
     string[] arguments = GetCustomActionDataArguments(session); 

     string environmentName = arguments[0]; 
     string brandId = arguments[1]; 
     string configPath = arguments[2]; 
     string itpBasePath = arguments[3]; 

     //Do stuff 

     return ActionResult.Success; 
    } 

    private static string[] GetCustomActionDataArguments(Session session) 
    { 
     string[] keys = new string[session.CustomActionData.Keys.Count]; 
     session.CustomActionData.Keys.CopyTo(keys,0); 
     return keys[0].Split(','); 
    } 

opere.

Analizzare gli argomenti CustomActionData è piuttosto brutto, ma funziona. Spero che qualcuno conosca un modo più elegante per farlo.

8

Ecco il mio codice di lavoro:

<Binary Id="MyCA" SourceFile="..\bin\ChainerRun.CA.exe" /> 

<CustomAction Id="SetCustomActionDataValue" Return="check" Property="CustomActionData" Value="TARGETDIR=[TARGETDIR];AA=Description;" /> 

<CustomAction Id="ReadAndSet" 
      BinaryKey="MyCA" 
      DllEntry="ReadAndSet" 
      Execute="immediate" 
      HideTarget="no" 
      Return="check" /> 

<InstallExecuteSequence> 
    <Custom Action="SetCustomActionDataValue" Before="InstallFiles" /> 
    <Custom Action="ReadAndSet" After="SetCustomActionDataValue" /> 
</InstallExecuteSequence> 

Nella funzione di C# azione personalizzata:

[CustomAction] 
public static ActionResult ReadAndSet(Session session) 
{ 
    ActionResult retCode = ActionResult.NotExecuted; 

    System.Diagnostics.Debug.Assert(false); 

    session.Log("ReadAndSet() begins ..."); 

    string installLocation = session.CustomActionData["TARGETDIR"]; 
    string hostName = session.CustomActionData["AA"]; 
    ... 
} 
+1

Grazie! Questo sembra essere l'esempio più chiaro qui e ha funzionato come un fascino. –

0

Se stiamo parlando di Wix Sharp (e non semplice Wix con la sua roba XML), l'aggiunta di una proprietà personalizzata è un pezzo di torta. Tutto quello che devi fare è impostare la proprietà UsesProperties per la tua azione gestita.

Ad esempio, se si desidera aggiungere una proprietà personalizzata denominata "MyProp", basta definire l'azione in questo modo:

new ElevatedManagedAction(nameof(CustomActions.MyCustomAction)) 
{ 
    Condition = Condition.Installed, 
    When = When.Before, 
    Step = Step.RemoveFiles, 
    Return = Return.check, 
    Execute = Execute.deferred, 
    UsesProperties = "MYPROP" 
} 

Impostare il valore della proprietà tramite msiexec riga di comando:

msiexec /i my.msi MYPROP=MYVALUE 

E poi sarete in grado di accedervi da l'azione personalizzata:

[CustomAction] 
public static ActionResult MyCustomAction(Session session) 
{ 
    session.Log("MYPROP VALUE: " + session.CustomActionData["MYPROP"]); 
    return ActionResult.Success; 
} 

Quando la proprietà non viene impostata tramite la riga di comando, il valore predefinito sarà una stringa vuota.