简体   繁体   English

C#-命令从命令提示符运行,但不是从服务运行

[英]C# - Command runs from command prompt but not from service

I have a C# (ASP Core) service running on Windows Server 2012 R2 that executes Newman test suites via the command line. 我在Windows Server 2012 R2上运行了C#(ASP核心)服务,该服务通过命令行执行Newman测试套件。 The command the service executes works perfectly when run directly in the command prompt, but is not working from the service. 当服务直接在命令提示符下运行时,该服务执行的命令可以完美运行,但不能从该服务运行。 To add insult to injury, the very same command DOES work from the service running locally on my dev machine (Windows 10 Pro). 为了加重侮辱,在我的开发机器(Windows 10 Pro)上本地运行的服务中也执行相同的命令。 I am certain I am running the same command in all instances, as the service outputs the CLI's StandardOutput into a file, the contents of which I paste straight into the command prompt. 我确定我在所有实例中都运行相同的命令,因为该服务将CLI的StandardOutput输出到一个文件中,并将其内容直接粘贴到命令提示符中。

EDIT : The service is hosted on IIS. 编辑 :该服务托管在IIS上。

The error I receive: 我收到的错误:

module.js:471
    throw err;
    ^

Error: Cannot find module 'C:\Users\MyName\AppData\Roaming\npm\node_modules\newman\bin\newman.js'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)
    at startup (bootstrap_node.js:150:9)
    at bootstrap_node.js:508:3

The command I'm running (all paths are the same in every instance): 我正在运行的命令(每个实例中的所有路径都相同):

"C:\\Program Files\\NodeJS\\node.exe" C:\\Users\\MyName\\AppData\\Roaming\\npm\\node_modules\\newman\\bin\\newman.js run https://api.getpostman.com/collections/MyCollectionURI -r cli,junit --reporter-junit-export D:\\TestHarnessServiceLogs\\XML\\{FilenameFromDatetime}.xml -e https://api.getpostman.com/environments/MyEnvironmentURI --disable-unicode “ C:\\ Program Files \\ NodeJS \\ node.exe” C:\\ Users \\ MyName \\ AppData \\ Roaming \\ npm \\ node_modules \\ newman \\ bin \\ newman.js运行https://api.getpostman.com/collections/MyCollectionURI- r cli,junit --reporter-junit-export D:\\ TestHarnessServiceLogs \\ XML \\ {FilenameFromDatetime} .xml -e https://api.getpostman.com/environments/MyEnvironmentURI --disable-unicode

C# to build and run command: C#构建并运行命令:

//Build over multiple lines to make it vaguely readable, then string replace away the newlines so it runs as one command
string runTestCmd = $@"{_nodeExecutablePath} 
                    {_newmanDotJsFile} run 
                    {collectionPath} 
                    -r cli,junit 
                    --reporter-junit-export {_junitReportPathWithFilename} 
                    -e {environmentPath} 
                    --disable-unicode"
                    .Replace(Environment.NewLine, " ")
                    .Replace("\t", "");

File.WriteAllText(@"D:\TestHarnessServiceLogs\Command.txt", runTestCmd);

//Launch hidden CLI
using (Process p = new Process())
{
    p.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.Arguments = _nodeVarsBatPath;
    p.Start();

    //Execute commands
    using (StreamWriter sw = p.StandardInput)
    {
        if (sw.BaseStream.CanWrite)
        {
            sw.WriteLine(runTestCmd);
        }
    }

    //Parse the various outputs
    output = p.StandardOutput.ReadToEnd();
    error = p.StandardError.ReadToEnd();
    exitCode = p.ExitCode.ToString();    //Always returns 0, think this is because it evaluates success of creating the process, not what happens inside it

    File.WriteAllText(@"D:\TestHarnessServiceLogs\output.txt", output);
    File.WriteAllText(@"D:\TestHarnessServiceLogs\error.txt", error);
    File.WriteAllText(@"D:\TestHarnessServiceLogs\exitCode.txt", exitCode);
}

Newman is installed globally in both environments, and some relevant AppSettings below (names modified slightly for brevity): 在这两种环境中,Newman均已全局安装,并且在下面安装了一些相关的AppSettings(为简短起见,略微修改了名称):

"_newmanDotJsFile": "C:\\Users\\MyName\\AppData\\Roaming\\npm\\node_modules\\newman\\bin\\newman.js",
"_nodeVarsBatPath": "\"C:\\Program Files\\NodeJS\\nodevars.bat\"",
"_nodeExecutablePath": "\"C:\\Program Files\\NodeJS\\node.exe\"",

How can an identical command find the newman module and run fine from the CLI but not from the service? 相同的命令如何找到newman模块并从CLI正常运行,但不能从服务正常运行?

EDIT : The user the service is running under couldn't access the file, having done that I now get the following (obviously permissions based) error instead, think I know where this is going...: 编辑 :服务正在运行的用户无法访问文件,这样做后,我现在收到以下(显然是基于权限的)错误,我想我知道这是怎么回事...:

fs.js:994
  binding.lstat(pathModule._makeLong(path), statValues);
          ^

Error: EPERM: operation not permitted, lstat 'C:\Users\MyName'
    at Error (native)
    at Object.fs.lstatSync (fs.js:994:11)
    at Object.realpathSync (fs.js:1676:21)
    at toRealPath (module.js:133:13)
    at Function.Module._findPath (module.js:181:22)
    at Function.Module._resolveFilename (module.js:467:25)
    at Function.Module._load (module.js:417:25)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)
    at startup (bootstrap_node.js:150:9)

EDIT 2 : Created a new user for the service to run under, installed newman for that user and gave it the right permissions (didn't seem clever to give Network Service access to my profile) - all is now working! 编辑2 :为要在其下运行的服务创建了一个新用户,为该用户安装了newman并赋予了它正确的权限(似乎不太聪明,无法向Network Service授予我的个人资料访问权限)-现在一切正常!

It sounds like the service doesn't run as your user? 听起来服务没有像您的用户一样运行? Perhaps that's why it doesn't find the file in that location you specified. 也许这就是为什么它在您指定的位置找不到文件的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM