1

I can not call Process.Start("DocToPDF.exe", "file.docx file.pdf") successfully from IIS.

  • I have tried allowing desktop interaction from IIS Admin Service;
  • I have tried granting full control permissions to DefaultAppPool for the directory containing the DocToPDF.exe

None of this worked. The DocToPDF.exe exits with code: 1 (I write the exit code to a file...). When I run the website in debug mode (F5) the program exits with code: 0 and everything is ok. 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.

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;            
}
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Alin
  • 342
  • 1
  • 4
  • 12
  • http://stackoverflow.com/questions/8414514/iis7-does-not-start-my-exe-file-by-process-start similar question. – Ahmet Arslan Jan 14 '15 at 10:23
  • Check you app pool identity (http://www.iis.net/learn/manage/configuring-security/application-pool-identities). Does it have local logon permissions? Does it have permissions to the `c:\utils` folder? Does it have permission to the %AppData% folder? – jessehouwing Jan 14 '15 at 10:24
  • agreed with @jessehouwing - it is to do with the profile/user account you are using on the apppool - it needs the right permissions. by default, it is restricted. you may need to create a user account with more permissions than the standard OOB app pool user account (ApplicationIdentity I think). It may not even have permission to execute a process. – Ahmed ilyas Jan 14 '15 at 10:26
  • Also check the bitness of your app pool and that of your executable. – jessehouwing Jan 14 '15 at 10:26
  • [Read the output of the program](http://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output) or make it log the error to a file if it supports that. Most likely the user running IIS doesn't have permissions to write in the given directory. – CodeCaster Jan 14 '15 at 10:26
  • It has permissions to the C:\Utils folder – Alin Jan 14 '15 at 10:38

1 Answers1

2

It is almost always a horrible idea to call any process from an IIS web site/application.

  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. (It is very likely that you hit this, as if the executable attempts to use printing drivers it will hit problems under session 0.)
  2. IIS worker processes are running under application pool identities (without user profile loading if you don't change pool settings). 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.

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. Anything works there can break when switching to IIS. So don't be surprise how little you understand about 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. 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.

Lex Li
  • 60,503
  • 9
  • 116
  • 147