I'm getting this error message randomly through my .NET application in Azure.
There seems to be very limited information available on the error, and I wondered if anyone knew what may be causing it?
It seems to occur when a certain stored procedure is called, but its really difficult to re-create and happens randomly.
System.BadImageFormatException: Bad IL format.
at System.Data.SqlClient.SqlConnection.ValidateAndReconnect(Action beforeDisconnect, Int32 timeout)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
at System.Data.SqlClient.SqlCommand.BeginExecuteNonQuery(AsyncCallback callback, Object stateObject)
at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl(Func`3 beginMethod, Func`2 endFunction, Action`1 endAction, Object state, TaskCreationOptions creationOptions)
at System.Data.SqlClient.SqlCommand.ExecuteNonQueryAsync(CancellationToken cancellationToken)
2 Answers
Chances are this is because of a processor architecture mismatch: loading 64-bit assemblies when you use 32-bit causes this, for instance.
Some things to check:
- If your application uses 32-bit components, make sure that it always runs as a 32-bit application.
- Make sure that you are not using a component that was created with a different version of the .NET Framework.
- Make sure that the file image is a valid managed assembly or module.
And, as far as the first item on the list goes:
If the Platform target property for your application project is set to AnyCPU, the compiled application can be run in either 64-bit or 32-bit mode. When it runs as a 64-bit application, the just-in-time (JIT) compiler produces 64-bit native code. If the application depends on a 32-bit managed or unmanaged component, that component will fail to load in 64-bit mode. To correct this problem, set the project's Platform target property to x86 and recompile.
Source: Troubleshooting Exceptions: System.BadImageFormatException
Also if you are trying to load .exe generated by netcore 3.1 and obtain still : System.BadImageFormatException: 'Bad IL format.'
even if you double checked x64 uniformity between loader and target ... you can try to load the associated .dll !
In my case it continued to failed at loading my console app.exe , but didnot complained to load the dll, which visual studio generates for you !
In fact the .exe is not MSIL , it looks to be a boot loader of the MSIL code in the .dll