【问题标题】:c# IronPdf shuts down .NET 6 Web API under Linux Dockerc# IronPdf 在 Linux Docker 下关闭 .NET 6 Web API
【发布时间】:2022-07-01 02:00:29
【问题描述】:

版本

  • 用于 PDF 导出的单独 .NET 6.0 Web API
  • IronPdf.Linux - 2022.5.5629
  • IronPdf.Native.Chrome.Linux - 2022.5.5618

截至编写最新 IronPdf Linux NuGet 包的日期

问题

当我在 Docker Linux Container 下运行 .NET 6 Web API 时,当我点击 IronPdf this.pdfRenderer.RenderHtmlAsPdfAsync 的第一个方法时,应用程序会自行关闭。没有错误或进入catch 块,只是应用程序停止,我必须再次运行它。

我尝试了什么?代码

我在关注 IronPdf 提供的官方文档:https://ironpdf.com/docs/questions/docker-linux/

这是我的用例以及我如何使用 IronPdf 库:

[HttpGet]
[Route("Reporting/PDF/{reportItemId:int}"]
public async Task<IActionResult> GenerateReport(int reportItemId)
{
    try
    {
        IronPdf.Logging.Logger.EnableDebugging = true;
        IronPdf.Logging.Logger.LogFilePath = "Default.log"; //May be set to a directory name or full file
        IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
        Installation.LinuxAndDockerDependenciesAutoConfig = false;
        IronPdf.Installation.Initialize();

        ReportItemViewModel reportItemViewModel = this.reportingManager.GetReportItemById(reportItemId); // Internal Logic
        List<PdfDocument> pdfDocs = new List<PdfDocument>();

        foreach (int itemsectionId in reportItemViewModel.ReportItemSectionIds)
        {
            PdfDocument pdfDocument = await CreatePDFDocument(itemsectionId);
            
            pdfDocs.Add(pdfDocument);
        }

        PdfDocument mergedPdfs = IronPdf.PdfDocument.Merge(pdfDocs);

        await AddFooterToPdfDocument(mergedPdfs);
        
        // ... getting mergedPdfs.Stream and uploading it

        return Ok();
    }
    catch (Exception ex)
    {
        this.diagnosticService.AddErrorLog(ex: ex, accountId: accountId, employeeId: employeeId);
        return BadRequest(ex.Message);
    }
}

在上面的代码 sn-p 中,我包含了建议的自动依赖安装、禁用 GPU 加速和“提前”初始化,如 IronPdf 库中所述。

要从 IronPdf 库调用的第一个方法在 CreatePDFDocument 方法内 - await this.pdfRenderer.RenderHtmlAsPdfAsync

private async Task<PdfDocument> CreatePDFDocument(int itemsectionId)
{
    try
    {
        this.pdfRenderer.RenderingOptions.CssMediaType = PdfCssMediaType.Print;
        this.pdfRenderer.RenderingOptions.PrintHtmlBackgrounds = true;
        this.pdfRenderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;

        this.pdfRenderer.RenderingOptions.MarginLeft = 6.35;
        this.pdfRenderer.RenderingOptions.MarginRight = 6.35;
        this.pdfRenderer.RenderingOptions.MarginBottom = 12;

        this.pdfRenderer.RenderingOptions.Timeout = 120;
        this.pdfRenderer.RenderingOptions.HtmlHeader = new IronPdf.HtmlHeaderFooter
        {
            HtmlFragment = await this.viewRenderer.ConvertToStringAsync("Reports/_Header", itemsectionId)
        };

        string viewAsString =
           await this.viewRenderer.ConvertToStringAsync("Reports/ReportContainer", itemsectionId);

        PdfDocument pdfDocument = await this.pdfRenderer.RenderHtmlAsPdfAsync(
              viewAsString,
              new Uri(this.BaseUrl));

        return pdfDocument;
    }
    catch (Exception ex)
    {
        this.diagnosticService.AddErrorLog(ex: ex, accountId: account.Id, employeeId: employeeId);
        throw;
    }
}

当我们点击await this.pdfRenderer.RenderHtmlAsPdfAsync 方法时,应用程序会直接关闭。我们不会进入catch 块或在某处抛出错误。我尝试将它传递给非常简单的 HTML,例如 &lt;div&gt;&lt;h1&gt;Hello World, from IronPdf under Docker Linux&lt;/h1&gt;&lt;/div&gt;,然后应用再次关闭。

注意:通过上面给出的代码流程,我已经成功提取 数百个 PDF。该问题仅在尝试构建和运行时出现 带有 Linux Docker 映像的项目。

Docker 配置

右键单击项目并选择Add -> Docker support... -> Target OS -> Linux 创建所需的Dockerfile

我正在修改 Dockerfile 以匹配上面链接中提供的 IronPdf 支持。我正在尝试Debian Linux DockerFiles -> Debian 11 with .NET 6 部分下提供的那个。

# base runtime image (Debian 11 w/ .NET6 runtime)
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 1433
EXPOSE 3306
# install necessary packages
RUN apt update \
    && apt install -y libgdiplus libxkbcommon-x11-0 libc6 libc6-dev libgtk2.0-0 libnss3 libatk-bridge2.0-0 libx11-xcb1 libxcb-dri3-0 libdrm-common libgbm1 libasound2 libxrender1 libfontconfig1 libxshmfence1
# update write permissions
RUN chmod 777 .
# base development image (Debian 11 w/ .NET6 SDK)
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
# restore NuGet packages
COPY ["Example.Export/Example.Export.csproj", "Example.Export/"]
RUN dotnet restore "Example.Export/Example.Export.csproj"
# build project
COPY . .
WORKDIR "/src/Example.Export"
RUN dotnet build "Example.Export.csproj" -c Release -o /app/build
# publish project
FROM build AS publish
RUN dotnet publish "Example.Export.csproj" -c Release -o /app/publish
# run app
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Example.Export.dll"]

我正在启动项目,一切正常运行:

编辑 1:我在调用 await this.pdfRenderer.RenderHtmlAsPdfAsync 方法之后提供 Docker 日志,之后项目停止。 (注意:我包括最后 X 行,因为整个日志非常大并且没有错误)

13:47:16 (139776153876224): Found job 2 in group 1 (of 1 groups)
13:47:16 (139776153876224): Found job 2 in group 1 (of 1 groups)
13:47:16 (139776153876224): Printing from browser to '/tmp/pdfzwqWbJ'
13:47:16 (139776153876224): Printing from browser to '/tmp/pdfzwqWbJ'
13:47:16 (139776153876224): Received browser print callback for path '/tmp/pdfzwqWbJ' (1)
13:47:16 (139776153876224): Received browser print callback for path '/tmp/pdfzwqWbJ' (1)
13:47:16 (139776153876224): Print finished for browser 2(success: 1)
13:47:16 (139776153876224): Print finished for browser 2(success: 1)
13:47:16 (139776153876224): Resolving job for browser 2 with 80743 bytes
13:47:16 (139776153876224): Resolving job for browser 2 with 80743 bytes
13:47:16 (139776153876224): PdfDocumentFactory created document 0x7f201403a860 from 80743 bytes
13:47:16 (139776153876224): PdfDocumentFactory created document 0x7f201403a860 from 80743 bytes
13:47:16 (139776153876224): Resolving job for browser 2 with 1 page document
13:47:16 (139776153876224): Resolving job for browser 2 with 1 page document
13:47:16 (139776153876224): Generating bytes for document 0x7f201403a860
13:47:16 (139776153876224): Generating bytes for document 0x7f201403a860
13:47:16 (139776153876224): Successfully generated 80751 bytes for document 0x7f201403a860
13:47:16 (139776153876224): Successfully generated 80751 bytes for document 0x7f201403a860
13:47:16 (139776153876224): PdfDocumentFactory closed document 0x7f201403a860
13:47:16 (139776153876224): PdfDocumentFactory closed document 0x7f201403a860
13:47:16 (139776153876224): Closing browser 2
13:47:16 (139776153876224): Closing browser 2
13:47:16 (139776153876224): Destroying browser (id:2)
13:47:16 (139776153876224): Destroying browser (id:2)
13:47:16 (139776153876224): Finished job 2
13:47:16 (139776153876224): Finished job 2
13:47:16 (139776153876224): Found job 2 in group 1 (of 1 groups)
13:47:16 (139776153876224): Found job 2 in group 1 (of 1 groups)
13:47:16 (139776153876224): Job group 1 finished
13:47:16 (139776153876224): Job group 1 finished
13:47:16 (139776153876224): Job group 1 has overlays
13:47:16 (139776153876224): Job group 1 has overlays
13:47:16 (139776153876224): Retrieved 80751 bytes for job group 1 page 0 overlay 2
13:47:16 (139776153876224): Retrieved 80751 bytes for job group 1 page 0 overlay 2
13:47:16 (139776153876224): PdfDocumentFactory created document 0x7f201403a860 from 7745 bytes
13:47:16 (139776153876224): PdfDocumentFactory created document 0x7f201403a860 from 7745 bytes
13:47:16 (139776153876224): Applying overlay to page 0
13:47:16 (139776153876224): Applying overlay to page 0
13:47:16 (139776153876224): PdfDocumentFactory created document 0x7f202024d270 from 80751 bytes
13:47:16 (139776153876224): PdfDocumentFactory created document 0x7f202024d270 from 80751 bytes
13:47:16 (139776153876224): PdfDocumentFactory closed document 0x7f202024d270
13:47:16 (139776153876224): PdfDocumentFactory closed document 0x7f202024d270
13:47:16 (139776153876224): Generating bytes for document 0x7f201403a860
13:47:16 (139776153876224): Generating bytes for document 0x7f201403a860
13:47:16 (139776153876224): Successfully generated 88299 bytes for document 0x7f201403a860
13:47:16 (139776153876224): Successfully generated 88299 bytes for document 0x7f201403a860
13:47:16 (139776153876224): PdfDocumentFactory closed document 0x7f201403a860
13:47:16 (139776153876224): PdfDocumentFactory closed document 0x7f201403a860
13:47:16 (139776153876224): Successfully applied overlays
13:47:16 (139776153876224): Successfully applied overlays
13:47:16 (139776153876224): CefMessagePumpStd::Quit()
13:47:16 (139776892073728): PdfDocumentFactory closed document 0x7f2050020740
13:47:16 (139776153876224): CefMessagePumpStd::Quit()
13:47:16 (139776892073728): PdfDocumentFactory closed document 0x7f2050020740
13:47:16 (139776892073728): PdfDocumentFactory created document 0x7f2050020740 from 88299 bytes
13:47:16 (139776892073728): PdfDocumentFactory created document 0x7f2050020740 from 88299 bytes
13:47:16 (139776892073728): Storing updated document 0
13:47:16 (139776892073728): Storing updated document 0

编辑 1 - 临时解决方案

我能够按照@ScottMildenberger 在 cmets 中的建议临时解决该问题。在更改了我部署到 Azure 的方式后,它现在可以用于生产了。

以 Zip Deploy 的形式部署到 Azure 应用服务。棘手的部分是在应用服务的Configuration 部分中,我们必须更改一个应用程序设置。转到Application Settings 并将WEBSITE_RUN_FROM_PACKAGE 更改为0 的值。这为应用服务提供了readwrite 权限。我们这样做是因为IronPdf 在幕后进行文件操作,需要write 访问。
或者,如果您正在使用带有 YML 的 Azure 管道:

- task: AzureTask@4
  inputs:
    ... inputs ...
    appType: 'webApp'
    packageForLinux: 'path.zip'
    AppSettings: '-WEBSITE_RUN_FROM_PACKAGE 0'
    enableCustomDeployment: true
    DeploymentType: 'zipDeploy'
    TakeAppOfflineFlag: false

我仍然无法在 Docker Linux 容器下运行它。如果我修复它,我将使用解决方案再次编辑。

【问题讨论】:

  • 你的容器有日志吗?
  • 这可能类似于我曾经在运行 Windows 的 Azure 应用服务中遇到的 IronPdf 问题。我使用的是 IronPdf 版本 2022.1.4599 .Net Core 3.1。 IronPdf 需要访问文件系统才能写入临时文件。由于我在 Azure Dev Ops 中的部署方式,文件系统是只读的。我能够更改为使文件系统可写的不同部署选项,然后它开始工作。在这个网站上的一篇帖子上发现了一条评论,它把我带到了那里……我不使用 Docker,但是在部署时你有一个可写的文件系统吗?
  • @G.Dimov 我将 Azure Dev Ops 中的部署更改为使用“Zip Deploy”,这不是默认设置。是的,我们有一个在成功使用 IronPdf 的应用服务中运行的 API 项目。这个问题让我困惑了很长时间,并且症状与您的相似,我找不到任何错误消息。一旦我更改了部署选项,它就开始工作了。
  • @G.Dimov 这是引导我找到解决方案的原始帖子,请参阅 Adam 和 darren 在第一个答案中的 cmets。 stackoverflow.com/questions/68434693/…
  • @ScottMildenberger 感谢您提供的信息,我已经编辑了问题。我们目前有一个作为应用服务的工作产品。如果我让它与 Docker 一起工作,我会再次编辑它。

标签: c# docker asp.net-web-api .net-6.0 ironpdf


【解决方案1】:

解决方案似乎是从命令行使用 Docker。在 MacOS 内置调试​​器 docker-compose 启动设置上使用 Visual Studio (17.0.2) 时,我遇到了同样的问题。但是,当从命令行使用 Docker 时,它并没有遇到问题,而且我能够一遍又一遍地反复调用“RenderHtmlAsPdfAsync”,而程序不会停止执行。所以,我认为这与与 docker 容器中附加的调试器的一些交互有关。

我已向 IronPdf 支持发送了一封电子邮件,他们回复了以下内容,希望能有所帮助:

“我们知道所描述的问题,以下几点可能有助于避免(尽管不能解决问题):

  • 删除所有调试断点
  • 在发布模式下运行
  • 在 Visual Studio 之外运行 Docker(通过 Docker 命令行)
  • 更新 Visual Studio"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-31
    • 2023-02-07
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 2022-12-08
    • 2023-01-11
    • 2013-01-21
    相关资源
    最近更新 更多