簡體   English   中英

如何在命令行安裝過程中使用作為參數傳遞的值從wix自定義操作更新appsettings.json?

[英]How to update appsettings.json from wix custom actions with the values passed as parameter during command line installation?

我目前正在開發一個使用Wix作為安裝程序的項目。 我的應用程序是使用.net core開發的,並具有appsettings.json作為配置文件。

我想使用在command-line installation過程中作為參數傳遞的值來更新appsettings.json上的值

例如,我通過參數BUFFER.SIZE傳遞值500

msiexec.exe /i c:\PathToMyMsi\MyMsi.msi BUFFER.SIZE="500" /L*vx c:\PathToMyLog.txt

為此,我在Product.wxs定義了propertycustom action ,如下所示

 <Property Id="BUFFER.SIZE" />

 <Binary Id="GetParameters.CA" SourceFile="..\..\Installer\CustomActions\bin\$(var.Configuration)\CustomActions.CA.dll" />
 <CustomAction Id="GetParValues" 
   BinaryKey="GetParameters.CA" 
   DllEntry="ConfigureBufferSize" 
   Execute="deferred" 
   Return="asyncWait" 
   Impersonate="no" />
 <InstallExecuteSequence>
    <Custom Action="GetParValues" After="InstallFiles"><![CDATA[NOT Installed]]></Custom>
</InstallExecuteSequence>

這是我的自定義操作

    [CustomAction]
    public static ActionResult ConfigureBufferSize(Session session)
    {
        try
        {
            session.Log("Begin ConfigureBufferSize");

            string size = "size = "+ session["BUFFER.SIZE"];
            session.Log(size); // I do not see any log like "size = 50"

            session.Log("End ConfigureBufferSize");
            return ActionResult.Success;
        }
        catch (Exception e)
        {
            return ActionResult.Failure;
        }           
    }

但是,由於無法讀取自定義函數中的值,我被困在這里。 日志不包含以下字符串

  "size = 500"

但是,我在日志中看到的值如下。

   MSI (c) (D0:54) [10:47:06:515]: Command Line: BUFFER.SIZE=500 
   CURRENTDIRECTORY=50 CLIENTUILEVEL=0 CLIENTPROCESSID=17360 
   MSI (s) (84:DC) [10:47:19:361]: PROPERTY CHANGE: Adding BUFFER.SIZE property. Its value is '500'.
   Property(C): BUFFER.SIZE = 500

如何在自定義操作中讀取這些值並更新appsettings.json

我嘗試按以下方式使用Component ,但未執行安裝后的操作

  <Component Id="config" Guid="*">
    <File Id="appconfig" Source="$(var.BasePath)\appsettings.json" KeyPath="yes" Vital="yes"/>
    <util:XmlFile
      Id="_pathFormat_" File="$(var.BasePath)\appsettings.json"
      Action="setValue"
      Name="pathFormat" Value="[BUFFER.SIZE]"
      ElementPath="/ApplicationLog/BufferSize"
      Sequence='1' />
  </Component>

困惑!!

更新資料

這就是我能夠在自定義操作中獲取傳遞的值的方式

聲明財產

  <Property Id="BUFFER.SIZE"  Secure="yes"/>

定義二進制

   <Binary Id="CustomActionDLL" SourceFile="..\..\Installer\CustomActions\CustomActions\bin\$(var.Configuration)\CustomActions.CA.dll" />

定義自定義動作

 <CustomAction Id="SetGetParsValues"
              Property="GetParsValues"
              Value="BUFFER.SIZE=[BUFFER.SIZE]"/>
<CustomAction Id="GetParsValues"
              BinaryKey="CustomActionDLL"
              DllEntry="ConfigureBufferSize"
              Execute="deferred"
              Return="check"
              Impersonate="no" /> 

設置安裝順序

  <InstallExecuteSequence>
    <Custom Action="GetParsValues" After="InstallFiles"><![CDATA[NOT Installed]]></Custom>
    <Custom Action="SetGetParsValues" Before="GetParsValues"><![CDATA[NOT Installed]]></Custom>
</InstallExecuteSequence>

現在,我可以在日志中看到傳遞的參數。

但是,當我嘗試傳遞json文件路徑時,它失敗了

     <Property Id="APPLICATION.PATH"  Secure="yes" Value="$(var.BasePath)\appsettings.json;"/>


 <CustomAction Id="SetFilePathID"
              Property="SetFilePath"
              Value="APPLICATION.PATH=[APPLICATION.PATH]"
              Return="check"/>

這失敗了。

您不能在延遲的自定義操作中使用session["BUFFER.SIZE"]

要將屬性從MSI傳遞到延遲的自定義操作中,您需要使用另一個操作來設置值,然后使用稍有不同的機制在您的自定義操作中讀取該值。

在wixtoolset頁面上的自定義操作中,您會看到它在指向此Microsoft文章的“屬性”描述中有一個特別提及,該文章討論如何在延遲的自定義操作中獲取上下文。

關於第二個動作要注意的重要事情是它的屬性值必須與延遲的自定義動作的Id值完全匹配。

<CustomAction Id="SetGetParsValues" Property="GetParsValues" Value="BUFFER.SIZE=[BUFFER.SIZE]" />

<InstallExecuteSequence>
    <Custom Action="SetGetParsValues" Before="GetParsValues"><![CDATA[NOT Installed]]></Custom>
</InstallExecuteSequence>

然后在您的自定義操作中,您可以通過session["BUFFER.SIZE"]更改為session.CustomActionData["BUFFER.SIZE"]來訪問值

了解[#FileId]可能對您很有用,它使用文件的Id值作為組件文件的安裝位置進行評估。 然后,可以通過將SetGetParsValues自定義操作中的值更新為Value="BUFFER.SIZE=[BUFFER.SIZE];JsonFilePath=[#JsonFileId]"來將兩個值傳遞給自定義操作。 我不確定100%地確定[#JsonFileId]是否可以在那里工作,因此您也可以在此之前設置一個屬性值,然后在“自定義”操作的“值”中使用該屬性值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM