如何修复 Azure 应用服务无法加载 DLL 'libwkhtmltox' 或其依赖项之一

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

Azure 应用服务无法加载 libwkhtmltox。我在本地机器上工作正常,但是在部署到 Azure 时,我收到无法加载或其依赖项之一的错误。我在网上搜索并修改了我的代码,我又遇到了这个错误。

再次推送到azure时出现如下错误

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

下面是更新后的代码

            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()));

我也将其添加到 .csproj

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

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

根据报错信息,

要解决此问题,您可以尝试将平台目标设置为 x86,如下所示,然后尝试发布。

更多信息请参考以下链接:

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