System.BadImageFormatException: Bad IL Format when monitoring events on .NET Core 3.0-preview5
See original GitHub issueDescription
FluentAssertions throws a BadImageFormatException
when invoking a monitored event. This only occurs in the .NET Core 3.0 preview.
Complete minimal example reproducing the issue
[TestClass]
public class Repro
{
public delegate void EventDelegate();
public class Tester
{
public event EventDelegate TestEventDelegate;
public event EventHandler TestEventHandler;
public void RaiseEventDelegate()
{
TestEventDelegate?.Invoke();
}
public void RaiseEventHandler()
{
TestEventHandler?.Invoke( this, EventArgs.Empty );
}
}
[TestMethod]
public void TestRaiseEventDelegate()
{
var tester = new Tester();
var monitor = tester.Monitor();
tester.RaiseEventDelegate();
monitor.Should().Raise( nameof( Tester.TestEventDelegate ) );
}
[TestMethod]
public void RaiseEventEventHandler()
{
var tester = new Tester();
var monitor = tester.Monitor();
tester.RaiseEventHandler();
monitor.Should().Raise( nameof( Tester.TestEventHandler ) );
}
}
Expected behavior:
EventHandlerFactory
should emit a handler that does not cause a BadImageFormatException
to be thrown.
Actual behavior:
A BadImageFormatException
is thrown when the event is invoked.
Versions
-
Which version of Fluent Assertions are you using? FluentAssertions v5.6.0 via NuGet
-
Which .NET runtime and version are you targeting? .NET Core 3.0-preview5
Additional Information
Have not looked into it. The exception is thrown immediately upon invoking the event. Bug does not occur in earlier versions of .NET Core (tested with 2.2).
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (10 by maintainers)
Top Results From Across the Web
Visual Studio 2019 · Issue #2907 · appveyor/ci
System.BadImageFormatException: Bad IL Format when monitoring events on .NET Core 3.0-preview5 fluentassertions/fluentassertions#1047.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Got it working by altering
EventHandlerFactory.GenerateHandler
in one of two ways:a) Change from
IEventRecorder
toEventRecorder
into
or b) change from
OpCodes.Call
toOpCodes.CallVirt
into
Interface method calls will always be virtual, and the JIT can’t inline those, so technically
Call
should have beenCallvirt
(orIEventRecorder
should have been strongly typed) in the first place.~I’m not sure what has changed, but given the fix @jnyrup has found, I would assume that it has to do with more rigorous code checking on the part of RyuJIT.~
See dotnet/coreclr #24330