.Net Core命令行应用程序 | Process.Start()在某些机器上运行但在其他机器上无法运行

5

背景

我正在编写一个.NET Core命令行应用程序作为我的构建系统的CLI。该构建系统的这一部分涉及从类库生成NuGet包。我使用ProcessStartInfo.csProcess.cs来调用nuget.exe发出打包命令(nuget.exe位置在系统PATH中)。

  • 注意:我不能使用dotnet CLI进行打包,因为类库不是.NET Core项目,请不要说“为什么不只使用dotnet pack。”

技术栈

  • C# .NET Core(我的CLI)
  • C# .NET 4.5(类库)
  • Thoughtworks GoCD(构建服务器)
  • Windows Server 2016(构建服务器操作系统)
  • Windows 10(本地机器操作系统)

问题

我面临的问题是,在我的个人计算机上运行构建系统CLI时,一切都正常;然而,当我的构建服务器运行它时,该进程似乎尝试启动,但随后退出,而不抛出任何异常或返回任何退出代码。两个帐户(我在本地计算机上的帐户和构建服务器下运行的帐户)都是本地管理员帐户。


代码

Cmd.cs(执行进程)

public static class Cmd
{
    public static int Execute(string filename, string arguments)
    {
        var startInfo = new ProcessStartInfo
        {
            CreateNoWindow = true,
            FileName = filename,
            Arguments = arguments,
        };

        using (var process = new Process { StartInfo = startInfo })
        {
            try
            {
                process.Start();
                process.WaitForExit(30000);
                return process.ExitCode;
            }

            catch (Exception exception)
            {
                if (!process.HasExited)
                {
                    process.Kill();
                }

                Console.WriteLine($"Cmd could not execute command {filename} {arguments}:\n{exception.Message}");
                return (int)ExitCode.Exception;
            }
        }
    }
}

Package.cs(使用Cmd.cs)

// Some code before this

// The below three variables are generated/provided and are made up here for show
var version = "1.0.0";
var package = $"MyLib.{version}";
var projectPath = "C:\Some\Made\Up\Path";
///////////

// Real code
var tempPath = $"{Path.Combine(Path.GetTempPath(), "NugetPackages")}";
var packagePath = Path.Combine(tempPath, package);

if (!Directory.Exists(tempPath))
{
    try
    {
        Directory.CreateDirectory(tempPath);
    }

    catch (Exception exception)
    {
        Console.WriteLine($"Could not create directory in user temp file: {exception.Message}");
        return (int) ExitCode.FilePermission;
    }
}

var filename = "nuget.exe";
var arguments = $"pack -Version {version} -OutputDirectory {tempPath} -properties Configuration=Release {projectPath}";
var exitCode = Cmd.Execute(filename, arguments);

if (exitCode != 0)
{
    Console.WriteLine($"Process failed to execute and exited with exit code: {exitCode}");
    return exitCode;
}

Console.WriteLine($"{package} built successfully");

// Some code after this

1
你需要获取nuget.exe的输出以检查错误,因为你隐藏了进程窗口,所以应该重定向StandardOutput并读取结果。此外,可能进程需要超过30秒的时间,而你没有等待它,为了调试,请移除WaitForExit的超时限制。 - Gusman
好的,我看了一下,似乎同一个版本的NuGet.exe在Windows 10 Pro和Windows Server 2016上的行为不同...不确定为什么,在Windows 10 Pro上,我可以使用-Version标记替换.nuspec中的$version$标记,但在Windows Server 2016上,我不能这样做,必须创建一个新的标记(例如$package$),并使用-properties开关将$package$替换为我的版本...很烦人! - Stephen P.
1个回答

1
我将代码修改为显示所有窗口输出,发现相同版本的NuGet.exe在Windows 10 Pro和Windows Server 2016上表现不同...不确定原因,但在Windows 10 Pro上,我可以使用-Version标签替换.nuspec中的$version$标记,但在Windows Server 2016上,我无法这样做,必须使用不同的标记(例如$package$)并使用-properties开关将$package$替换为我的版本...太糟糕了!
因此,不是:
nuget.exe pack -Version 1.0.0 -OutputDirectory C:\Somewhere -properties Configuration=Release MyLib

我必须使用

nuget.exe pack -OutputDirectory C:\Somwhere -properties "Configuration=Release;package=1.0.0" MyLib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接