Support Delegate Factories when compiled in AOT mode
Problem Statement
Using delegate factories in Autofac when AOT mode is on causes exceptions because it emits JIT compiled methods. I was not sure whether to open this as a bug or not as I didn't know if it is a supported use case. At the very least, this documentation page https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html could be updated to state that AOT is not supported.
Desired Solution
Frankly, I'm not sure if its possible to easily support this use case, but perhaps it can be done by generated source code. The desired solution is that delegate factories would work in AOT mode.
Alternatives You've Considered
We could refactor our code base to manually resolve these dependencies but we loose a lot of flexibility.
Additional Context
This problem was discovered when deploying a iOS app in release mode
I think we have some reflection support for AOT but we can always use help adding more. I don't personally do any AOT work and we've had a bit of a dry spell for PRs - we'd love to get some help adding this support if you're willing to contribute.
We stumbled over this with our iOS project. The error doesn't appear if the UseInterpreter option is enabled, but this reduces runtime performance and so is undesirable in release builds.
To make this page more discoverable from web searches, here is the exact exception that is thrown:
Autofac.Core.DependencyResolutionException: An exception was thrown while activating λ:System.Func`N[<Parameters>].
---> System.ExecutionEngineException: Attempting to JIT compile method '(wrapper dynamic-method) <signature>' while running in aot-only mode. See https://learn.microsoft.com/xamarin/ios/internals/limitations for more information.
We were able to work around the issue by replacing the generic delegates we requested from Autofac with typed-out code where all the relevant instructions exist at compile time, e.g. _capturedContext.Resolve<Foo>(TypedParameter.From(bar)).
Note that parameterless factory methods like Func<Foo> already work with AOT.