首页 > 解决方案 > 为什么读取由进程启动的 server.js 中的证书会导致进程终止?

问题描述

我有一个进程,它在启动时运行 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 服务器时,它会在永久终止之前终止/启动,如上所述。

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

标签: c#node.jssslserviceprocess

解决方案


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


推荐阅读