如何使用在命令行安装期间作为参数传递的值从 wix 自定义操作更新 appsettings.json?

How to update appsettings.json from wix custom actions with the values passed as parameter during command line installation?(如何使用在命令行安装期间作为参数传递的值从 wix 自定义操作更新 appsettings.json?)

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

问题描述

我目前正在开发一个项目,我使用 Wix 作为安装程序.我的应用程序是使用 .net core 开发的,并将 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

例如我通过参数BUFFER.SIZE

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

为此,我在 Product.wxs 中定义了 propertycustom action 如下

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

 <Property Id="BUFFER.SIZE" />

 <Binary Id="GetParameters.CA" SourceFile="....InstallerCustomActionsin$(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="....InstallerCustomActionsCustomActionsin$(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.

关于第二个操作需要注意的重要一点是,它的属性值必须与延迟的自定义操作的 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>

然后在您的自定义操作中,您可以通过将 session["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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用在命令行安装期间作为参数传递的值从 wix 自定义操作更新 appsettings.json?

基础教程推荐