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.

Unable to load assembly in container environment

See original GitHub issue

I have a .Net 2.2 core application that I’m trying to move into a container and I’m having little success getting the assembly loaded. I’m curious has anyone attempted this with any success? I have been hung up on an ELF header error for two days now :

“System.DllNotFoundException: Unable to load shared library ‘/app/libwkhtmltox.dll’ or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: /app/libwkhtmltox.dll: invalid ELF header”

Details :

details

  1. Its a 64-bit container image (Debian 9). It’s worth pointing out I’m currently using microsoft/dotnet:2.2-aspnetcore-runtime and I have tried :

    • mcr.microsoft.com/dotnet/core/runtime:2.2
    • mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim
    • mcr.microsoft.com/dotnet/core/runtime:2.2-bionic
    • mcr.microsoft.com/dotnet/core/runtime:2.2.7-bionic
  2. The app is built/published on the same flavor of Linux

  3. The 64-bit DLL is in the apps working directory as is the dylib and so (see the file sizes in my screenshot)

  4. libgdiplus is already present on the Debian 9 base image (it’s in my Dockerfile regardless)

  5. I’m installing all of the dependencies I could find in the various older issues…

    • libx11-6
    • libxext6
    • libxrender1
    • zlib1g
    • fontconfig
    • libfreetype6
    • libgdiplus
    • libxcb1
    • xfonts-75dpi
    • xfonts-base
  6. The Deb package for wkhtmltox is being installed.

  7. libwkhtmltox.dll is also copied into /usr/lib.

I think the above covers every fix i have found on past issues for loading the dll on ubuntu and container environments, although I want to say none of those were 2.2Core

The relevant part of the C# looks like so :

        private SynchronizedConverter Converter
        {
            get
            {
                if (_converter == null)
                {
                    var context = new CustomAssemblyLoadContext();
                    var wkHtmlToPdfPath = Path.Combine(AppContext.BaseDirectory, $@"libwkhtmltox.dll");
                    Console.WriteLine($"Path to the file : {wkHtmlToPdfPath}");
                    try
                    {
                        context.LoadUnmanagedLibrary(wkHtmlToPdfPath);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("This is where the trouble starts...");
                        Console.WriteLine(e.ToString());
                        Console.WriteLine(e.InnerException.Message.ToString());
                    }

                    _converter = new SynchronizedConverter(new PdfTools());
                }

                return _converter;
            }
        }

Which of course throws an exception :

Loaded ‘/app/DinkToPdf.dll’. Cannot find or open the PDB file. Path to the file : /app/libwkhtmltox.dll This is where the trouble starts… Loaded ‘/usr/share/dotnet/shared/Microsoft.NETCore.App/2.2.7/System.IO.MemoryMappedFiles.dll’. Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled. System.DllNotFoundException: Unable to load shared library ‘/app/libwkhtmltox.dll’ or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: /app/libwkhtmltox.dll: invalid ELF header at System.Runtime.Loader.AssemblyLoadContext.InternalLoadUnmanagedDllFromPath(String unmanagedDllPath) at System.Runtime.Loader.AssemblyLoadContext.LoadUnmanagedDllFromPath(String unmanagedDllPath) at FormsService.ServiceInterface.Common.HTMLToPDFConverter.get_Converter() in /src/FormsService.ServiceInterface/Common/IHTMLToPDFConverter.cs:line 21

