502 错误:使用 IronPDF 的 Azure 应用服务上的网关错误

Dis*_*rno 9 c# azure azure-web-app-service asp.net-core ironpdf

我正在尝试让 IronPDF 参与我的 ASP.NET Core 3.1 应用服务部署。我没有使用 Azure Functions 来实现这些目的,而只是使用 Azure 应用服务上的常规端点 - 当用户调用它时,该服务会生成并返回生成的 PDF 文档。

在本地主机上运行端点时,它可以完美地工作 - 从传递到方法中的 HTML 生成报告。但是,一旦我将其部署到 Azure Web App Service,我就会收到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" };
}
Run Code Online (Sandbox Code Playgroud)

服务:

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;
}
Run Code Online (Sandbox Code Playgroud)

解决方案:

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

所以我去窥探我的管道,并将其更改为这样,它起作用了。

            - 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'
Run Code Online (Sandbox Code Playgroud)

“ DeploymentType : 'zipDeploy'”是关键。

感谢亚历克斯·汉尼曼为我指明了正确的方向。

小智 6

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

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