question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.BadImageFormatException: Bad IL Format when monitoring events on .NET Core 3.0-preview5

See original GitHub issue

Description

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:closed
  • Created 4 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
jnyrupcommented, May 23, 2019

Got it working by altering EventHandlerFactory.GenerateHandler in one of two ways:

a) Change from IEventRecorder to EventRecorder

MethodInfo methodToCall = typeof(IEventRecorder).GetMethod("RecordEvent",
    BindingFlags.Instance | BindingFlags.Public);

into

MethodInfo methodToCall = typeof(EventRecorder).GetMethod("RecordEvent",
    BindingFlags.Instance | BindingFlags.Public);

or b) change from OpCodes.Call to OpCodes.CallVirt

ilGen.EmitCall(OpCodes.Call, methodToCall, null);

into

ilGen.EmitCall(OpCodes.CallVirt, methodToCall, null);
2reactions
Wildenhauscommented, May 25, 2019

Interface method calls will always be virtual, and the JIT can’t inline those, so technically Call should have been Callvirt (or IEventRecorder 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found