我有一个 WEB API 应用程序,在 ASP.NET 框架 4.7 中完成,我想使用命令行调用 NodeJs 中的应用程序,因此,使用 System.Diagnostic.Process。
NodeJs 应用程序已使用
npm install -g mapshaper
在开发环境中一切正常,我什至可以捕获输出,但是当我在生产服务器上运行时,我会在标准输出中看到以下消息:
'mapshaper' is not recognized as an internal or external command,\r\noperable program or batch file.
(mapshaper 是我要运行的 nodejs 应用程序)
请注意,如果我直接从命令行在生产服务器上运行应用程序,它会完美运行。
如果我从 API 运行,而不是“mapshaper”,命令“DIR”我会得到目录列表,因此它可以工作。我也尝试运行'node -v'并且我正确地获得了安装的节点版本:
c:\\windows\\system32\\inetsrv>node -v\r\nv10.16.3
这是我用来调用 cmd.exe 的代码,并插入要运行的命令:
string strOutput = "";
string path = HostingEnvironment.MapPath("~");
string fileName = "cmd.exe ";
string args = "mapshaper -h";
//args = "dir"; //IT WORKS
//args = "node -v"; //IT WORKS and I get the ver. installed as output
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = fileName;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.RedirectStandardError = true;
pProcess.StartInfo.RedirectStandardInput = true;
ConcurrentQueue<string> messages = new ConcurrentQueue<string>();
pProcess.ErrorDataReceived += (object se, DataReceivedEventArgs ar) =>
{
string data = ar.Data;
if (!string.IsNullOrWhiteSpace(data))
messages.Enqueue(data);
};
pProcess.OutputDataReceived += (object se, DataReceivedEventArgs ar) =>
{
string data = ar.Data;
if (!string.IsNullOrWhiteSpace(data))
messages.Enqueue(data);
};
pProcess.Start();
pProcess.StandardInput.WriteLine(args);
pProcess.StandardInput.Flush();
pProcess.StandardInput.Close();
pProcess.BeginErrorReadLine();
pProcess.BeginOutputReadLine();
while (!pProcess.HasExited)
{
string data = null;
if (messages.TryDequeue(out data))
strOutput+=data+"\r\n";
}
pProcess.WaitForExit(2000);
pProcess.Close();
因此,在生产中,只有从上述代码运行时无法识别的命令 mapshaper(如果从命令行手动运行,它就可以工作)。
哪个可能是原因?在服务器上执行 NodeJs 的一些权限?