3

I have an existing ASPNET Web Application. The front end is HTML/JavaScript and the backend is WebAPI 2. We have been using NRECO.PDFGenerator to drop PDFs to the users, this uses a the QT browser and debugging is becoming a chore. I found PuppeteerSharp and figured I'd give it a try.

I installed the libs using nuget, added an Async method to one of my Web Api Controllers but when I call it the LaunchAsync method just crashed.

    [Route("api/pdf/v2/download")]
    public IHttpActionResult V2Download([FromBody]  Models.PDF.List _pdf) {

        Models.Message.Response _return = new Models.Message.Response();
        _return.Message = "Success!";
        _return.Now = DateTime.Now;
        try
        {
            string[] argu = null; 
            Meh(argu);
        }
        catch (Exception ex)
        {
            _return.Message = ex.Message;
            _return.Severity = 3;
        }
        return Ok(_return);
    }


    public static async Task Meh(string[] args)
    {
        var options = new LaunchOptions
        {
            Headless = true,
            Args = new[] {"--no-sandbox" }
        };

        Debug.WriteLine("Downloading chromium");
        await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);

        Debug.WriteLine("Navigating google");
        using (var browser = await Puppeteer.LaunchAsync(options))
        using (var page = await browser.NewPageAsync())
        {

            Debug.WriteLine("Navigating to page");
            await page.GoToAsync("http://www.google.com");

            Debug.WriteLine("Generating PDF");
            await page.PdfAsync(Path.Combine(Directory.GetCurrentDirectory(), "google.pdf"));

            Debug.WriteLine("Export completed");

            if (!args.Any(arg => arg == "auto-exit"))
            {
                Console.ReadLine();
            }
        }
    }

I'm not sure where to go from here. This is the console output. There's more...

Navigating google The thread 0x3418 has exited with code 0 (0x0). Exception thrown: 'System.NullReferenceException' in System.Web.dll Exception thrown: 'System.NullReferenceException' in mscorlib.dll

The project is using framework 4.7.2

Solution

Everything that touches the Async method needs to also be Async. Also, Passing null args will cause problems as well.

Here's a code sample.

        //[Authorize]
    [Route("api/pdf/v2/download")]
    public async Task<IHttpActionResult> V2Download([FromBody]  Models.PDF.List _pdf) {

        Models.Message.Response _return = new Models.Message.Response();
        _return.Message = "Success!";
        _return.Now = DateTime.Now;
        try
        {
            string[] argu = { }; 
           await Meh(argu);
        }
        catch (Exception ex)
        {
            _return.Message = ex.Message;
            _return.Severity = 3;
        }
        return Ok(_return);
    }


    public static async Task Meh(string[] args)
    {
        var options = new LaunchOptions
        {
            Headless = true
        };

        Debug.WriteLine("Downloading chromium");
        await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);

        Debug.WriteLine("Navigating google");
        using (var browser = await Puppeteer.LaunchAsync(options))
        using (var page = await browser.NewPageAsync())
        {

            Debug.WriteLine("Navigating to page");
            await page.GoToAsync("http://www.google.com");

            Debug.WriteLine("Generating PDF");

            await page.PdfAsync(Path.Combine(Directory.GetCurrentDirectory(), "google.pdf"));

            Debug.WriteLine(Path.Combine(Directory.GetCurrentDirectory(), "google.pdf"));
            Debug.WriteLine("Export completed");

            if (!args.Any(arg => arg == "auto-exit"))
            {
                Console.ReadLine();
            }
        }
    }
Community
  • 1
  • 1
Jeremy Bond
  • 159
  • 1
  • 15

0 Answers0