-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AOT] Add middleware reflection fallback #45890
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@eerhardt After making these changes locally, updating the assembly in the SDK folder, and republishing, I still see expressions related warnings.
I mentioned on the original issue that it's odd the warnings are coming from Or am I not testing correctly? |
I thought this was the issue. |
This doesn't fix the expressions AOT warnings, but IMO this change is still worthwhile. Invoking via raw reflection is better than interpreting compiled expressions. And this change is pretty modest, self-contained, and is on the critical path (called potentially multiple times per request). Source gen is the best solution, but this is a good improvement for existing libraries who call |
Yes this is fine. The source gen will also be usable in libraries. |
@@ -107,7 +109,9 @@ public static class UseMiddlewareExtensions | |||
return (RequestDelegate)invokeMethod.CreateDelegate(typeof(RequestDelegate), instance); | |||
} | |||
|
|||
var factory = Compile<object>(invokeMethod, parameters); | |||
var factory = RuntimeFeature.IsDynamicCodeSupported |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JamesNK can you make a change to this to use https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.runtimefeature.isdynamiccodecompiled?view=net-7.0 instead?
Addresses the warning part of #45528
Adds a reflection fallback to invoking middleware. The goal is to avoid native AOT warnings on publish.
There is also a small performance benefit to using reflection instead of an expression with AOT. Avoids allocating and running expression interpreter. The ideal solution to performance here is using source generation to invoke middleware. That is still to be done before #45528 is complete.