My Dockerfile, which has gone through countless permutations over the last 12 hours or so now looks like :

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 51950
EXPOSE 44378

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["FormsService/FormsService.csproj", "FormsService/"]
COPY ["FormsService.ServiceModel/FormsService.ServiceModel.csproj", "FormsService.ServiceModel/"]
COPY ["FormsService.ServiceInterface/FormsService.ServiceInterface.csproj", "FormsService.ServiceInterface/"]
RUN dotnet restore "FormsService/FormsService.csproj"
COPY . .
WORKDIR "/src/FormsService"
RUN dotnet build "FormsService.csproj" -c Release -o /app

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
RUN apt-get update \ 
	&& apt-get install -y zlib1g fontconfig libfreetype6 \
        libx11-6 libxext6 libxrender1 \
		libgdiplus libxcb1 xfonts-75dpi \
		xfonts-base wget \
    && wget -nv -O /tmp/wkhtmltox.deb https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox_0.12.5-1.stretch_amd64.deb \
	&& apt-get -qy install /tmp/wkhtmltox.deb \
	&& cp /usr/local/bin/wkhtmlto* /usr/bin/ \
    && curl -o /usr/lib/libwkhtmltox.so \
        --location \
        https://github.com/rdvojmoc/DinkToPdf/blob/master/v0.12.4/64%20bit/libwkhtmltox.so?raw=true \
	&& curl -o /usr/lib/libwkhtmltox.dylib \
        --location \
        https://github.com/rdvojmoc/DinkToPdf/blob/master/v0.12.4/64%20bit/libwkhtmltox.dylib?raw=true \
	&& curl -o /usr/lib/libwkhtmltox.dll \
        --location \
        https://github.com/rdvojmoc/DinkToPdf/blob/master/v0.12.4/64%20bit/libwkhtmltox.dll?raw=true \
	&& cp /usr/lib/libwkhtmltox* /app \
	&& cp /usr/lib/libwkhtmltox* /app/Dependencies/64Bit/
	

ENTRYPOINT ["dotnet", "FormsService.dll"]

I added the steps to re-download the Dll/So/Dylib on the off chance that I had some corrupt version locally. Alas this had no effect…

Does anyone have any insight into what the underlying issue maybe? I’m assuming the ELF Header is signaling that a dependency is missing or that dependency is the incorrect arch (32-bit) but I’m grasping at straws at this point.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
oscartorres9commented, Oct 31, 2019

I have the exactly same error on centos 7,which working nicely in loacl windows 10, but occur the error when I publish my app to the centos 7 server. please tell me how to solve this fxxking problem… thanks

0reactions
mKay00commented, Aug 10, 2020

What worked for me was to install wkhtmltopdf with apt (worked on ubuntu, but resulted in a big docker image) or use the correct binary from here, download with wget, install the .deb file with dpkg, which results in an error and then use apt-get -f install -y to correctly install it.

Here is my dockerfile to build and deploy an asp.net core app to heroku which uses wkhtmltopdf (the images used are based on debian 10):

# NuGet restore
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["{your_solution}/{your_project}.csproj", "{your_project}/"]
RUN dotnet restore "{your_project}/{your_project}.csproj"
COPY . .

# publish
WORKDIR "/src/{your_project}"
RUN dotnet publish "{your_project}.csproj" -c Release -o /app/publish

# release
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS release_base
WORKDIR /tmp
# Add wkhtmltopdf and its depoendencies
RUN apt-get update \
    && apt-get install -y wget \
    && wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb \
    && dpkg -i ./wkhtmltox_0.12.6-1.buster_amd64.deb \
    ; apt-get -f install -y \
    && rm wkhtmltox_0.12.6-1.buster_amd64.deb \
    && rm -rf /var/cache/apt/lists/*

FROM release_base AS release
WORKDIR /app
COPY --from=build /app/publish .

# Run the image as a non-root user
RUN useradd -m myuser
USER myuser

# heroku uses the following
CMD ASPNETCORE_URLS=http://*:$PORT dotnet {your_project}.dll
Read more comments on GitHub >

github_iconTop Results From Across the Web

net core project cannot load assemblies in linux docker ...
I have the following issue: I have one project which loads another project using Assembly.LoadFile() . Everything works fine on windows but when ......
Read more >
How to resolve “Could not load file or assembly … or one of its ...
Cached Assembly: Check the same assembly bound before then use it, if it failed then current request is failed immediately without any further...
Read more >
Cannot load file or assembly 'System.Runtime'
When I try to run the application, I get this error: Could not load file or assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, ...
Read more >
Unable to load DLL 'PortableEngine.Native.dll' or one of its ...
Hi, I get this error on a windows container. with .NET Core 3.1 so I found an article ...
Read more >
Troubleshoot deployment issues in Lambda
Error: Unable to load type 'Function.Handler' from assembly 'Function'. The name of the file or class in your function's handler configuration doesn't match ......
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