[英]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")
。
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网站/应用程序调用任何进程几乎总是一个可怕的想法。
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.