gpt4 book ai didi

azure - 在 azure webjob 中使用 PdfSharp 出现内存不足异常

转载 作者:行者123 更新时间:2023-12-03 06:02:34 29 4
gpt4 key购买 nike

我正在使用 Azure 存储队列向 WebJob 发送消息。然后,此 WebJob 创建 PDF 并将其存储在 Blob 容器中。这在我的开发机器上运行良好。接收消息、实例化对象并创建 PDF 并将其存储在 Blob 存储中。当我将 WebJob 部署到 Azure 时,我在收到消息时收到内存不足异常。

内存限制是多少?如何保持低于这些限制?

 public static void HandleNewRegistration(
[QueueInput("pdf")] Models.Registration registration,
[BlobOutput("pdf/{Name}.txt")] TextWriter writer,
[BlobOutput("pdf/{Name}.pdf")] Stream pdfWriter)
{
try
{
// Store received registration in database (using EF)
AppContext db = new AppContext();
db.Registrations.Add(registration);
db.SaveChanges();

// Create PDF document (nothing fancy, just a section with a paragraph)
var pdf = CreatePdf(registration);
var renderer = new MigraDoc.Rendering.PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
renderer.Document = pdf;
renderer.RenderDocument();
renderer.Save(pdfWriter,true);

}
catch (Exception e)
{
writer.WriteLine(e.Message);
writer.WriteLine(e.StackTrace);
}
writer.WriteLine(registration.Name);
}

使用这个,我最终在我的 blob 存储中只得到一个带有堆栈跟踪的文本文件:

Out of memory.
at System.Drawing.Graphics.FromHwndInternal(IntPtr hwnd)
at System.Drawing.Graphics.FromHwnd(IntPtr hwnd)
at PdfSharp.Drawing.XGraphics..ctor(Graphics gfx, XSize size, XGraphicsUnit pageUnit, XPageDirection pageDirection)
at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
at WebJob.Program.HandleNewRegistration(Registration registration, TextWriter writer, Stream pdfWriter) in d:\Source\Workspaces\[...]\WebJob\Program.cs:line 43

最佳答案

显然,您正在使用 MigraDoc 的 GDI+ 版本 - 并且 Azure 服务器上没有 GDI+,并且 Graphics.FromHwnd() 失败。

使用 MigraDoc 的 WPF 版本,一切应该在 Azure 服务器上运行良好。

关于azure - 在 azure webjob 中使用 PdfSharp 出现内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24015551/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com