EPPlus icon indicating copy to clipboard operation
EPPlus copied to clipboard

System.TypeInitializationException: 'The type initializer for 'Gdip' threw an exception.'

Open drwharris opened this issue 7 years ago • 28 comments

I am running an application using EPPlus 4.5.0.1-beta under docker with microsoft/aspnetcore:2.0.4. My docker file includes the following to ensure GDI+ is installed:

RUN apt-get update RUN apt-get install -y libgdiplus

In this snippet of code:

var reader GetDataReader();
using (var xlPackage = new ExcelPackage(newFile))
{
    var worksheet = xlPackage.Workbook.Worksheets.Add("Report");
    worksheet.Cells["A1"].LoadFromDataReader(reader, true);
    for (int i = 0; i < reader.FieldCount; i++)
    {
         worksheet.Column(i + 1).AutoFit(10, 80); // <-- Error Here
    }
}

The "AutoFit" method is throwing the following: An unhandled exception has occurred while executing the request System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'libdl': The specified module or one of its dependencies could not be found. (Exception from HRESULT: 0x8007007E) at Interop.Libdl.dlopen(String fileName, Int32 flag) at System.Drawing.SafeNativeMethods.Gdip.LoadNativeLibrary() at System.Drawing.SafeNativeMethods.Gdip..cctor() --- End of inner exception stack trace --- at System.Drawing.SafeNativeMethods.Gdip.GdipGetGenericFontFamilySansSerif(IntPtr& fontfamily) at System.Drawing.FontFamily.GetGdipGenericSansSerif() at System.Drawing.FontFamily.get_GenericSansSerif() at System.Drawing.Font.CreateFont(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte charSet, Boolean isVertical) at OfficeOpenXml.ExcelRangeBase.AutoFitColumns(Double MinimumWidth, Double MaximumWidth) at MyApp.Common.ReportHelper.ExtractDataFromRedshift(GetReportRequest request) in C:\Development\MyApp\Common\ReportHelper.cs:line 93 at MyApp.Controllers.EtlController.GetReport(GetReportRequest request) in C:\Development\MyApp\Controllers\EtlController.cs:line 40 at lambda_method(Closure , Object , Object[] ) at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__14.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResourceFilter>d__22.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext() Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware:Error: An unhandled exception has occurred while executing the request

System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'libdl': The specified module or one of its dependencies could not be found. (Exception from HRESULT: 0x8007007E) at Interop.Libdl.dlopen(String fileName, Int32 flag) at System.Drawing.SafeNativeMethods.Gdip.LoadNativeLibrary() at System.Drawing.SafeNativeMethods.Gdip..cctor() --- End of inner exception stack trace --- at System.Drawing.SafeNativeMethods.Gdip.GdipGetGenericFontFamilySansSerif(IntPtr& fontfamily) at System.Drawing.FontFamily.GetGdipGenericSansSerif() at System.Drawing.FontFamily.get_GenericSansSerif() at System.Drawing.Font.CreateFont(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte charSet, Boolean isVertical) at OfficeOpenXml.ExcelRangeBase.AutoFitColumns(Double MinimumWidth, Double MaximumWidth) at MyApp.Common.ReportHelper.ExtractDataFromRedshift(GetReportRequest request) in C:\Development\MyApp\Common\ReportHelper.cs:line 93 at MyApp.Controllers.EtlController.GetReport(GetReportRequest request) in C:\Development\MyApp\Controllers\EtlController.cs:line 40 at lambda_method(Closure , Object , Object[] ) at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__14.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextResourceFilter>d__22.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeFilterPipelineAsync>d__17.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeAsync>d__15.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()

This was working perfectly yesterday and works fine when I am running it under dotnet under windows.

drwharris avatar Dec 14 '17 03:12 drwharris

Just confirmed...this works fine when using Beta 1 but Beta 2 breaks.

Just noticed this comment against sampleapp.core "Beta 2. Replaced CoreCompat.System.Drawing.v2 with System.Drawing.Common"

drwharris avatar Dec 14 '17 03:12 drwharris

Still a problem with 4.5.1 and preview 3 of System.Drawing.Common?

JanKallman avatar Mar 30 '18 10:03 JanKallman

I have the same problem for our setup at work - .NET Core 2.0.8 with Docker in a clustered environment. Using latest EPPlus 4.5.1.2.

Have commented out AutoFit for now.

LeJerk avatar May 31 '18 14:05 LeJerk

