AutoDI icon indicating copy to clipboard operation
AutoDI copied to clipboard

Run-time exception with private nested classes

Open Keboo opened this issue 9 years ago • 7 comments

Found with the CI project.

Though the generated <AutoDI> is generated within the assembly there are still cases where we cannot invoke the constructor. In the 3.0.0 release it attempts to generate a factory method for it.

This ends up causing a MethodAccessException at run-time (and somewhat difficult to track down if you have a large number of factory methods).

In GenerateFactoryMethod we should only attempt to invoke constructors when the accessibility domain (language spec 3.5) of the constructor is public, internal, or protected internal (need to look up the CIL for these). If the accessibility domain of the constructor does not meet this criteria GenerateFactoryMethod should return null. This will end up logging a message to the user indicating that a valid constructor was not found.

This will also require several unit tests to test all of the various combinations. It should also not assume a single level of nesting. We should check everything up to the top-level type.

For anyone upgrading from 2.1.4 to 3.0.0 and encountering this issue you can exclude all nested classed by adding this to your AutoDI configuration.

<type name="Falcon\.UI\..*`" lifetime="None" />

Keboo avatar Oct 12 '17 22:10 Keboo

Hello Keboo,

Do you still need someone to work on this?

MarsMSJ avatar Oct 13 '17 03:10 MarsMSJ

I was actually planning on looking at it a little later tonight.

Keboo avatar Oct 13 '17 03:10 Keboo

Was going to ask where I could find this code. In particular, the anonymous delegate used to subscribe and unsubscribe is problematic. According to Microsoft they do not recommended using anonymous delegates to subscribe to events at all.

MarsMSJ avatar Oct 13 '17 03:10 MarsMSJ

The project it reference is actually a closed source project. I only copied in the code as a reference for myself. I suspect I know where the problem is located, but wanted to make sure I could reproduce it in a test first. And yes, I did agree with the anonymous delegates as event handler, though in this case it is a local function which gets handled a little differently by the compiler.

Keboo avatar Oct 13 '17 03:10 Keboo

Thanks for the link, I'll check it out.

MarsMSJ avatar Oct 13 '17 04:10 MarsMSJ

Note to self: This is proving to be more difficult than anticipated to reproduce. I have recreated the project with the same build configuration, and class structure and everything appears to be working. More investigation will be needed to understand the exact conditions which this occurs.

Keboo avatar Oct 13 '17 06:10 Keboo

More notes: Determined the source of the issue(s). In the 2.1.4 release the issue was caused by a rouge binding redirect putting the AutoDI dlls in an inconsistent state (I really have no idea how it was working at all....). In the 3.0.0 release the issue is caused by having a private nested class. In 2.1.4 AutoDI (incorrectly) ignored all nested classes. In 3.0.0 it finds all of them and uses them as potential classes for mapping. Even though the generated <AutoDI> it obviously cannot invoke the class's constructor (even though the constructor is public). We need to determine if we will actually be able to invoke the constructor in GenerateFactoryMethod.

To be more specific we should only attempt to invoke constructors when the accessibility domain (language spec 3.5) of the constructor is public, internal, or protected internal (need to look up the CIL for these). If the accessibility domain of the constructor does not meet this criteria GenerateFactoryMethod should return null.

Work around, declare the following in the FodyWeavers.xml. It will strip out all nested classes (not great, but at least is feature parity with 2.1.4)

<type name=".*\/.*" lifetime="None" />

I will update the description of this bug shortly.

Keboo avatar Oct 13 '17 22:10 Keboo