Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.BadImageFormatException: 'Bad IL format.' #1622

Closed
atpyk opened this issue May 31, 2018 · 25 comments
Closed

System.BadImageFormatException: 'Bad IL format.' #1622

atpyk opened this issue May 31, 2018 · 25 comments

Comments

@atpyk
Copy link

atpyk commented May 31, 2018

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
Copy link
Author

atpyk commented May 31, 2018

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
Copy link
Author

atpyk commented May 31, 2018

So sad, it doesn't work too.

@VladislavAntonyuk
Copy link

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

@atpyk
Copy link
Author

atpyk commented Jun 1, 2018

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.

@VladislavAntonyuk
Copy link

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

@atpyk
Copy link
Author

atpyk commented Jun 1, 2018

I have reinstall my OS, it works.
Thanks.

@mangod3
Copy link

mangod3 commented Jun 3, 2018

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

@rafamerlin
Copy link

rafamerlin commented Jun 3, 2018

@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.

@mangod3
Copy link

mangod3 commented Jun 4, 2018

@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

@noahfalk
Copy link
Member

noahfalk commented Jun 4, 2018

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?

@mangod3
Copy link

mangod3 commented Jun 4, 2018

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

@joseclament
Copy link

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?

@noahfalk
Copy link
Member

noahfalk commented Jun 6, 2018

@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.

@joseclament
Copy link

@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.

@nil4
Copy link

nil4 commented Jun 7, 2018

@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) 

@joseclament
Copy link

joseclament commented Jun 7, 2018

@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.

@nil4
Copy link

nil4 commented Jun 7, 2018

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).

@mangod3
Copy link

mangod3 commented Jun 7, 2018

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.

@nil4
Copy link

nil4 commented Jun 7, 2018

@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 [[],[]]

@noahfalk
Copy link
Member

noahfalk commented Jun 7, 2018

@joseclament - Glad you were able to get the bottom of it and very appreciated that you came back to post your findings in case others hit the same issue in the future! Indeed it looks like your issue had a similar symptom but different root cause than the other ones here. I created #1670 to track any further work there.

@nil4 - Thanks for the repro! Let me try repro your issue so we can figure out if this is the same root cause or something else.

@noahfalk
Copy link
Member

noahfalk commented Jun 8, 2018

@nil4 - Thanks, I was able to reproduce the problem easily using your excellent instructions! Although the AppDynamics issue is slighly different than the NewRelic one (AppDynamics does not call ICorProfilerInfo::SetILFunctionBody or ICorProfilerFunctionControl::SetILFunctionBody) , the fix I submitted earlier today (#18322) does solve both issues at the same time.

I wasn't sure if you were an employee with AppDynamics or a customer of AppDynamics? For potential short term resolutions to the issue you could either continue to use .Net Core 2.0 or use a daily build of .Net Core that has this fix as soon as the installers are ready for download (I'll write back again when they are). A little further in the future I'm attempting to get this fix into the official servicing releases for 2.1. I can also try to reach out to AppDynamics to see if there are any additional solutions available for the issue.

Let me know if you've got any other questions or concerns?

@nil4
Copy link

nil4 commented Jun 8, 2018

@noahfalk glad I could help, and thank you for the update, much appreciated! I am a customer, and have opened an AppDynamics support ticket about this; will update it shortly with a link to this issue.

@Petermarcu
Copy link
Member

Issue moved to dotnet/coreclr #18448 via ZenHub

@cuvillier
Copy link

My build just stuck beacuse of a "Bad IL format" .

I just clean up all the nuget caches (local & global) and restore everything.
Then, it works...

Hop this help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants