PuppeteerSharp in IIS

Viewed 1290

I am facing a bit of an annoying situation. We try to use PuppeteerSharp in our application to generate background PDF, and while it works well in dev mode, it doesn't work when in production.

The app is a WebAPI 2.0 site, .NET4.7.1, Windows 10 machine. The main differences I would see beween the two environments are:

  • build in Release instead of Debug: calling my code from a console app either in Debug or Release mode seems to work in the same way
  • Hosting in IIS Express in development and full IIS in Production

We use the following code:

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 never returns, and eventually times out.

Edit:

  • I set ConfigureAwait to false in all async calls
  • I tried using the AspNetWebSocketTransport.AspNetTransportFactory transport factory, which doesn't seem to work either
2 Answers

If you are deploying your .NET Framework app on IIS. You need to also use PuppeteerSharp.AspNetFramwork and set the TransportFactory to the browser:

using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()
{
    Headless = true,
    TransportFactory = AspNetWebSocketTransport.AspNetTransportFactory,
    ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision)
}).ConfigureAwait(false))

Update: the Nuget package is outdated (hard reference to PuppeteerSharp 1.0.0.0), but the source can be found here: https://github.com/hardkoded/puppeteer-sharp/blob/076897d0cf627c947c61a1192fcb20d968d05cbc/lib/PuppeteerSharp.AspNetFramework/AspNetWebSocketTransport.cs

using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()
{
    Headless = true,
    ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision)
})

Helped me fix the issue, AspNetWebSocketTransport presents references issues and does not seem useful anymore

Related