core icon indicating copy to clipboard operation
core copied to clipboard

System.BadImageFormatException: 'Bad IL format.'

Open atpyk opened this issue 8 years ago • 25 comments

I've upgraded dotnetcore2.0 to 2.1, and upgraded all packages too. When I startup my web application, the exception prompts.

StackTrace: at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors) at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at GrapeLEAF.AdminService.Program.BuildWebHost(String[] args) in E:\workspace\dotnet\XXX\XXX.AdminService\XXX.AdminService\Program.cs:line 16 at GrapeLEAF.AdminService.Program.Main(String[] args) in E:\workspace\dotnet\XXX\XXX.AdminService\XXX.AdminService\Program.cs:line 12

atpyk avatar May 31 '18 02:05 atpyk

I uninstall original .net core sdk and runtime; and reinstall VS2017 and 2.1 sdk. I create a new webapi application by scaffold, it doesn't work.

atpyk avatar May 31 '18 07:05 atpyk

https://blogs.msdn.microsoft.com/webdev/2018/05/07/asp-net-core-2-1-0-rc1-now-available/

VladislavAntonyuk avatar May 31 '18 08:05 VladislavAntonyuk

So sad, it doesn't work too.

atpyk avatar May 31 '18 09:05 atpyk

Make sure you have installed the latest VS and try to reinstall .net core. Roolback your changes to .net core 2.0 and follow the instruction I have sent you

VladislavAntonyuk avatar May 31 '18 10:05 VladislavAntonyuk

I debug into Microsoft.AspNetCore.Hosting and find out the error line: var hostingServices = BuildCommonServices(out var hostingStartupErrors);

It will throw the exception: 'Bad IL format.'

But if I rename this method to BuildCommonServices111 or other, It works. So weird~ It seems like the namespace "Microsoft.AspNetCore.Hosting" conflicts with others, the .net runtime involves dlls of different version at same time.

atpyk avatar Jun 01 '18 03:06 atpyk

make sure you have installed version 2.1.300 and try to uninstall all previous versions. Also doublecheck you have finished all steps in the instruction I've sent you

VladislavAntonyuk avatar Jun 01 '18 08:06 VladislavAntonyuk

I have reinstall my OS, it works. Thanks.

atpyk avatar Jun 01 '18 08:06 atpyk

Did you have NewRelic monitoring your .net app by any chance when you ran into the issue?

mangod3 avatar Jun 03 '18 01:06 mangod3

@mangod3 I was about to comment here. My issue seemed to be Newrelic. Was returning the IL issue. I was building it and running it on docker.

rafamerlin avatar Jun 03 '18 23:06 rafamerlin

