0

当我在调试模式下运行代码时,测试完成,当我在运行测试中运行它时,ps exec 似乎挂起并且永远不会返回,我也将 kill 到位,但它从未完成。

为什么这在调试时完成但不在运行模式下。

这里有什么我想念的傻事吗?

public class RemoteExeInvokerTask :IQaTask
{

  public string TargetHostName { get; set; }
  public string TargetHostUserName { get; set; }
  public string TargetPassword { get; set; }
  public string TargetExecutableWithParams { get; set; }


  public string DoWork()
  {
    string psExecUtility =  Path.GetDirectoryName(Assembly
                                                  .GetExecutingAssembly()
                                                  .Location)
                            + "\\PsTools\\psExec.exe";


    string quotedArgs = string.Format("\\\\{0} -u {1} -p {2} {3} ",
                                      TargetHostName,
                                      TargetHostUserName,
                                      TargetPassword,
                                      TargetExecutableWithParams);


    ProcessStartInfo processInfo = new ProcessStartInfo(psExecUtility, quotedArgs);
    processInfo.RedirectStandardError = true;
    processInfo.RedirectStandardOutput = true;
    processInfo.UseShellExecute = false;
    processInfo.CreateNoWindow = true;
    Process process = Process.Start(processInfo);

    process.WaitForExit();
    string error = process.StandardError.ReadToEnd();
    string output = process.StandardOutput.ReadToEnd();
    int exitCode = process.ExitCode;
    process.Close();

    if (Process.GetProcessById(process.Id) != null)
    {
      process.Kill();
    }




    if (exitCode <= -1)
    {
      string errorMessage = string.Format("Process Exited with ErrorMessge {0} {1} "
                                          + "OutputMessage {2}",
                                          error,
                                          Environment.NewLine,
                                          output);

      throw new MyApplicationException(errorMessage);
    }

      return output;
  }

测试代码。

[Test]
[ExpectedException(typeof(MyApplicationException))]
[Description("Expect a exception message form the Server.")]
public void RemoteInvokeOfJobWrapperFromCommandLine()
{
  RemoteExeInvokerTask task = new RemoteExeInvokerTask {
                                TargetHostName = "Serverd02",
                                TargetHostUserName = @"corp\username",
                                TargetPassword = @"password",
                                TargetExecutableWithParams = @" E:\D01\Home.Framework.JobExecutionEngine\Home.Framework.JobexecutionEngine.exe 99999  1"};

  string value = task.DoWork();
  Assert.IsNotNull(value);
}
4

1 回答 1

0

基本上用变量前面的@替换你有“\”双斜杠的地方,例如

what if you change string 
quotedArgs = string.Format("\\\\{0} -u {1} -p {2} {3} " 

像这样阅读
stringquotedArgs = string.Format(@"{0} -u {1} -p {2} {3} " 或

string quotedArgs = string.Format("{0} -u {1} -p {2}{3} ",
@TargetHostName,
@TargetHostUserName,
@TargetPassword,
@TargetExecutableWithParams4

也替换字符串 psExecUtility = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\PsTools\psExec.exe";

像这样

string psExecUtility =  Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "@PathandExName 

//这应该是这个“\PsTools\psExec.exe”的一个变量;

于 2012-01-04T19:45:11.720 回答