本文介绍了如何在命令行安装过程中使用作为参数传递的值从wix自定义操作更新appsettings.json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在一个项目中,我正在使用 Wix 作为安装程序。我的应用程序是使用 .net核心 appsettings.json 作为配置文件开发的。

I’m currently working on a project where I am using Wix for the installer. My application is developed using .net core and having appsettings.json as a configuration file.

我想用在命令中作为参数传递的值来更新 appsettings.json 上的值线安装

I would like to update the values on the appsettings.json with the values which passed as a parameter during command-line installation

例如,我通过参数<$传递值 500 c $ c> BUFFER.SIZE

For example, I am passing value 500 through parameter BUFFER.SIZE

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

到为此,我在 Product.wxs 属性自定义操作 $ c>如下

To achieve this, I have defined property and custom action in Product.wxs as follow

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

但是,我被困在这里,因为我无法读取自定义函数中的值。日志不包含以下字符串

But, I am stuck here because I am not able to read the values inside the custom function. the log does not contain below string

  "size = 500"

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

But, I see values in log as follow.

   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

How do I read these values in custom action and update the appsettings.json

我尝试使用 Component 如下,但它不执行安装后的操作

I tried to useComponent as follow but it's not executing post installation

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

困惑!

更新

这是我能够在自定义操作中获取传递的值的方式

This is how I am able to get the passed values in custom actions

声明

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

现在,我能够在日志中看到传递的参数。

Now, I am able to see the passed parameters in the log.

但是,当我尝试传递 json 文件路径时,它失败了

But, when I try to pass json file path, it fails

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

You can't use session["BUFFER.SIZE"] in a deferred custom action.

要将属性从MSI传递到延迟的自定义操作中,您需要使用另一个操作来设置值,然后使用稍有不同的机制在您的自定义操作中读取该值。

To pass a property from the MSI into a deferred custom action you need to use another action to set the value and then read that value in your custom action using a slightly different mechanism.

在wixtoolset页面上,用于,它讨论了如何在延迟的自定义操作中获取上下文。

On the wixtoolset page for custom action you'll see it has a special mention in the Property description pointing to this microsoft article which talks about how getting context in a deferred custom action works.

关于第二个动作要注意的重要一点是它的属性值必须精确tch到延迟的自定义操作的ID值。

An important thing to note about the second action is that its property value must be an exact match to the deferred custom action's Id value.

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

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

然后在您的自定义操作中,您可以通过更改会话来访问值[ BUFFER.SIZE] 成为 session.CustomActionData [ BUFFER.SIZE]

then in your custom action you can access the value by changing your session["BUFFER.SIZE"] to be session.CustomActionData["BUFFER.SIZE"]

了解 [#FileId] 可能对您很有用,它使用文件的ID值作为组件文件的安装位置进行评估。然后,您可以通过将SetGetParsValues自定义操作中的值更新为 Value = BUFFER.SIZE = [BUFFER.SIZE]; JsonFilePath = [#JsonFileId],将两个值传递给您的自定义操作。。我不确定100%确定 [#JsonFileId] 可以在那里工作,因此您也可以在此之前设置属性值,然后在自定义操作的值中使用该属性值。

It might also be useful for you to know about [#FileId] which gets evaluated as the install location of a component's File using the File's Id value. Then you can pass in two values to your custom action by updating the Value in the SetGetParsValues custom action to be Value="BUFFER.SIZE=[BUFFER.SIZE];JsonFilePath=[#JsonFileId]". I'm not 100% sure doing [#JsonFileId] will work there so you can also just set a property value before that and use the property value in the Custom action's Value.

这篇关于如何在命令行安装过程中使用作为参数传递的值从wix自定义操作更新appsettings.json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:38