起价ASP.NET进程 - 为什么这个过程马上死吗? [英] Starting a process from ASP.NET - Why does the process immediately die?

查看:141
本文介绍了起价ASP.NET进程 - 为什么这个过程马上死吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在使用过程中被设置为通过命令行参数露脸模式下运行,因此不需要GUI在任何地方出现。没有给予太多了,它执行对特定数据复杂的计算(数据从外部源拉出从过程中,该数据被不断更新),并经由API暴露的结果。我的应用程序通过传递XML格式的查询和接收类似格式的回答与这个API进行通信。

我遇到的问题是,在启动过程中正常工作,但它运行的过程作为系统用户。这是因为我开始这个过程被注册为特定用户的问题,并在有限的试用版模式系统运行时运行,并根据需要,我需要的API不起作用。所以不是我打算为程序下注册的特定用户身份运行。当我添加的登录信息正确的用户然而,这一进程开始,并立即退出......结果
有没有我应该做一些这方面的具体的方式?

这里的code我有一个不正常:

 公共静态布尔ProcessActive(){
    返回Process.GetProcesses()任何(P => p.ProcessName.Equals(过程,StringComparison.InvariantCultureIgnoreCase));
}公共静态无效StopProcess(){
    清单<处理> processExists = Process.GetProcesses()其中(p值=> p.ProcessName.Equals(过程,StringComparison.InvariantCultureIgnoreCase))。了ToList();    的foreach(在processExists进程p){
        p.Kill();
    }
}公共静态布尔StartProcess(布尔stopIfStarted = FALSE){
    布尔retVal的= FALSE;
    使用(为System.Security.SecureString密码=新为System.Security.SecureString()){        //设置运行过程中所需的变量
        的ProcessStartInfo PINFO =新的ProcessStartInfo(@C:\\ Program Files文件\\程序\\ Program.exe文件);
        pInfo.Arguments = @ - ARG1 -arg2
        pInfo.WorkingDirectory = @C:\\ Program Files文件\\计划;
        pInfo.UserName =用户名;
        pInfo.Domain =DOMAIN;
        password.AppendChar('P');
        / *重复密码的其他字符* /
        pInfo.Password =密码;
        pInfo.UseShellExecute = FALSE;        如果(System.IO.File.Exists(pInfo.FileName)){            //检查进程已经运行
            布尔processExists = ProcessActive();
            如果(stopIfStarted&安培;&安培; processExists){
                StopProcess();
                processExists = ProcessActive();
            }            //如果没有,启动它。
            如果(!processExists){
                StatusHelper.Log(进程未运行起动过程......。);
                使用(过程realProcess =的Process.Start(PINFO)){
                    processExists = ProcessActive();
                    System.Threading.Thread.Sleep(1000);
                    processExists = ProcessActive();
                    如果(realProcess.HasExited){
                        //总是能够在这里
                    }
                    retVal的= processExists;
                }
            }
        }
    }
    返回retVal的;
}


解决方案

我的方式开始从ASP .NET 4.0的过程:


1.远程的,实际上启动过程类(它开始ADOBE READER命令行打印PDF)


2.系统托盘应用作为主机的远程对象


3.它的.Net 2.0下和特定用户下运行,为远程对象客户端的web服务


4. ASP .NET 4.0的网站调用WebService的方法

当一个过程从ASP.NET的它挂起启动。我可以看到它在任务管理器,但它确实没有什么,所以这是我发现的唯一的解决方案。你可以在一个控制台应用程序或Windows服务承载远程对象。 WCF将远程访问另一个很好的选择,但我没有尝试,但。

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 Files\Program\Program.exe");
        pInfo.Arguments = @"-arg1 -arg2";
        pInfo.WorkingDirectory = @"C:\Program Files\Program";
        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;
}

解决方案

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.

这篇关于起价ASP.NET进程 - 为什么这个过程马上死吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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