Home > Enterprise >  How to fix Azure app service unable to load DLL 'libwkhtmltox' or one of its dependencies
How to fix Azure app service unable to load DLL 'libwkhtmltox' or one of its dependencies

Time:09-30

Azure app service unable to load libwkhtmltox. I work fine on the local machine, But upon deployment to azure, I got an error that cannot load or one of its dependencies. I search online and made some changes to my code, I got this error again.

I got the error below when I push to azure again

BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B) System.Runtime.InteropServices.NativeLibrary.LoadFromPath(string libraryName, bool throwOnError)

Below is the updated code

            var wkHtmlToPdfFileDllName = "libwkhtmltox";
             if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                wkHtmlToPdfFileDllName  = ".so";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                wkHtmlToPdfFileDllName  = ".dylib";
            }
            else
            {
                wkHtmlToPdfFileDllName  = ".dll";
            }

            var wkHtmlToPdfPath = Path.Combine(Directory.GetCurrentDirectory(), "wkhtmltox", wkHtmlToPdfFileDllName);
            CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
            context.LoadUnmanagedLibrary(wkHtmlToPdfPath);
            services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

I also added this to .csproj

  <ItemGroup>
  <None Remove="wkhtmltox\libwkhtmltox.dll" />
</ItemGroup>

<ItemGroup>
  <EmbeddedResource Include="wkhtmltox\libwkhtmltox.dll">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </EmbeddedResource>
</ItemGroup>

CodePudding user response:

According to the error message,

To fix this issue you can try to set the platform target to x86 as below and then try to publish.

enter image description here

For more information please refer to this below links:

. Unable to load DLL 'libwkhtmltox' | GitHub

. BadImageFormatException |MSDN

. An attempt was made to load a program with an incorrect format" even when the platforms are the same | SO THREAD

. DinkToPdf Net Core not able to load DLL files | SO Thread

  • Related