4

我正在尝试让 IronPDF 处理我的 ASP.NET Core 3.1 应用服务部署。我没有为此使用 Azure Functions,只是 Azure App Service 上的常规端点——当用户调用它时,该服务会生成并返回生成的 PDF 文档。

在 localhost 上运行端点时,它可以完美地工作——从传递给方法的 HTML 生成报告。但是,一旦我将它部署到我的 Azure Web 应用服务,我就会收到一个502 - Bad Gateway错误,如附件所示(为简洁起见,在 Swagger 中显示)。

在此处输入图像描述

控制器:

[HttpPost]
[Route("[action]")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> AgencyDownload([FromBody] AgencyReportSubmissionDto filters)
{
  var user = await _userService.GetUserByIdAsync(HttpContext.User.GetUserId());

  // Generate the PDF
  var content = await _agencyReport.Generate(user, null, filters.FilterDate, filters.Content, filters.Type);

  // Return the PDF to the browser
  return new FileContentResult(content.BinaryData, "application/pdf") { FileDownloadName = "report.pdf" };
}

服务:

public async Task<PdfDocument> Generate(User user, byte[] letterhead, DateTimeOffset filterDate, string html, AgencyReportTypes reportType)
{
   var corporateIdentity = new CorporateIdentity()
   {
        PrimaryColor = "#000000",
        PrimaryTextColor = "#ffffff",
        SecondaryColor = "#ffffff"
   };

    // Inserts the HTML content (from form) into the HTML template
    var htmlContent = Template(corporateIdentity.PrimaryColor, corporateIdentity.PrimaryTextColor).Replace("{{HtmlContent}}", html);
        
    TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("South Africa Standard Time");
    var convertedDate = TimeZoneInfo.ConvertTimeFromUtc(filterDate.UtcDateTime, tz);
    var Renderer = new ChromePdfRenderer();

    Renderer.RenderingOptions.Title = "Agency Report - for " + convertedDate.ToString("d MMMM yyyy");
    Renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;

    var doc = await Renderer.RenderHtmlAsPdfAsync(htmlContent);
    return doc;
}

解决方案:

我注意到,如果我对该应用程序服务执行手动部署,它就可以工作,但是当我从管道进行部署时,我遇到了上述错误。

所以我去窥探我的管道,然后把它改成这个,它起作用了。

            - task: AzureRmWebAppDeployment@4
              displayName: Deploy API Artifact
              inputs:
                ConnectionType: 'AzureRM'
                AzureSubscription: 'My-Azure-Subscription'
                enableCustomDeployment: true
                DeploymentType: 'zipDeploy'
                deployToSlotOrASE: true
                SlotName: 'development'
                AppType: 'webApp'
                WebAppName: 'my-api'
                Package: '$(Pipeline.Workspace)/**/API.zip'
                ResourceGroupName: 'MyResource'

'DeploymentType: 'zipDeploy'”是关键。

感谢 Alex Hanneman 为我指明了正确的方向。

4

2 回答 2

2

我还使用 Azure App Service 作为 API,包装 IronPDF。升级到最新的 Iron PDF 包也破坏了我的应用程序,返回 502。

我所做的修复它是使用 ZipDeploy 部署代码,然后将 WEBSITE_RUN_FROM_PACKAGE 设置为 0。这也是使 Iron PDF 日志文件显示在 Azure 中所必需的,正如他们在此处推荐的那样:https ://iron.helpscoutdocs.com /article/122-azure-log-files

于 2021-12-21T13:58:14.600 回答
1

应用服务在沙盒中运行您的应用,大多数 PDF 库将失败。查看IronPDF 文档,他们说您可以在 VM 或容器中运行它。由于您已经在使用应用服务,只需将应用打包到容器中,将其发布到容器注册表并配置应用服务以运行它。

于 2021-12-14T02:19:35.603 回答