process.BasePriority引发了类型'System.InvalidOperationException'的异常

时间:2017-07-21 08:49:03

标签: c# asp.net-web-api iis-7.5

当我尝试从本地Web API启动进程时,它已成功启动,但当我将其托管到IIS 7.5并尝试启动该进程时,我没有得到任何响应。当我尝试调试将进程附加到visual studio并启动调试时我看到了这个错误进程的BaseProperty

process.BasePriority threw an Exception of Type 'System.InvalidOperationException'

我正在启动一个启动cmd.exe的进程,这是代码:

public static void Start(long campaign_id, long contact_id, string startDate, string endDate, string user)
    {
        try
        {
            //WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.WorkingDirectory = @"C:\";
            startInfo.Arguments = "/c sparkclr-submit --master " + ConfigurationManager.AppSettings["SparkMaster"] + " --driver-class-path " + AppDomain.CurrentDomain.BaseDirectory + "Engine\\mysql.jar " + "--exe CmAnalyticsEngine.exe " + AppDomain.CurrentDomain.BaseDirectory + "Engine " + campaign_id + " " + contact_id + " " + startDate + " " + endDate + " " + user;
            startInfo.CreateNoWindow = false;
            startInfo.UseShellExecute = false;
            startInfo.LoadUserProfile = true;
            //startInfo.Verb = "runas";
            process.StartInfo = startInfo;
            process.Start();
            if (!process.HasExited)
            {
                Console.WriteLine("process is running");
            }
            else
            {
                Console.WriteLine("process is stopped");
            }
        }
        catch (Exception e)
        {
            LogWritter.WriteErrorLog(e);
        }
    }

当我在本地运行它时它可以正常工作但在IIS上它的打印信息进程已停止

我是否需要授予 cmd.exe 权限才能从IIS启动?如果是,那该怎么办?

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:1)

此错误表示进程已退出.-或 - 进程尚未启动,因此没有进程ID。

public static void Start(long campaign_id, long contact_id, string startDate, string endDate, string user)
{
    try
    {
        ProcessStartInfo startInfo = new ProcessStartInfo(); 
        startInfo.FileName = "cmd.exe";
        startInfo.WorkingDirectory = @"C:\";
        startInfo.Arguments = "/c sparkclr-submit --master " + ConfigurationManager.AppSettings["SparkMaster"] + " --driver-class-path " + AppDomain.CurrentDomain.BaseDirectory + "Engine\\mysql.jar " + "--exe CmAnalyticsEngine.exe " + AppDomain.CurrentDomain.BaseDirectory + "Engine " + campaign_id + " " + contact_id + " " + startDate + " " + endDate + " " + user;
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.LoadUserProfile = true;
        //startInfo.Verb = "runas";
        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();
        if (!process.HasExited)
        {
            Console.WriteLine("process is running");
        }
        else
        {
            Console.WriteLine("process is stopped");
        }
    }
    catch (Exception e)
    {
        LogWritter.WriteErrorLog(e);
    }
}

希望它有所帮助。