C# pg_dump.exe无法在IIS8.0中运行
我使用C#中的System.Diagnostics.Process调用PostgreSQL备份数据库组件pg_dump.exe。这是我的密码:C# pg_dump.exe无法在IIS8.0中运行,c#,iis-8,postgresql-9.5,pg-dump,pg-restore,C#,Iis 8,Postgresql 9.5,Pg Dump,Pg Restore,我使用C#中的System.Diagnostics.Process调用PostgreSQL备份数据库组件pg_dump.exe。这是我的密码: string argbackup = @"--host localhost --port 5432 --username ""postgres"" --no-password --format custom --blobs --verbose --file dtname dtname"; Process backupProcess = new
string argbackup = @"--host localhost --port 5432 --username ""postgres"" --no-password --format custom --blobs --verbose --file dtname dtname";
Process backupProcess = new Process();
backupProcess.StartInfo.FileName = @"C:/Program Files/PostgreSQL/9.5/bin/pg_dump.exe";
backupProcess.StartInfo.Arguments = argbackup;
backupProcess.EnableRaisingEvents = true;
backupProcess.Exited += (object sender, EventArgs args) =>
{
//Execution after the process ends.
};
backupProcess.Start();
代码可以在C#中成功运行。但是,当我在Visual Studio中发布项目并将其部署到IIS上时,无法调用pg_dum.exe。并成功运行进程结束后运行的代码
我修改了上述代码:
var pinfo = new ProcessStartInfo();
pinfo.Arguments = @"--host localhost --port 5432 --username ""postgres"" --no-password --format custom --blobs --verbose --file dtname dtname";
info.FileName = @"C:/Program Files/PostgreSQL/9.5/bin/pg_dump.exe";
pinfo.UseShellExecute = false;
using (var process = new Process())
{
process.EnableRaisingEvents = true;
process.StartInfo = pinfo;
process.Start();
while (!process.HasExited)
Thread.Sleep(10000);
process.WaitForExit();
if (process.ExitCode != 0)
throw new Exception("error");
process.Close();
}
它也可以在C#中使用,但在IIS中仍然不起作用
我再次修改了代码:
string argpostgre = @"c: && cd C:/Program Files/PostgreSQL/9.5/bin";
string argbackup = @"pg_dump.exe --host localhost --port 5432 --username ""postgres"" --no-password --format custom --blobs --verbose --file dtname dtname";
Process theProcess = new Process();
theProcess.StartInfo.FileName = @"cmd.exe";
theProcess.StartInfo.UseShellExecute = false;
theProcess.StartInfo.RedirectStandardInput = true;
theProcess.StartInfo.RedirectStandardOutput = true;
theProcess.StartInfo.RedirectStandardError = true;
theProcess.StartInfo.CreateNoWindow = false;
theProcess.EnableRaisingEvents = true;
theProcess.Exited += (object sender, EventArgs args) =>
{
//Execution after the process ends.
};
string strOutput = null;
try
{
theProcess.Start();
theProcess.StandardInput.WriteLine(argpostgre + " && " + argbackup + " & exit");
theProcess.WaitForExit();
}
catch (Exception ex)
{
strOutput = ex.Message;
}
它一直在C#运行,不会退出。但在我强制关闭C#程序后,数据库可以成功备份。它会自动停止进程,但当我部署到IIS时,情况与上面相同
我确信这不是IIS中的安装问题,因为我还使用了进程组件来访问WinRAR.exe(C:\Program Files\WinRAR\WinRAR.exe)压缩程序,该程序可以成功运行
我想知道PostgreSQL是否有权限,因此我无法访问pg_dump.exe和pg_restore.exe等其他组件。从IIS应用程序池运行进程与从控制台应用程序运行进程相比,有三个明显的区别:应用程序池标识、它没有在交互会话中运行的事实,以及默认情况下,IIS不加载帐户的用户配置文件(因此可能缺少环境变量)。其中任何一个都可能导致进程行为不同。第一种是通过(暂时!)让应用程序在您自己的帐户下运行来轻松诊断的,第二种是通过更改应用程序池设置来诊断的。检查非交互式会话的问题更难。我已经解决了这个问题。这是一个关于PostgreSQL权限设置的问题。在安装目录中打开pg_hba.conf,并根据需要将文档末尾的md5修改为trust。例如,如果我使用管理员登录IIS在IPv6网络上运行程序,请将IPv6本地连接的md5修改为信任。