question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ClosedXML doesn't work within AWS Lambda Serverless App

See original GitHub issue
  • Bug

Version of ClosedXML 0.94.2

What is the current behavior?

ASP .NET Core Web API hosted on AWS Lambda using ClosedXML to create an Excel file fails with an exception:

System.DllNotFoundException: Unable to load shared library ‘libdl’ or one of its dependencies

Exception stack trace:

System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'libdl' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibdl: cannot open shared object file: No such file or directory
   at IntPtr Interop+Libdl.dlopen(string fileName, int flag)
   at IntPtr System.Drawing.SafeNativeMethods+Gdip.LoadNativeLibrary()
   at static System.Drawing.SafeNativeMethods+Gdip()
   --- End of inner exception stack trace ---
   at int System.Drawing.SafeNativeMethods+Gdip.GdipGetGenericFontFamilySansSerif(out IntPtr fontfamily)
   at IntPtr System.Drawing.FontFamily.GetGdipGenericSansSerif()
   at FontFamily System.Drawing.FontFamily.get_GenericSansSerif()
   at void System.Drawing.Font.CreateFont(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte charSet, bool isVertical)
   at Font ClosedXML.Excel.FontBaseExtensions.GetCachedFont(IXLFontBase fontBase, Dictionary<IXLFontBase, Font> fontCache)
   at double ClosedXML.Excel.FontBaseExtensions.GetWidth(IXLFontBase fontBase, string text, Dictionary<IXLFontBase, Font> fontCache)
   at IXLColumn ClosedXML.Excel.XLColumn.AdjustToContents(int startRow, int endRow, double minWidth, double maxWidth)
   at IXLColumns ClosedXML.Excel.XLColumns.AdjustToContents()+(XLColumn c) => { }
   at void System.Collections.Generic.List<T>.ForEach(Action<T> action)
   at IXLColumns ClosedXML.Excel.XLColumns.AdjustToContents()
   at IActionResult Api.Controllers.ValuesController.GetExcelClosedXml() in D:/agent01/w/8c3be5c345025092/src/Api/Controllers/ValuesController.cs:line 92
   at object lambda_method(Closure, object, object[])
   at object Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(object target, object[] parameters)
   at ValueTask<IActionResult> Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
   at async Task Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
   at async Task Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()

What is the expected behavior or new feature?

It should… Just Work™ 😃

Did this work in previous versions of our tool? Which versions? Don’t know / probably not.

Reproducibility

[HttpGet]
public IActionResult GetExcelClosedXml()
{
    using (var workbook = new XLWorkbook())
    {
        var worksheet = workbook.Worksheets.Add("Items");
        worksheet.Cell("A1").Value = "Name";
        worksheet.Cell("B1").Value = "Value (%)";

        worksheet.Cell("A2").Value = "Item 1";
        worksheet.Cell("B2").Value = 0.35;
        worksheet.Cell("B2").Style.NumberFormat.Format = "0.00%";

        worksheet.Columns().AdjustToContents();

        var memoryStream = new MemoryStream();

        workbook.SaveAs(memoryStream);
        memoryStream.Seek(0, SeekOrigin.Begin);

        return File(memoryStream, contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        fileDownloadName: "items.xlsx");
    }
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
augustoproietecommented, Aug 7, 2020

As for the file being corrupted, it seems very strange as the file you attached is not even considered a valid zip archive

Thanks. I’ll investigate further. It could be something else on AWS corrupting the file - i.e. not related to ClosedXML.

@Pankraty: Just to circle back on the corrupted file issue. As suspected, it is not related to ClosedXML. AWS’s API Gateway was corrupting the file by not treating it as binary.

For the benefit of people in the future that may find this issue via search, the fix was to set binary media types in AWS’s API Gateway for the file types I’m using:

  • application/octet-stream
  • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • multipart/form-data
  • */*

image

2reactions
Lonli-Loklicommented, Sep 2, 2019

Issue with Drawing on lambda can be fixed by adding two refs into net core project

  1. https://www.nuget.org/packages/System.Drawing.Common/ >=4.5.0
  2. https://www.nuget.org/packages/runtime.linux-x64.CoreCompat.System.Drawing/
Read more comments on GitHub >

github_iconTop Results From Across the Web

ClosedXML throwing an exception when deployed to aws ...
I have an AWS lambda that reads data from an excel file. I have used ClosedXml v 0.93.0 with .net core 2 ....
Read more >
Best build architecture for Serverless App : r/aws
Hi everybody, I plan to start a private project which consists of an API Gateway with Lambda integration which communicates with DynamoDB, ...
Read more >
Making OpenXML Easy with ClosedXML - InfoQ
But as most of you know, the Office Automation library is not appropriate for servers and working with XML can be quite tedious....
Read more >
Onion Architecture In ASP.NET Core With CQRS - Detailed
In this article, We will talk about Onion Architecture In ASP.NET Core and its advantages. We will also together build a WebApi that...
Read more >
Query English Language Learners
log in help ... 'sentence-transformers', 'sequelize-hooks', 'serf', 'serialapi', 'serial-monitor', 'serverless.com', 'serverless-framework-step-functions', ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found