puppeteer-sharp - IIS 中的 PuppeteerSharp

标签 puppeteer-sharp

我正面临着一些恼人的情况。我们尝试在我们的应用程序中使用 PuppeteerSharp 来生成背景 PDF,虽然它在开发模式下运行良好,但在生产中却无法运行。

该应用程序是一个 WebAPI 2.0 站点、.NET4.7.1、Windows 10 机器。我认为这两种环境之间的主要区别是:

  • 在发布而不是调试中构建:在调试或 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运输工厂,这似乎也不起作用
  • 最佳答案

    如果您要在 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))
    

    关于puppeteer-sharp - IIS 中的 PuppeteerSharp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62426256/

    相关文章:

    c# - Web API Controller 返回任务并不总是等待任务完成(puppeteer-sharp)

    c# - 使用 Puppeteer-sharp 启动 Tor 浏览器

    c# - 使用 PuppeteerSharp 访问 windows.localStorage

    c# - 在 PuppeteerSharp 中取消下载

    c# - 如何从 ElementHandle 中获取文本?

    javascript - puppeteer 师夏普 : setting a fake path when loading content?

    c# - Puppeteer Sharp 奇怪的行为

    c# - 如何在高性能环境下生成网页图片?

    c# - 无法启动 Base!/app/.local-chromium/Linux-706915/chrome-linux/chrome : . .. libX11.so.6