简体   繁体   English

从控制台应用程序和ASP.NET应用程序启动过程之间的区别

[英]Difference between starting process from Console applciation and ASP.NET application

I have a Web API application that needs to run a Python script which in turn runs a Perl script:) does some otehr stuff and get the output results from it. 我有一个Web API应用程序,该应用程序需要运行Python脚本,而Python脚本又运行Perl脚本:)做一些其他工作,并从中获取输出结果。 The way I do this is with starting a Process: 我这样做的方法是开始一个流程:

        var start = new ProcessStartInfo()
        {
            FileName = _pythonPath,                                 //@"C:\Python27\python.exe",
            Arguments = arguments,                                  //@"D:\apps\scripts\Process.py
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true
        };

        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                var result = reader.ReadToEnd();
                var err = process.StandardError.ReadToEnd();                
                process.WaitForExit();

                return result;
            }
        }

The script inside tries to connect to Perforce server using P4 Python API and then Perl script call a P4 command as well. 里面的脚本尝试使用P4 Python API连接到Perforce服务器,然后Perl脚本也调用P4命令。 When running this code from Console application, everything goes fine. 从控制台应用程序运行此代码时,一切正常。 The program automatically gets the Perforce settings (I've got a P4V client with all the settings specified). 该程序会自动获取Perforce设置(我有一个P4V客户端,其中包含所有指定的设置)。 But when running from ASP.NET Web API, it doesn't get the settigns and says that it cannot conenct to perforce:1666 server (I guess this is the standard value when no settign specified). 但是,当从ASP.NET Web API运行时,它没有获得settigns,并说它不能与perforce:1666服务器连接(我猜这是未指定settign时的标准值)。

I do understand that not so many people use Perforce, especially in such way and can help here, but would like to know what is the difference between running this script from Console app and Web API app that mich cause this different behaviour. 我确实了解到,没有多少人使用Perforce,尤其是以这种方式使用Perforce,并且可以在此处提供帮助,但是我想知道从控制台应用程序和Web API应用程序运行此脚本可能会导致这种不同行为的区别是什么。

One of the most obvious differences between running code from a console application and running code in IIS * is that, usually, the two pieces of code will be running under different user accounts. 从控制台应用程序运行的代码与IIS *中的运行代码之间最明显的区别之一是,通常,这两段代码将在不同的用户帐户下运行。

Frequently, if you're experiencing issues where code works in one of those situations and not the other, it's a permissions or a per-user-settings issue. 通常,如果您遇到的问题是代码在其中一种情况下而不在其他情况下可以工作,则这是权限问题或按用户设置的问题。 You can verify whether this is the case by either running the console application under the same user account that is configured for the appropriate IIS application pool, or vice verse, configure the application pool to use your own user account, and then see whether the problem persists. 您可以通过在为适当的IIS应用程序池配置的同一用户帐户下运行控制台应用程序来验证是否是这种情况,反之亦然,将应用程序池配置为使用您自己的用户帐户,然后查看是否存在问题仍然存在。

If you've confirmed that it's a permissions issue and/or per-user-settings, then you need to decide how you want to fix it. 如果您确认这是权限问题和/或每个用户的设置,则需要确定修复方法。 I'd usually recommend not running IIS application pools under your own user account - if you cannot seem to get the correct settings configured for the existing application pool user, I'd usually recommend creating a separate user account (either locally on the machine or as part of your domain, depending on what's required) and giving it just the permissions/settings that it requires to do the job. 我通常建议不要以您自己的用户帐户运行IIS应用程序池-如果您似乎无法获得为现有应用程序池用户配置的正确设置,则通常建议创建一个单独的用户帐户(在计算机本地或作为您网域的一部分,具体取决于所需内容),并仅向其提供完成该工作所需的权限/设置。


* IIS Express being the exception here because it doesn't do application pools and the code does end up running under your own user account. * IIS Express是这里的例外,因为它不执行应用程序池,并且代码确实以您自己的用户帐户运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM