处理退出事件未从Web服务中触发

时间:2010-06-01 20:49:44

标签: c# web-services event-handling

我正在尝试在Web服务中包装第三方命令行应用程序。

如果我在控制台应用程序中运行以下代码:

Process process= new System.Diagnostics.Process();
process.StartInfo.FileName = "some_executable.exe";

// Do not spawn a window for this process
process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;

// Redirect input, output, and error streams
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.EnableRaisingEvents = true;


process.ErrorDataReceived += (sendingProcess, eventArgs) => {
    // Make note of the error message
    if (!String.IsNullOrEmpty(eventArgs.Data))
        if (this.WarningMessageEvent != null)
            this.WarningMessageEvent(this, new MessageEventArgs(eventArgs.Data));
};

process.OutputDataReceived += (sendingProcess, eventArgs) => {
    // Make note of the message
    if (!String.IsNullOrEmpty(eventArgs.Data))
        if (this.DebugMessageEvent != null)
            this.DebugMessageEvent(this, new MessageEventArgs(eventArgs.Data));
};

process.Exited += (object sender, EventArgs e) => {
    // Make note of the exit event
    if (this.DebugMessageEvent != null)
        this.DebugMessageEvent(this, new MessageEventArgs("The command exited"));
};

process.Start();
process.StandardInput.Close();
process.BeginOutputReadLine();
process.BeginErrorReadLine();

process.WaitForExit();

int exitCode = process.ExitCode;
process.Close();
process.Dispose();

if (this.DebugMessageEvent != null)
    this.DebugMessageEvent(this, new MessageEventArgs("The command exited with code: " + exitCode));

所有事件,包括“process.Exited”事件都会按预期触发。但是,当从Web服务方法中调用此代码时,除“process.Exited”事件之外的所有事件都会触发。

执行似乎挂在了一行:

process.WaitForExit();

是否有人能够对我可能遗失的内容有所了解?

2 个答案:

答案 0 :(得分:1)

事实证明问题是由我试图调用的可执行文件引起的。

不幸的是,这个第三方可执行文件是通过一种模拟器运行的UNIX命令的一个端口。可执行文件旨在将消息输出到输出和错误流,如预期的那样。但是,我们的供应商用于将二进制文件移植到Windows的工具包不使用标准输出流。

当我逐步完成Web服务并从命令行手动调用该过程时,我看到模拟器显示错误对话框。从C#的角度来看,除非在对话框中单击[确定]按钮,否则该过程不会完成,因此“退出”事件永远不会触发。

在与我们的供应商讨论可执行文件后,我了解到它在64位Windows中并不完全支持。我在32位环境中安装了Web服务,一切都很好。

答案 1 :(得分:0)

你在那里运行的过程是什么?既然你提到了它的控制台应用程序,那么它还在等待更多的输入?由于将此作为Web服务运行,可执行文件是否在与ASP Web服务相同的权限下运行?可能是Web服务没有释放正在加载的可执行文件,或者指定Web服务永远运行,直到IIS重新启动并且然后 Process的Exit事件可能会得到烧成。

我也注意到,Process的实例化对象周围没有using条款,即

using (Process proc = new Process())
{
}

编辑: 此外,请在此处查看link类似的概念。比较结果后唯一的事情是属性WindowStyle已设置...

ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
相关问题