繁体   English   中英

检查是否使用带有 Wix 的 CustomAction 安装了 .NETCore

[英]Check if .NETCore is installed using CustomAction with Wix

如果未安装 NetCore 3.1(预览版),我想取消安装

我创建了这个 CustomAction:

using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Win32;

namespace WixCustomAction
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult CheckDotNetCore31Installed(Session session)
        {
            session.Log("Begin CheckDotNetCore31Installed");

            RegistryKey lKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedhost");

            var version = (string)lKey.GetValue("Version");

            session["DOTNETCORE31"] = version == "3.1.0-preview3.19553.2" ? "1" : "0";

            return ActionResult.Success;
        }
    }
}

然后在 WXS 文件中:

<<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:netfx="http://schemas.microsoft.com/wix/NetFxExtension">

   <Product ...>

  (...)

    <Property Id="DOTNETCORE31">0</Property>

    <Condition Message="You must first install the .NET Core 3.1 Runtime">
      Installed OR DOTNETCORE31="1"
    </Condition>

    <InstallExecuteSequence>
      <Custom Action="Check.NetCore" Before="LaunchConditions">NOT Installed</Custom>
    </InstallExecuteSequence>

  </Product>

  <Fragment>
    <Binary Id="WixCA.dll" SourceFile="$(var.WixCustomAction.TargetDir)$(var.WixCustomAction.TargetName).CA.dll" />
    <CustomAction Id="Check.NetCore" BinaryKey="WixCA.dll" DllEntry="CheckDotNetCore31Installed" Execute="immediate"  />
  </Fragment>

这就是我遇到问题的地方,因为我总是收到警告信息。 一个主意? 谢谢

调试:您是否将调试器附加到您的自定义操作,以便您可以看到那里发生了什么? 我敢打赌,它没有正确设置您的财产。 The custom action might not be running at all 显示一个消息框来冒烟测试一下? 更多参与(附加 Visual Studio 调试器):

LaunchCondition :在 MSI 数据库中,启动条件由LaunchCondition 表中的记录表示。 该表有两列。 Condition 列包含一个表达式,该表达式必须计算为True才能继续安装

微星

结论So your condition does not evaluate to true properly DOTNETCORE31的实际值是多少? 我敢打赌它是0 请仔细检查。 最简单的方法显然是直接将其设置为1而不是0 - 然后再次编译并测试。 硬编码暂时像这样:

 <Property Id="DOTNETCORE31">1</Property>

链接:以下是有关启动条件和其他主题的一些先前答案:


WiX 自定义操作:您有调用自定义操作的基本标记吗? 使用Orca检查编译的 MSI 以查看BinaryCustomActionInstallExecuteSequenceInstallUISequence表中是否有条目。 一些模型 WiX 标记( 用于样品的掠夺 gihub.com? ):

<Binary Id="CustomActions" SourceFile="C:\Test.CA.dll" />

<...>

<CustomAction Id="CustomAction1" BinaryKey="CustomActions" DllEntry="CustomAction1"/>

<...>

<InstallUISequence>
  <Custom Action="CustomAction1" After="CostFinalize" />
</InstallUISequence>

<...>

<InstallExecuteSequence>
  <Custom Action="CustomAction1" After="CostFinalize" />
</InstallExecuteSequence>

GUI 和静默安装:显然,您也可以从对话框事件中运行自定义操作——比如单击按钮——但这会使它不会在静默模式下运行。 GUI 在静默模式下被跳过,因此您需要在InstallExecuteSequence和 GUI 中运行自定义操作。

也许最好使用带有dotnet --version命令的 cmd 检查 .net 核心版本,以避免 windows 架构依赖性

此案例的自定义操作示例:

    [CustomAction]
    public static ActionResult CheckVersion(Session session)
    {
       var minVersion = new Version(3, 1, 1);
        var command = "/c dotnet --version";// /c is important here
        var output = string.Empty;
        using (var p = new Process())
        {
            p.StartInfo = new ProcessStartInfo()
            {
                FileName = "cmd.exe",
                Arguments = command,
                UseShellExecute = false,                    
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
            };
            p.Start();
            while (!p.StandardOutput.EndOfStream)
            {
                output += $"{p.StandardOutput.ReadLine()}{Environment.NewLine}";
            }
            p.WaitForExit();
            if (p.ExitCode != 0)
            {
                session["DOTNETCORE1"] = "0";
                return ActionResult.Success;
                //throw new Exception($"{p.ExitCode}:{ p.StandardError.ReadToEnd()}");
            }

            //you can implement here your own comparing algorithm
            //mine will not work with -perview string, but in this case you can just 
            //replace all alphabetical symbols with . using regex, for example
            var currentVersion = Version.Parse(output);
            session["DOTNETCORE1"] = (currentVersion < minVersion) ? "0" : "1";
            
            return ActionResult.Success;
         }
      

更新:正如亚当所提到的,我错了那个片段 - 它只适用于 SDK。 这是另一个获得运行时的方法:

    static readonly List<string> runtimes = new List<string>()
    {
        "Microsoft.NETCore.App",//.NET Runtime
        "Microsoft.AspNetCore.App",//ASP.NET Core Runtime
        "Microsoft.WindowsDesktop.App",//.NET Desktop Runtime
    };

    [CustomAction]
    public static ActionResult CheckVersion(Session session)
    {
        var minVersion = new Version(3, 1, 1);
        var command = "/c dotnet --list-runtimes";// /c is important here
        var output = string.Empty;
        using (var p = new Process())
        {
            p.StartInfo = new ProcessStartInfo()
            {
                FileName = "cmd.exe",
                Arguments = command,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
            };
            p.Start();
            while (!p.StandardOutput.EndOfStream)
            {
                output += $"{p.StandardOutput.ReadLine()}{Environment.NewLine}";
            }
            p.WaitForExit();
            if (p.ExitCode != 0)
            {
                session["DOTNETCORE1"] = "0";
                return ActionResult.Success;
                //throw new Exception($"{p.ExitCode}:{ p.StandardError.ReadToEnd()}");
            }
            session["DOTNETCORE1"] = (GetLatestVersionOfRuntime(runtimes[0], output) < minVersion) ? "0" : "1";
            return ActionResult.Success;
        }
    }

    private static Version GetLatestVersionOfRuntime(string runtime, string runtimesList)
    {
        var latestLine = runtimesList.Split(Environment.NewLine).ToList().Where(x => x.Contains(runtime)).OrderBy(x => x).LastOrDefault();
        if (latestLine != null)
        {
            Regex pattern = new Regex(@"\d+(\.\d+)+");
            Match m = pattern.Match(latestLine);
            string versionValue = m.Value;
            if (Version.TryParse(versionValue, out var version))
            {
                return version;
            }
        }
        return null;
    }

Stein Asmul的帮助下,我能够调试我的 CustomAction。 这是有效的代码:

using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Win32;

namespace WixCustomAction
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult CheckDotNetCore31Installed(Session session)
        {
            session.Log("Begin CheckDotNetCore31Installed");

            RegistryKey localMachine64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            RegistryKey lKey = localMachine64.OpenSubKey(@"SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedhost\", false);

            var version = (string)lKey.GetValue("Version");

            session["DOTNETCORE1"] = version == "3.1.0-preview3.19553.2" ? "1" : "0";

            return ActionResult.Success;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM