System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B). dll import in Azure function

Morshed 1 Reputation point
2021-07-27T13:43:30.887+00:00

I wrote an azure function and use a 3rd party dll file using dllimport. When I debugged the code, I am getting the error when dll's method called.

I used .net core 3.1 and VS 2019 IDE

I searched a lot but did not get any solution.

I used dumpbin and dependencies(x64) tools to know more info. The dll is 32 bit and all dependency files are available in my system "windows\syswow64\kernal32.dll" and "windows\syswow64\msvcr110.dll".

Now, if I change build platform target from anycpu to x86, azure function does not run. I am getting error and shows is console log which is as follows:

[2021-07-27T13:19:31.887Z] A host error has occurred during startup operation 'e81d42a7-9706-42ce-8956-941d9c5240a5'.
[2021-07-27T13:19:31.888Z] System.Private.CoreLib: Could not load file or assembly 'AzureFunctionNetCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
Value cannot be null. (Parameter 'provider')

I am totally stuck. Please give me solution or hints or ideas?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,932 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Amir Farrokh Ghanbarpour 6 Reputation points
    2022-05-18T08:09:45.997+00:00

    Please also note that the version of refrenced dll file(s) which is in use is very crucial. I had almost the same problem with "Microsoft.WebView2.FixedVersionRuntime.101.0.1210.39.x64" when I tried to use the WebView2 component in the MMC Snap-Ins with types of "HTMLView" or "FormView".

    I just copied the referenced dll file in a proper path that was accessible for the project (you could just put it beside your project output files first to test it) and then WebView2 browser started to function as expected. Microsoft error messages sometimes (at least in my case) was a little bit misleading and did not convey enough and to the point information.

    I received "BadImageFormatException" that normally occurs when you mix platform targets (for example using a dll file compiled in X64 in an application that targeted for x86 or vice versa) or mix native code and .NET but that was not my problem at all. I hope this help one who may stuck in.

    1 person found this answer helpful.
    0 comments No comments

  2. MikeUrnun 9,777 Reputation points Moderator
    2021-08-18T22:36:01.383+00:00

    Hello @Morshed - Sorry for the delayed response. Are you still hitting this issue? If so, could you try out the steps in the following link and see if that helps? https://github.com/Azure/azure-functions-core-tools/wiki/Running-64bit-functions-in-Visual-Studio

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.