gpt4 book ai didi

c# - 为什么读取进程启动的 server.js 中的证书会导致进程终止?

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:25 25 4
gpt4 key购买 nike

我有一个进程,在启动时运行一个 node.js 服务器。尝试将 HTTPS 添加到服务器会导致服务器所连接的进程终止。可能是什么原因造成的?

该进程由用 C# 编写的服务启动。该进程然后由 server.js 附加到。我已经尝试调试服务以查看哪条执行路径导致进程被终止,但我无法追踪到它。从 server.js 中删除读取证书的行会导致进程未终止。但是,我不明白为什么会这样。

读取证书:

var privatekey = fs.readFileSync("xxx.pem", "utf8");
var certificate = fs.readFileSync("xxx.cert", "utf8");
var credentials = { key: privatekey, cert: certificate };

进程的设置:

            var nodePath = GetNodePath();

// Get the path to the web files
var serverPath = ".\\server\\server.js";

// Create the process arguments
ProcessStartInfo info = new ProcessStartInfo();
info.WorkingDirectory = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location );
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.FileName = nodePath;


info.EnvironmentVariables.Add( "NODE_ENV", "production" );
info.Arguments = $"{serverPath} --max-old-space=200";

我有一个用于进程退出的事件处理程序,主要如下所示:

private static void webProcess_Exited( object sender, EventArgs e )
{

// Make sure we haven't restarted more than 10 times
if ( restartCount > 5 )
{
var message = "The xxx web service is being shut down due to the nodejs process expectantly terminating too many times.";

// Log to the event log
ShutdownService();
}
else
{
var message = "The nodejs process was expectantly terminated, restarting the process in 5 seconds.";

// Log to the event log
Thread.Sleep( 5000 );
//Starts the process/node.js server again
Start();
}
}

启动 server.js 读取证书工作正常。删除读取证书和运行服务的行也可以正常工作。但是,出于某种原因,当读取证书并通过服务启动 node.js 服务器时,它会在永久终止之前终止/启动,如上所述。

有人会是什么原因造成的吗?或者建议进一步解决此问题的方法?

最佳答案

调查@FrankerZ 的评论,我发现将证书文件从 server.js 所在的目录移动到进程程序集的位置解决了我的问题。我认为进程终止的原因是工作目录在 c# 服务中设置为程序集所在的位置(在 server.js 之上一级)。但是,证书文件存储在下面的级别(与 server.js 一起)。因此,当我读取证书时,它会尝试从它们实际所在的目录上方读取它们,从而导致进程终止。

关于c# - 为什么读取进程启动的 server.js 中的证书会导致进程终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57719473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com