Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

c# - Starting a process from ASP.NET - Why does the process immediately die?

I'm attempting to start a Process from an ASP.NET C# application. My application communicates with this process via the process' own API (this is a third-party program). I only need to run this process under certain circumstances, so I see no need in having it running on the server constantly, hence the need to start the process from my application.

The process I'm using is set to run in a "faceless" mode via command line arguments, and so doesn't need a GUI to appear anywhere. Without giving too much away, it performs complex calculations on specific data (the data is pulled from an external source from within the process, and this data is updated constantly) and exposes the results via an API. My application communicates with this API by passing xml formatted queries and receiving similarly formatted responses.

The problem I am having is that starting the process works fine, but it runs the process as the SYSTEM user. This is a problem because the process I'm starting is registered to a specific user, and runs in a limited "trial version" mode when running as SYSTEM and the API I need does not work as desired. So instead I intend to run as the specific user the program was registered under. When I add the logon information for the correct user however, the process starts and immediately exits...
Is there some specific way I should be doing this?

Here's the code I have that is not working properly:

public static bool ProcessActive() {
    return Process.GetProcesses().Any(p => p.ProcessName.Equals("Process", StringComparison.InvariantCultureIgnoreCase));
}

public static void  StopProcess() {
    List<Process> processExists = Process.GetProcesses().Where(p => p.ProcessName.Equals("Process", StringComparison.InvariantCultureIgnoreCase)).ToList();

    foreach (Process p in processExists) {
        p.Kill();
    }
}

public static bool StartProcess(bool stopIfStarted = false) {
    bool retVal = false;
    using (System.Security.SecureString password = new System.Security.SecureString()) {

        // Set up the variables required to run the process
        ProcessStartInfo pInfo = new ProcessStartInfo(@"C:Program FilesProgramProgram.exe");
        pInfo.Arguments = @"-arg1 -arg2";
        pInfo.WorkingDirectory = @"C:Program FilesProgram";
        pInfo.UserName = "username";
        pInfo.Domain = "DOMAIN";
        password.AppendChar('p');
        /* repeat for other password chars */
        pInfo.Password = password;
        pInfo.UseShellExecute = false;

        if (System.IO.File.Exists(pInfo.FileName)) {

            //Check if the process is already running
            bool processExists = ProcessActive();
            if (stopIfStarted && processExists) {
                StopProcess();
                processExists = ProcessActive();
            }

            //if not, start it.
            if (!processExists) {
                StatusHelper.Log("Process isn't running. Starting process...");
                using (Process realProcess = Process.Start(pInfo)) {
                    processExists = ProcessActive();
                    System.Threading.Thread.Sleep(1000);
                    processExists = ProcessActive();
                    if (realProcess.HasExited) {
                        // Always gets here
                    }
                    retVal = processExists;
                }
            }
        }
    }
    return retVal;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My way to start a process from ASP .Net 4.0:
1. A remoted class which actually starts the process ( it starts Adobe Reader in command line to print a PDF)
2. A systray application as the host for the remote object
3. An webservice which runs under .Net 2.0 and under a specific user, as a client for the remote object
4. The ASP .Net 4.0 website calls the webservice method

When a process is started from ASP .Net it "hangs". I can see it in Task Manager, but it does nothing, so this was the only solution I found. You can host the remote object in a console application or an windows service. WCF would be another good option to remoting, but I didn't try it, yet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...