如何从C#.NET Core 2.2在Linux中启动和查询ngrok?

问题描述 投票:0回答:1

我们有一个基于红k的控制台驱动的Web应用程序,该应用程序在启动时会自动启动ngrok。我们使用指向ngrok.exe的Process.Start(),然后在同一C#中查询http://127.0.0.1:4040/api/tunnels以获取ngrok URL,以便我们可以同步到外部依赖项。

它一直在Windows中工作。在Linux中(我正在使用MX Linux),当我逐步浏览Rider中的代码时,Process.Start返回true,并且当我们到达查询http://127.0.0.1:4040/api/tunnels的代码时,过程对象仍然说.HasExited == false 。但是,那时候它失败了(拒绝连接),如果我打开另一个终端并且wget -O - http://127.0.0.1:4040/api/tunnel也无法连接,那么我真的很困惑,ngrok是否启动?如果在终端中我手动导航到ngrok的Linux可执行文件所在的位置,并将与传递给Process.Start()相同的参数传递给它,则它确实会启动,它会填满整个屏幕,然后打开另一个终端并尝试wget -O - http://127.0.0.1:4040/api/tunnel它成功连接并返回输出。

我的意思是,如果该进程无法从C#启动,为什么Process.Start()返回true? ..为什么进程对象即使在一些线程休眠后也说.HasExited == false

c# linux .net-core ngrok
1个回答
0
投票

我只是想通了。我直觉这与控制台流被某些ProcessStartInfo详细信息阻止有关,因为在Linux和Windows中使用Shell的工作方式不同。这是工作代码,注意到更改是在开头添加了if块:

 if (Company.RuntimeEnvironment.IsLinux && showWindow == "true")
     { showWindow = "false"; }
 var process = new Process
 {
     StartInfo =
     {
        FileName = FileNgrokExecutable,
        CreateNoWindow = showWindow != "true" && showWindow != "integrated",
        RedirectStandardOutput = showWindow != "true" && showWindow != "integrated",
        RedirectStandardError = showWindow != "true" && showWindow != "integrated",
        UseShellExecute = showWindow == "true",
        Arguments = $"http {localPort} -host-header=localhost"
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.