我有一个WEB应用程序,它是在NodeJSFramework4.7中完成的,我想在ASP.NET中使用命令行调用应用程序,因此,使用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的权限?
发布于 2019-08-29 07:09:47
请确保您已经在生产服务器上运行了命令npm install -g mapshaper,npm install不会在开发服务器上安装全局包。
还要确保与/npm包目录链接的路径,请在命令行中运行此脚本
path=%PATH%;%APPDATA%\npmhttps://stackoverflow.com/questions/57700774
复制相似问题