DinkToPdf icon indicating copy to clipboard operation
DinkToPdf copied to clipboard

System.BadImageFormatException: An attempt was made to load a program with an incorrect format.

Open fabercs opened this issue 5 years ago • 15 comments

I have asp.net core 2.1 project which I use 64 bit libwkhtmltox.dll. Locally all works fine either while debugging or hosting on local iis. However when I host it on server which is win 64 bit OS, exception is thrown as like;

System.AggregateException: One or more errors occurred. (An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)) ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
   at DinkToPdf.WkHtmlToXBindings.wkhtmltopdf_init(Int32 useGraphics)
   at DinkToPdf.PdfTools.Load()
   at DinkToPdf.BasicConverter.Convert(IDocument document)
   at DinkToPdf.SynchronizedConverter.<>n__0(IDocument document)
   at DinkToPdf.SynchronizedConverter.<>c__DisplayClass5_0.<Convert>b__0()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__278_1(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
   --- End of inner exception stack trace ---
   at DinkToPdf.SynchronizedConverter.Invoke[TResult](Func`1 delegate)
   at DinkToPdf.SynchronizedConverter.Convert(IDocument document)

deps.json configuration is as follows

{
  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v2.1",
    "signature": "17c0d97ba89af48f20c96c6f42ea9f22e1c7de55"
  },
  "compilationOptions": {
    "defines": [
      "TRACE",
      "RELEASE",
      "NETCOREAPP",
      "NETCOREAPP2_1"
    ],
    "languageVersion": "",
    "platform": "AnyCPU",
    "allowUnsafe": false,
    "warningsAsErrors": false,
    "optimize": true,
    "keyFile": "",
    "emitEntryPoint": true,
    "xmlDoc": false,
    "debugType": "portable"
  },
  "targets": {
    ".NETCoreApp,Version=v2.1": {
      "KatefWeb/1.0.0": {
        "dependencies": {
          "Dapper": "1.50.5",
          "Dapper.Contrib": "1.50.5",
          "DinkToPdf": "1.0.8",
          "Katef.Core": "1.0.0",
          "Microsoft.AspNetCore.App": "2.1.1",
          "Microsoft.NETCore.App": "2.1.0",
          "Microsoft.VisualStudio.Web.CodeGeneration.Design": "2.1.1",
          "Serilog": "2.8.0",
          "Serilog.AspNetCore": "2.1.1",
          "Serilog.Settings.Configuration": "3.0.1",
          "Serilog.Sinks.MSSqlServer": "5.1.2",
          "System.Drawing.Common": "4.5.1"
        },

Publishing with options Portable and Framework-Dependent.

fabercs avatar Mar 27 '19 21:03 fabercs

As I know this exception is a 64 bit/ 32 bit conflict in general, I replaced the dll on server with a 32 bit one and exception gone, but I don't know why.

fabercs avatar Mar 28 '19 05:03 fabercs

@fabercs I faced same Issue and after replacing all the dll on server with 32 bit it worked for me also.

Thank you :) !

iAmBipinPaul avatar Jun 17 '20 11:06 iAmBipinPaul

As I know this exception is a 64 bit/ 32 bit conflict in general, I replaced the dll on server with a 32 bit one and exception gone, but I don't know why.

x2

Ksantacr avatar Jul 14 '20 22:07 Ksantacr

@Ksantacr from where did you get the 32 bit one? TY

bronzewind avatar Jul 20 '20 20:07 bronzewind

@Ksantacr from where did you get the 32 bit one? TY

From here https://github.com/rdvojmoc/DinkToPdf/tree/master/v0.12.4/32%20bit

Ksantacr avatar Jul 20 '20 21:07 Ksantacr

Just wanted to check the source... I got it from the same place and I can't make it work, I'm having this error no matter the version. I'm using Core 3.1. Any help would be really much appreciated!

bronzewind avatar Jul 21 '20 02:07 bronzewind

Just wanted to check the source... I got it from the same place and I can't make it work, I'm having this error no matter the version. I'm using Core 3.1. Any help would be really much appreciated!

Full source code https://github.com/rdvojmoc/DinkToPdf/tree/master/src/DinkToPdf I solved this issue in IIS .net core app 3.1

Ksantacr avatar Jul 21 '20 14:07 Ksantacr

Could you share an example project? It does not work for me at all. I have even created an independent issue. No matter what I try it just doesn't work and the error is the same.

bronzewind avatar Jul 21 '20 16:07 bronzewind

Could you share an example project? It does not work for me at all. I have even created an independent issue. No matter what I try it just doesn't work and the error is the same.

Same here using core 3.1 ..it works fine locally but doesn't work on iis

Oriahie avatar Dec 06 '20 19:12 Oriahie

Hey guys does anyone have any updates on this? I'm experiencing the same issue. On my local the 64bit library works, however in Azure only the 32bit works. Experiencing the same error as above:

An attempt was made to load a program with an incorrect format.

Does anyone have any thoughts on a way to include both libraries and have the function work out which it requires? Cheers!

jimmytricks avatar Aug 23 '21 15:08 jimmytricks

@jimmytricks you can change azure to be 64bit - image

IeuanWalker avatar Aug 24 '21 15:08 IeuanWalker

Thanks @IeuanWalker - haven't tried this yet, but looks like it'll resolve it, cheers

jimmytricks avatar Aug 31 '21 14:08 jimmytricks

Hi, I have the same issue however my issue is that I can't even make it work locally. I tried replacing 32bit and 64bit but still having this issue. I am using it on my .NET 5 Project and Visual Studio 2019 Community on windows 10 64 bit environment

image

flotesrx avatar Jul 28 '22 02:07 flotesrx

I resolved this as below [appreciate everyone's answer which led me to the solution.]

  1. Created Azure Function App [Windows, .Net 6]
  2. Deployed Azure Function [Used 32bit libwkhtmltox.dll]
  3. Got the 'above noted' error.
  4. Tried replacing 64bit "libwkhtmltox.dll' to 32 bit
  5. Still the same error was there.
  6. Then Changed Azure Function app runtime from 32bit to 64bit.
  7. Placed 64bit libwkhtmltox.dll
  8. Did refresh
  9. Tested - Worked fine.

Hope this helps!

Sach88 avatar Mar 02 '23 11:03 Sach88

Hello I tried updating Azure to 64 bit but still giving the error

System.AggregateException: One or more errors occurred. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) at DinkToPdf.WkHtmlToXBindings.wkhtmltopdf_init(Int32 useGraphics) at DinkToPdf.PdfTools.Load() in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\PdfTools.cs:line 27 at DinkToPdf.BasicConverter.Convert(IDocument document) in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\BasicConverter.cs:line 42 at DinkToPdf.SynchronizedConverter.<>n__0(IDocument document) at DinkToPdf.SynchronizedConverter.<>c__DisplayClass5_0.<Convert>b__0() in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\SynchronizedConverter.cs:line 27 at System.Threading.Tasks.Task1.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of inner exception stack trace --- at DinkToPdf.SynchronizedConverter.Invoke[TResult](Func1 delegate) in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\SynchronizedConverter.cs:line 48 at DinkToPdf.SynchronizedConverter.Convert(IDocument document) in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\SynchronizedConverter.cs:line 27 at PdfService.GeneratePdf(String header, String htmlContent) in D:\Yash\Transom Corporation\Scentia Selection Program\selection-program\api\WebApi\Helpers\PDF\PdfService.cs:line 19 at WebApi.Controllers.PdfController.Get(String id, String type, Nullable1 dimensions, Nullable1 electrical, String funds, Nullable1 multiplier, String delivery, String terms) in D:\Yash\Transom Corporation\Scentia Selection Program\selection-program\api\WebApi\Controllers\PdfController.cs:line 266 ---> (Inner Exception #0) System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) at DinkToPdf.WkHtmlToXBindings.wkhtmltopdf_init(Int32 useGraphics) at DinkToPdf.PdfTools.Load() in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\PdfTools.cs:line 27 at DinkToPdf.BasicConverter.Convert(IDocument document) in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\BasicConverter.cs:line 42 at DinkToPdf.SynchronizedConverter.<>n__0(IDocument document) at DinkToPdf.SynchronizedConverter.<>c__DisplayClass5_0.<Convert>b__0() in C:\Users\admin\Documents\DinkToPdf\src\DinkToPdf\SynchronizedConverter.cs:line 27 at System.Threading.Tasks.Task1.InnerInvoke() at System.Threading.Tasks.Task.Execute()<---

Any help on this will be appriciated

Aryesh-M avatar Jan 25 '24 04:01 Aryesh-M