【发布时间】:2020-06-17 09:54:27
【问题描述】:
我面临着一个令人讨厌的情况。我们尝试在我们的应用程序中使用 PuppeteerSharp 来生成背景 PDF,虽然它在开发模式下运行良好,但在生产环境中却无法运行。
该应用是 WebAPI 2.0 站点、.NET4.7.1、Windows 10 机器。我认为这两种环境之间的主要区别是:
- 在 Release 中构建而不是 Debug:在 Debug 或 Release 模式下从控制台应用程序调用我的代码似乎以相同的方式工作
- 在开发中托管在 IIS Express 中,在生产中托管完整的 IIS
我们使用以下代码:
var launchOptions = new LaunchOptions
{
DefaultViewport = new ViewPortOptions
{
Width = 1920,
Height = 1080,
IsLandscape = printOptions.Orientation == PrintOrientation.Landscape
},
ExecutablePath = this._chromiumPath,
Timeout = Timeout,
TransportFactory = AspNetWebSocketTransport.AspNetTransportFactory
};
var browser = await Puppeteer.LaunchAsync(launchOptions)
.ConfigureAwait(false);
var page = await browser.NewPageAsync()
.ConfigureAwait(false);
await page.EmulateMediaTypeAsync(PuppeteerSharp.Media.MediaType.Print)
.ConfigureAwait(false);
await page.GoToAsync(url, Timeout, new[] { WaitUntilNavigation.Networkidle0 })
.ConfigureAwait(false);
await page.WaitForTimeoutAsync(2000)
.ConfigureAwait(false);
var options = new PdfOptions
{
Width = printOptions.Format == PrintFormat.A4 ? "210mm" : "297mm",
Height = printOptions.Format == PrintFormat.A4 ? "297mm" : "420mm",
PrintBackground = true,
Landscape = printOptions.Orientation == PrintOrientation.Landscape,
MarginOptions = new PuppeteerSharp.Media.MarginOptions
{
Top = ".4in",
Bottom = ".4in",
Left = ".4in",
Right = ".4in"
}
};
await page.PdfAsync(outputFile, options)
.ConfigureAwait(false);
return result;
page.GoToAsync 永不返回,并最终超时。
编辑:
- 我在所有异步调用中将 ConfigureAwait 设置为
false - 我尝试使用
AspNetWebSocketTransport.AspNetTransportFactory传输工厂,但似乎也不起作用
【问题讨论】:
-
您可以尝试在每个异步调用中添加 ConfigureAwait 吗?
-
感谢您的及时回复。我会尝试使用
true和false看看,然后我会回复你 -
一定是假的:)
-
我以为我在 GitHub 上的某个问题上读过;
true和false似乎都没有产生影响……托管环境(IIS Express 与 IIS)是否会受到影响? (我不明白怎么做)
标签: puppeteer-sharp