c# - 为什么读取由进程启动的 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 服务器时,它会在永久终止之前终止/启动,如上所述。
有人会是什么原因造成的吗?或建议进一步解决此问题的方法?
解决方案
调查@FrankerZ 的评论,我发现将证书文件从 server.js 所在的目录移动到流程程序集解决我的问题的位置。我假设进程终止的原因是,工作目录在 c# 服务中设置为程序集所在的位置(高于 server.js 的一级)。但是,证书文件存储在下面的级别(与 server.js 一起)。因此,当我读取证书时,它会尝试从它们实际所在的目录中读取它们,从而导致进程终止。
推荐阅读
- android-layout - 当单击 EditText 以绑定消息时,如何调整工具栏和底部导航栏?
- r - 如何使用 r 中的 ggplot() 函数在单个图中标记多个重叠曲线?
- javascript - 添加确认输入类型提交
- java - 如何在Java中使用正则表达式将一个单词替换为字符串中的新单词
- format - sublime text 3 格式化的快捷键是什么
- php - 致命错误:未捕获的错误:调用成员函数 find() PHP simple_html_dom_parser
- python - 如何使用单个输入框实现简单的 tkinter GUI 应用程序?
- r - 如果块有 message = FALSE,则安静地渲染不起作用
- javascript - MongoError:不支持的投影选项 - 使用 $elemMatch 时
- python - 无法正确标注 numpy 数组的字典