从 ASP.NET 启动进程 - 为什么进程会立即终止? [英] Starting a process from ASP.NET - Why does the process immediately die?

查看:31
本文介绍了从 ASP.NET 启动进程 - 为什么进程会立即终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 ASP.NET C# 应用程序启动进程.我的应用程序通过进程自己的 API(这是一个第三方程序)与这个进程通信.我只需要在某些情况下运行这个进程,所以我认为没有必要让它一直在服务器上运行,因此需要从我的应用程序启动这个进程.

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.

我使用的进程设置为通过命令行参数以匿名"模式运行,因此不需要在任何地方显示 GUI.在不透露太多的情况下,它对特定数据执行复杂的计算(数据是从流程内部的外部来源提取的,并且这些数据会不断更新)并通过 API 公开结果.我的应用程序通过传递 xml 格式的查询并接收类似格式的响应来与此 API 通信.

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.

我遇到的问题是启动进程工作正常,但它以 SYSTEM 用户身份运行进程.这是一个问题,因为我正在启动的进程已注册到特定用户,并且在以 SYSTEM 身份运行时以有限的试用版"模式运行,而我需要的 API 无法按预期工作.因此,我打算以注册该程序的特定用户身份运行.但是,当我为正确的用户添加登录信息时,该过程开始并立即退出...
有什么具体的方法我应该这样做吗?

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;
}

推荐答案

我从 ASP .Net 4.0 启动进程的方法:
1. 一个真正启动进程的远程类(它在命令行中启动 Adob​​e Reader 来打印 PDF)
2. 系统托盘应用程序作为远程对象的宿主
3.在.Net 2.0和特定用户下运行的webservice,作为远程​​对象的客户端
4.ASP.Net 4.0网站调用webservice方法

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

当一个进程从 ASP .Net 启动时,它挂起".我可以在任务管理器中看到它,但它什么也不做,所以这是我找到的唯一解决方案.您可以在控制台应用程序或 Windows 服务中托管远程对象.WCF 将是远程处理的另一个不错的选择,但我还没有尝试过.

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.

这篇关于从 ASP.NET 启动进程 - 为什么进程会立即终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