我编写了一个PDF生成API,它使用dinktopdf将一些模板化的HTML转换为字节数组。这一切都在我的本地计算机上运行良好,但是当我部署到我的 azure Web 应用程序时,API 只能工作一次。当我第二次尝试时,我收到以下消息和 502 错误:
指定的 CGI 应用程序遇到错误,服务器终止了该进程。
这是我的代码的精简版本,仍然显示相同的错误:
static IPdfConverter pdfConverter = new SynchronizedConverter(new PdfTools());
public static byte[] BuildPdf(string html)
{
return pdfConverter.Convert(new HtmlToPdfDocument()
{
Objects =
{
new ObjectSettings
{
HtmlContent = html
}
}
});
}
我还尝试使用 IronPDF 进行 HTML 到 PDF 的转换,并遇到了相同的确切问题(在本地机器上完美运行,但在 Azure 部署上只有一次,然后给出一致的 502 错误(。
使用var converter = new SynchronizedConverter(new PdfTools());´, remove it, and just inject
IConverter 初始化转换器in your service.
I tried and this approach is working, after all, we already register the converter, we don't need to create an instance using the
new' 关键字。我的应用程序不在 Azure 上,但我有同样的问题,转换器在第一次调用后挂起,因此我决定写下这条评论。
更新:通过将 Azure 应用服务计划更改为基本而不是免费解决了问题(PDF 生成显然至少需要基本计划(。