Bad IL format - Strange .NET error

10,392

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

Share:
10,392
Philip
Author by

Philip

Updated on June 12, 2022

Comments

  • Philip
    Philip almost 2 years

    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)
    
  • Philip
    Philip almost 6 years
    Thanks Rick, appreciate that prompt answer. I've made these changes and will monitor accordingly.