简体   繁体   English

进程启动IIS服务器

[英]Process Start IIS Server

I can not call Process.Start("DocToPDF.exe", "file.docx file.pdf") successfully from IIS. 我无法从IIS成功调用Process.Start("DocToPDF.exe", "file.docx file.pdf")

  • I have tried allowing desktop interaction from IIS Admin Service; 我尝试过允许IIS Admin Service进行桌面交互;
  • I have tried granting full control permissions to DefaultAppPool for the directory containing the DocToPDF.exe 我已经尝试向DefaultAppPool授予包含DocToPDF.exe的目录的完全控制权限

None of this worked. 这些都没有奏效。 The DocToPDF.exe exits with code: 1 (I write the exit code to a file...). DocToPDF.exe以代码退出:1(我将退出代码写入文件...)。 When I run the website in debug mode (F5) the program exits with code: 0 and everything is ok. 当我以调试模式(F5)运行网站时,程序以代码退出:0,一切正常。 It works perfectly well in debug mode. 它在调试模式下运行良好。 My guess is it has something to do with IIS permissions or something like that because as I said it works well when launching the application from visual studio. 我的猜测是它与IIS权限或类似的东西有关,因为我说它在从visual studio启动应用程序时效果很好。

Here is the method that I am using: 这是我正在使用的方法:

public byte[] DocToPdf(MemoryStream ms)
{            
    string fileName = Guid.NewGuid().ToString();
    string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\" + fileName + ".docx";
    string newPath = path.Replace("docx", "pdf");
    FileStream fs = new FileStream(path.ToString(), FileMode.OpenOrCreate);
    fs.Write(ms.ToArray(), 0, ms.ToArray().Length);
    fs.Close();

    Process p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.FileName = @"C:\Utils\OfficeToPDF.exe";
    p.StartInfo.Arguments = path + " " + newPath;
    p.Start();
    p.WaitForExit();
    System.IO.File.WriteAllText(@"C:\Utils\exitcode.txt", p.ExitCode.ToString());

    var bytes = System.IO.File.ReadAllBytes(newPath);

    System.IO.File.Delete(path);
    System.IO.File.Delete(newPath);

    return bytes;            
}

It is almost always a horrible idea to call any process from an IIS web site/application. 从IIS网站/应用程序调用任何进程几乎总是一个可怕的想法。

  1. IIS and its worker processes run in session 0. The session isolation can lead to issues if the extra executable requires to be run in a user session. IIS及其工作进程在会话0中运行。如果额外的可执行文件需要在用户会话中运行,则会话隔离可能会导致问题。 (It is very likely that you hit this, as if the executable attempts to use printing drivers it will hit problems under session 0.) (很有可能你点击这个,就好像可执行文件试图使用打印驱动程序一样,它会遇到会话0下的问题。)
  2. IIS worker processes are running under application pool identities (without user profile loading if you don't change pool settings). IIS工作进程正在应用程序池标识下运行(如果不更改池设置,则不加载用户配置文件)。 That kind of identities can also lead to failures as the executable might require a normal user account. 由于可执行文件可能需要普通用户帐户,因此这种身份也会导致失败。

There are far too many executable fail to run under IIS, so yours is not something new or uncommon. 有太多的可执行文件无法在IIS下运行,所以你的不是新的或不常见的东西。

Meanwhile, when you hit F5 in Visual Studio, your app is running under ASP.NET Development Server, or IIS Express by default, and that's in your user session . 同时,当您在Visual Studio中点击F5时,您的应用程序默认运行在ASP.NET Development Server或IIS Express下,并且在您的用户会话中 Anything works there can break when switching to IIS. 切换到IIS时,在那里工作的任何东西都可能中断。 So don't be surprise how little you understand about IIS/Windows. 因此,您对IIS / Windows的理解程度并不令人惊讶。 Every experts were beginners. 每个专家都是初学者。

Typical workaround is to find an alternative way that either supported or recommended by Microsoft or a third party vendor. 典型的解决方法是找到Microsoft或第三方供应商支持或推荐的替代方法。 Note that free and open source things are usually developed or tested without such session 0 case in mind, and they can lead to more issues than you thought. 请注意,免费和开源的东西通常是在没有会话0的情况下开发或测试的,并且它们可能导致比您想象的更多问题。

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

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