IIS中的PuppeteerSharp

问题描述 投票:1回答:1

我面临着一个令人烦恼的情况。我们尝试在我们的应用程序中使用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运输工厂,但似乎也不起作用
puppeteer-sharp
1个回答
0
投票

[如果要在IIS上部署.NET Framework应用程序。您还需要使用PuppeteerSharp.AspNetFramwork并将TransportFactory设置为浏览器:

using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()
{
    Headless = true,
    TransportFactory = AspNetWebSocketTransport.AspNetTransportFactory,
    ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision)
}).ConfigureAwait(false))
© www.soinside.com 2019 - 2024. All rights reserved.