ASP.NET OWIN自托管控制台窗口无法从普通的ASP.NET Web API控制器动态启动

问题描述 投票:0回答:1

我需要根据需求启动我的OWIN自托管控制台应用程序。这个需求是在一个单独的ASP.NET Web API中决定的,它通常在调试模式下在VS的IIS express下托管。但即使没有收到任何错误,我也无法在制定ASP.NET Web API的决策中提示命令窗口。下面是我的测试代码,只是调用命令提示符,直接写在我的决策Web API控制器中。

 using (Process p = new Process())
                {
                    // set start info
                    p.StartInfo = new ProcessStartInfo("cmd.exe")
                    {
                        RedirectStandardInput = true,
                        UseShellExecute = false,
                        WorkingDirectory = @"d:\"
                    };
                    // event handlers for output & error
                    p.OutputDataReceived += p_OutputDataReceived;
                    p.ErrorDataReceived += p_ErrorDataReceived;

                    // start process
                    p.Start();
                    // send command to its input
                    p.StandardInput.Write("dir" + p.StandardInput.NewLine);
                    //wait
                    p.WaitForExit();
                }

所有应用程序都在同一台机器上在ASP.NET MVC控制器事件下运行上面的代码我没有得到任何输出,如果在控制台应用程序下运行,相同的代码给我一个预期的命令窗口。

Is there any restriction to prompt command window under an ASP.NET Web API context?
c# asp.net asp.net-mvc owin self-hosting
1个回答
0
投票

我可以通过删除“RedirectStandardInput”来提示命令,如下所示。

try
  {
                    using (Process p = new Process())
                    {
                        // set start info
                        p.StartInfo = new ProcessStartInfo("cmd.exe")
                        {
                            // RedirectStandardInput = true,
                            // UseShellExecute = false,
                            //WorkingDirectory = root + @"\Test"
                        };

                        // start process
                        p.Start();
                        p.WaitForExit();
                        p.Close();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

保持这个线程打开,好像有人为RedirectStandardInput = true的情况提示命令的答案

© www.soinside.com 2019 - 2024. All rights reserved.