【问题标题】:ASP.NET OWIN self hosting console window couldn't start dynamically from a normal ASP.NET Web API ControllerASP.NET OWIN 自托管控制台窗口无法从普通的 ASP.NET Web API 控制器动态启动
【发布时间】:2018-03-01 06:30:07
【问题描述】:

我需要根据需求启动我的 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】:

    我可以通过删除“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 的情况给出提示命令的答案一样

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 2015-01-09
      • 2018-03-02
      • 2016-02-21
      • 2013-10-30
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多