puppeteer-sharp - IIS 中的 PuppeteerSharp
问题描述
我面临着一个令人讨厌的情况。我们尝试在我们的应用程序中使用 PuppeteerSharp 来生成背景 PDF,虽然它在开发模式下运行良好,但在生产环境中却不起作用。
该应用程序是一个 WebAPI 2.0 站点、.NET4.7.1、Windows 10 机器。我在这两种环境之间看到的主要区别是:
- 构建发布而不是调试:在调试或发布模式下从控制台应用程序调用我的代码似乎以相同的方式工作
- 在开发中托管在 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
永远不会返回,并最终超时。
编辑:
- 我
false
在所有异步调用中将 ConfigureAwait 设置为 - 我尝试使用
AspNetWebSocketTransport.AspNetTransportFactory
运输工厂,它似乎也不起作用
解决方案
using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()
{
Headless = true,
ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision)
})
帮我解决了这个问题,AspNetWebSocketTransport 提出了引用问题,似乎不再有用了
推荐阅读
- tensorflow - 如何用张量流重复张量元素?
- python - 从 UFT(VB 脚本)调用的 Python 脚本未按预期工作
- php - 将 woocommerce 产品类别缩略图替换为相应类别中最新产品的图像
- reactjs - 在 React 中,为什么要使用 %PUBLIC_URL% 而不是相对 url?
- java - 在Java中如何用字符串替换数字和小数
- python - TensorFlow 中的默认 tf.gradients - 全导数还是偏导数?
- typo3 - 编辑器删除页面时页面树崩溃
- firebase - 反应原生 Firebase 分析不保存特定用户属性
- javascript - 使用钩子编写开源组件会导致在编译后使用它时出现无效的钩子调用
- c# - OnCreateView() 在 OnCreate() 完成之前发生导致