Skip to content

Unable to load assembly in container environment  #97

@jxmoore

Description

@jxmoore

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions