Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

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

Open
drwharris opened this issue Dec 14, 2017 · 28 comments
Assignees

Comments

@drwharris
Copy link

drwharris commented Dec 14, 2017

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.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.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.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.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.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.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.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.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.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.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.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.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.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.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.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.d__7.MoveNext()

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

@drwharris
Copy link
Author

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"

@JanKallman
Copy link
Owner

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

@JanKallman JanKallman self-assigned this Mar 30, 2018
@LeJerk
Copy link

LeJerk commented May 31, 2018

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.

@VahidN
Copy link
Contributor

VahidN commented May 31, 2018

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"]

@jgobl
Copy link

jgobl commented Jun 1, 2018

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

apt-get install -y libc6-dev

@qmfrederik
Copy link

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.

@mherarsh
Copy link

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

@mii9000
Copy link

mii9000 commented Mar 12, 2019

Any idea how to resolve this problem for Centos?

@nagstaku
Copy link

nagstaku commented Apr 24, 2019

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"]

@justlearntutors
Copy link

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

@oguzhantopcu
Copy link

#dotnet/core#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

@EliuTimana
Copy link

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.

@songzheng45
Copy link

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. :)

@necipsunmaz
Copy link

necipsunmaz commented Oct 31, 2019

Any idea how to resolve this problem for Centos?

Yes, worked this command lines;

yum install glibc-devel glibc-devel.i686

@Jerome2606
Copy link

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

@qmfrederik
Copy link

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.

@Jerome2606
Copy link

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 ^^

@qmfrederik
Copy link

Sorry, don't know about App Service.

@felipe-rodrigues
Copy link

felipe-rodrigues commented Dec 11, 2019

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

@qmfrederik
Copy link

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

@felipe-rodrigues
Copy link

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

No I'm using 2.2

@qmfrederik
Copy link

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 😄 .

@felipe-rodrigues
Copy link

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

@jonny64bit
Copy link

jonny64bit commented Dec 13, 2019

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

@qmfrederik
Copy link

@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.

@jonny64bit
Copy link

@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)

@qmfrederik
Copy link

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.

@jonny64bit
Copy link

jonny64bit commented Dec 17, 2019

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests