vescon / MethodBoundaryAspect.Fody

A Fody weaver which allows to decorate methods and hook into method start, method end and method exceptions.
MIT License
251 stars 75 forks source link

Async & InvalidProgramException : Common Language Runtime detected an invalid program. #97

Closed wzswzd closed 2 years ago

wzswzd commented 2 years ago

async & InvalidProgramException i find out MethodBoundaryAspect will throw a InvalidProgramException : Common Language Runtime detected an invalid program. only like this :

[Logging]
async Task TestMethodAsync()
{
    try
    {
        // do nothing
    }
    catch (Exception ex)
    {
        throw new Exception(""); // or throw;
    }
}

and add this code can avoid crush :

[Logging]
async Task TestMethodAsync()
{
    try
    {
        // do nothing
    }
    catch (Exception ex)
    {
        throw new Exception(""); // or throw;
        await Task.Yield(); // <- add this line
    }
    // or add here "await Task.Yield();" // <- add this line
}
psaxton commented 2 years ago

This appears to be related to #66 in that a statement appears to be required between the end of a code block and the end of the method.

psaxton commented 2 years ago

After further discovery, this is similar to #66 but is actually caused by something different.

psaxton commented 2 years ago

I've created a branch on my fork https://github.com/psaxton/MethodBoundaryAspect.Fody/tree/issue97/invalid-program-exception and pushed commit 91482ee with tests proving the try-catch defects.

psaxton commented 2 years ago

This will be fixed with PR #100

Ralf1108 commented 2 years ago

@psaxton Thank you very much for your contribution !

Ralf1108 commented 2 years ago

please checkout nuget packeage 2.0.147-pre if issues are solved

wzswzd commented 2 years ago

2.0.147-pre is good for me. no InvalidProgramException any more. Thanks! @psaxton @Ralf1108