About Unable to load DLL 'libdl' error in containers, they are suggesting:

  • Install libgdiplus and libc6-dev in your container (If you're on Ubuntu or Debian, you can try to install the libc6-dev package to get libdl.so).
  • Check /usr/lib/x86_64-linux-gnu/libdl.so file exists inside your container.
  • If it still fails, you can try setting LD_LIBRARY_PATH to $LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu but that should be included already.
  • If that still fails, you can set the LD_DEBUG environment variable to libs in your Dockerfile, restart your container.
FROM microsoft/aspnetcore
RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get install -y libgdiplus
RUN ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
WORKDIR /app
COPY /app ./
EXPOSE 8082
ENTRYPOINT ["dotnet", "WebApp.dll"]

VahidN avatar May 31 '18 15:05 VahidN

I had this same issue and also installing libc6-dev solved it.

apt-get install -y libc6-dev

jgobl avatar Jun 01 '18 08:06 jgobl

Yes, unfortunately you need to install libc6-dev at the moment to get System.Drawing.Common working properly in .NET Core on Linux.

A fix for this in .NET Core depends on https://github.com/dotnet/corefx/issues/17135 being implemented. If you're hitting this issue and would like to see it solved - it would help if you can upvote that issue in the .NET Core repository.

qmfrederik avatar Jun 05 '18 12:06 qmfrederik

From Docker:

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
RUN ln -s /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libdl.so
RUN apt update
RUN apt install -y libgdiplus
RUN ln -s /usr/lib/libgdiplus.so /lib/x86_64-linux-gnu/libgdiplus.so

mherarsh avatar Jul 12 '18 16:07 mherarsh

Any idea how to resolve this problem for Centos?

mii9000 avatar Mar 12 '19 08:03 mii9000

Still getting: DllNotFoundException: Unable to load DLL 'libdl': The specified module or one of its dependencies could not be found.

EDIT: FIXED BY MOVING / PUTTING apt-get install ... to line 2:

FROM microsoft/aspnetcore:2.0 AS base
RUN apt-get update && apt-get install -y apt-utils libgdiplus libc6-dev
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY ["SampleWebApp.Core/SampleWebApp.Core.csproj", "SampleWebApp.Core/"]
COPY ["EPPlus/EPPlus.MultiTarget.csproj", "EPPlus/"]
RUN dotnet restore "SampleWebApp.Core/SampleWebApp.Core.csproj"
COPY . .
WORKDIR "/src/SampleWebApp.Core"
RUN dotnet build "SampleWebApp.Core.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "SampleWebApp.Core.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "SampleWebApp.Core.dll"]

nagstaku avatar Apr 24 '19 23:04 nagstaku

Do you need to restart after apt-get install -y libc6-dev?

justlearntutors avatar May 02 '19 12:05 justlearntutors

#https://github.com/dotnet/core/issues/2746

@JanKallman

What is the possibility of removing the code relayed upon libgdiplus. In my scenario, I have encountered with an error when I try to add image to the sheet. As far as I can see in the code the code can be replaceable by any image manipulation library like ImageSharp.

*https://github.com/SixLabors/ImageSharp

oguzhantopcu avatar Jun 11 '19 21:06 oguzhantopcu

The problem continues even if I install libdl, the problem only occurs when the AutoFit method is used, if we use the Width property, it works fine.

EliuTimana avatar Sep 26 '19 22:09 EliuTimana

Any idea how to resolve this problem for Centos? In case someone get this problem on Cetnos, For Centos, try

yum update && yum install -y \
		glibc-devel  \
        libgdiplus \
        libX11-devel 

Note that, the package names are little bit defferent with those on Ubuntu. I spent several hours to find out that . Hope it'll be helpful. :)

songzheng45 avatar Oct 23 '19 10:10 songzheng45

Any idea how to resolve this problem for Centos?

Yes, worked this command lines;

yum install glibc-devel glibc-devel.i686

necipsunmaz avatar Oct 31 '19 13:10 necipsunmaz

I'm working with an Azure app service on Linux and I get this error as well. What can I do to fix it ?

Jerome2606 avatar Dec 05 '19 20:12 Jerome2606