@Merurino Thanks for responding. After debugging the issue it looks like the 2.1 changes related to code versioning (https://github.com/dotnet/coreclr/blob/master/Documentation/design-docs/code-versioning-profiler-breaking-changes.md) are causing issues with profiling APIs. @noahfalk who should we follow up with to get this investigated, looks like the ILHeader is not getting initialized correctly after updating a method IL using SetILFunctionBody

mangod3 avatar Jun 04 '18 03:06 mangod3

Sorry you are running into trouble! Feel free to start with me as the investigator and I can pull in others as needed. To help get us started:

  1. Could you post steps I can follow to try reproducing the problem?
  2. Can you let me know what you observed while debugging that lead you to your conclusions?

noahfalk avatar Jun 04 '18 20:06 noahfalk

Thanks for your response @noahfalk. Will package up a repro and send over email.

mangod3 avatar Jun 04 '18 21:06 mangod3

I have a web job built on netcoreapp2.1 console application, targeting the win10-x64 and getting the "Unhandled Exception: System.BadImageFormatException: Could not load file or assembly exception". I haven't had any issue with locally. Deployed using the visual studio directly to the app service. Any thoughts?

joseclament avatar Jun 06 '18 15:06 joseclament

@joseclament - Are you aware of any .NET profiler or monitoring agent being used in your deployment environment? If not we should probably a open a new issue because there would be a decent chance that the root cause for you will be unrelated to the root cause here.

noahfalk avatar Jun 06 '18 21:06 noahfalk

@noahfalk , I am using AppInsight to push the logs, other than no profiling or tooling. Let me know if I need to open a new issue.

joseclament avatar Jun 07 '18 08:06 joseclament

@noahfalk I am seeing a similar issue using the AppDynamics .NET Core profiler agent on a netcoreapp2.1 application and .NET Core SDK 2.1.300.

When the application starts, the following exception is thrown:

System.BadImageFormatException
  HResult=0x8007000B
  Message=Bad IL format.
  Source=System.Private.CoreLib
  StackTrace:
   at System.AppDomainManager..ctor()
   at System.Reflection.Assembly.GetEntryAssembly()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at MyApp.Web.Startup.Main(String[] args) 

nil4 avatar Jun 07 '18 14:06 nil4

@noahfalk and @nil4 , I got the bottom of the problem. It took couple of days to get the bottom of the issue and made me bit nut. I was able to dig and find this link: https://tutel.me/c/programming/questions/42726350/how+to+change+azure+app+service+to+64bit

Your core should be built on x86 version even if the deploying server(Azure) is based on 64 bit architecture. Because the 64 bit .NET core processes using the .NET core runtime (as opposed to the .NET Framework runtime) are not yet supported on Azure but is planned to be coming in the future. You can verify this by go to the kudu and type dotnet --info. You can see something like this below:

.NET Core SDK (reflecting any global.json): Version: 2.1.300 Commit: 32f29b6eb9

Runtime Environment: OS Name: Windows OS Version: 10.0.14393 OS Platform: Windows RID: win10-x86 _Base Path: D:\Program Files (x86)\dotnet\sdk\2.1.300_ [You can see only the x86 version here]

Host (useful for support): Version: 2.1.0 Commit: caa7b7e2ba

.NET Core SDKs installed: 1.1.8 [D:\Program Files (x86)\dotnet\sdk] 2.1.101 [D:\Program Files (x86)\dotnet\sdk] 2.1.300 [D:\Program Files (x86)\dotnet\sdk]

You may be able to debug this locally "Any CPU" option as you might have x86 and x64 based libraries installed on your machine. I could not find any documentation on MSDN about this. I was assumed it would have been automatically support x64 version. Also make sure that the application setting is enabled for the "32" bit.

joseclament avatar Jun 07 '18 14:06 joseclament

I am not using Azure. The error occurs on my local PC when launching the application, with IIS Express, under the VS debugger, regardless of whether IIS Express 64-bit or 32-bit is selected.

The application starts up correctly if I remove the AppDynamics.Agent.Windows NuGet package, or set CORECLR_ENABLE_PROFILING environment variable to 0 in my Web.config (disabling the profiler).

nil4 avatar Jun 07 '18 15:06 nil4

is it possible for you to capture a full native stack of the exception? That might help with identifying if you are hitting the same issue or something different.

mangod3 avatar Jun 07 '18 16:06 mangod3

@noahfalk Here are the steps to reproduce the issue I see. In a command prompt, run:

mkdir Repro1622 && pushd Repro1622
dotnet new web
dotnet add package AppDynamics.Agent.Windows --version 4.4.3

Open the project in Visual Studio, and build it once.

A number of files brought by the AppDynamics package show up under the project, including Repro1622.AppDynamicsConfig.json. Edit this file and replace its content with:

{
  "controller": {
    "host": "localhost",
    "port": 443,
    "account": "customer1",
    "password": "c025be84-d72d-4f6e-b5b4-b33ce264992b",
    "ssl": true,
    "enable_tls12": true
  },
  "application": {
    "name": "irrelevant",
    "tier": "irrelevant",
    "node": "irrelevant"
  }
}

These values are not important, they just need to be different than the default placeholders for the profiler to activate.

Add a Web.config file to the project, with the following content:

<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%">
      <environmentVariables>
        <environmentVariable name="CORECLR_ENABLE_PROFILING" value="1" />
        <environmentVariable name="CORECLR_PROFILER" value="{39AEABC1-56A5-405F-B8E7-C3668490DB4A}" />

        <environmentVariable name="CORECLR_PROFILER_PATH_32" value="D:\Repro1622\bin\Debug\netcoreapp2.1\AppDynamics.Profiler_x86.dll"/>
        <environmentVariable name="CORECLR_PROFILER_PATH_64" value="D:\Repro1622\bin\Debug\netcoreapp2.1\AppDynamics.Profiler_x64.dll"/>
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>

⚠️ Per the AppDynamics docs, the two CORECLR_PROFILER_PATH_* variables must be full paths; adjust as required.

Start debugging (F5) with IIS Express and note BadImageFormatException is thrown at System.AppDomainManager..ctor() when the application starts.

Find the newest .log file under %ProgramData%\AppDynamics\DotNetAgent\Logs\Profiler, which should be similar to:

2018-06-07 18:22:31.086553<info>:Use CoreCLR profiler
2018-06-07 18:22:31.089553<info>:Should instrument standalone profiler process (config found at D:\Repro1622\bin\Debug\netcoreapp2.1\Repro1622.AppDynamicsConfig.json): dotnet as app Repro1622.dll
2018-06-07 18:22:31.092555<info>:Agent Configuration: {"controller":{"account":"customer1","enableTls12":true,"ssl":true,"host":"localhost","password":"***","port":443},"application":{"tier":"irrelevant","node":"irrelevant","name":"irrelevant"}}
2018-06-07 18:22:31.092555<info>:Enabled rejit normally, disabled all NGEN images.
2018-06-07 18:22:31.163553<error>:InjectMEE failed.
2018-06-07 18:22:31.935346<info>:Transforming 57540:clrhost:System.Void System.AppDomainManager..ctor() with [[],[]]

nil4 avatar Jun 07 '18 16:06 nil4