Here's what I recommend:

  • Pull in a recent version of System.Drawing.Common. 4.6.1 or 4.7.0 are good choices
  • Target netcoreapp3.0.
  • Install version 6.0.4 or later of libgdiplus via the Mono repositories. (There's no need for you to install Mono itself).

If you do this, you should not need the glibc-devel nor the libc6-dev packages. That's supposed to be a thing of the past.

qmfrederik avatar Dec 05 '19 20:12 qmfrederik

I don't know if I can install a library on an Azure Web App. Is it something that should be part of the azure devOps deployment? I'm quite new with Azure and Linux ^^

Jerome2606 avatar Dec 05 '19 20:12 Jerome2606

Sorry, don't know about App Service.

qmfrederik avatar Dec 05 '19 20:12 qmfrederik

I have the same problem for our setup at work - .NET Core 2.0.8 with Docker in a clustered environment. Using latest EPPlus 4.5.1.2.

Have commented out AutoFit for now.

I'm having the same problem with the version 4.5.3.2 of Epplus , that uses Sytem.Drawing.Commom 4.6.0

felipe-rodrigues avatar Dec 11 '19 16:12 felipe-rodrigues

@felipe-rodrigues Are you on .NET Core 3.0?

qmfrederik avatar Dec 11 '19 17:12 qmfrederik

@felipe-rodrigues Are you on .NET Core 3.0?

No I'm using 2.2

felipe-rodrigues avatar Dec 11 '19 17:12 felipe-rodrigues

This is fixed in .NET Core 3.0 but not 2.2. If you're on 2.2, you'll need to install libc6-dev, glibc6-devel or the equivalent package for your distro which contains libdl.so.

.NET Core 2.2 goes out of support this month, so that's another incentive to upgrade 😄 .

qmfrederik avatar Dec 12 '19 08:12 qmfrederik

This is fixed in .NET Core 3.0 but not 2.2. If you're on 2.2, you'll need to install libc6-dev, glibc6-devel or the equivalent package for your distro which contains libdl.so.

.NET Core 2.2 goes out of support this month, so that's another incentive to upgrade 😄 .

I'll change , but for a while I got doing what you said. Installing the libs:

RUN ln -s /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libdl.so RUN apt update RUN apt install -y libgdiplus RUN ln -s /usr/lib/libgdiplus.so /lib/x86_64-linux-gnu/libgdiplus.so

felipe-rodrigues avatar Dec 12 '19 13:12 felipe-rodrigues

Does anyone know how to fix this if your using the alpine image? (3.1-alphine)

Managed to get libgdiplus installed but its not hooking up correctly i think.

RUN apk add libgdiplus --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted RUN ln -s /usr/lib/libgdiplus.so.0 /usr/lib/gdiplus.dll

jonny64bit avatar Dec 13 '19 16:12 jonny64bit

@jonny64bit What .NET Core version and System.Drawing.Common version are you using? On .NET Core 3.x and 4.6.0, it should pick up libgdiplus.so.0 just fine.

What does ldd /usr/lib/libgdiplus.so.0 get you?

PS: I don't think there is any scenario in which /usr/lib/gdiplus.dll would get picked up by System.Drawing.Common.

qmfrederik avatar Dec 13 '19 16:12 qmfrederik

@qmfrederik using:

  • .net core 3.1
  • System.Drawing.Common 4.6.26919.02
  • EPPlus 4.5.3.2

ldd /usr/lib/libgdiplus.so.0 gives me

/lib/ld-musl-x86_64.so.1 (0x7f59626a7000) libglib-2.0.so.0 => /usr/lib/libglib-2.0.so.0 (0x7f596254b000) libcairo.so.2 => /usr/lib/libcairo.so.2 (0x7f596245e000) libfreetype.so.6 => /usr/lib/libfreetype.so.6 (0x7f59623ac000) libjpeg.so.8 => /usr/lib/libjpeg.so.8 (0x7f5962316000) libtiff.so.5 => /usr/lib/libtiff.so.5 (0x7f59622ac000) libpng16.so.16 => /usr/lib/libpng16.so.16 (0x7f596227c000) libX11.so.6 => /usr/lib/libX11.so.6 (0x7f596215a000) libexif.so.12 => /usr/lib/libexif.so.12 (0x7f5962119000) libfontconfig.so.1 => /usr/lib/libfontconfig.so.1 (0x7f59620dd000) libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7f59626a7000) libpcre.so.1 => /usr/lib/libpcre.so.1 (0x7f5962080000) libintl.so.8 => /usr/lib/libintl.so.8 (0x7f5962070000) libpixman-1.so.0 => /usr/lib/libpixman-1.so.0 (0x7f5961fdf000) libxcb-shm.so.0 => /usr/lib/libxcb-shm.so.0 (0x7f5961fda000) libxcb.so.1 => /usr/lib/libxcb.so.1 (0x7f5961fb3000) libxcb-render.so.0 => /usr/lib/libxcb-render.so.0 (0x7f5961fa4000) libXrender.so.1 => /usr/lib/libXrender.so.1 (0x7f5961f98000) libXext.so.6 => /usr/lib/libXext.so.6 (0x7f5961f85000) libz.so.1 => /lib/libz.so.1 (0x7f5961f6b000) libbz2.so.1 => /usr/lib/libbz2.so.1 (0x7f5961f5c000) libexpat.so.1 => /usr/lib/libexpat.so.1 (0x7f5961f3a000) libuuid.so.1 => /lib/libuuid.so.1 (0x7f5961f31000) libXau.so.6 => /usr/lib/libXau.so.6 (0x7f5961f2c000) libXdmcp.so.6 => /usr/lib/libXdmcp.so.6 (0x7f5961f24000) libbsd.so.0 => /usr/lib/libbsd.so.0 (0x7f5961f0e000)

jonny64bit avatar Dec 13 '19 16:12 jonny64bit

That's odd; I don't see anything that immediately catches my eye.

Could you try running your program with LD_DEBUG set to libs? E.g.:

export LD_DEBUG=libs
./myapp

It should get you a more precise idea of which library fails to load.

qmfrederik avatar Dec 13 '19 17:12 qmfrederik

@qmfrederik i have to admit i struggled to get any output out of doing LD_DEBUG=libs. Thanks for your help.

i have managed to fix my issue by doing:

RUN apk add libgdiplus-dev fontconfig ttf-dejavu --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted

Thanks to this guy for working it out.

jonny64bit avatar Dec 17 '19 11:12 jonny64bit