Skip to content

fix(SimpleW): map module routes and the not found handler without expression trees - #17

Closed
christallire wants to merge 1 commit into
stratdev3:masterfrom
christallire:fix/aot-safe-internal-routes
Closed

christallire wants to merge 1 commit into
stratdev3:masterfrom
christallire:fix/aot-safe-internal-routes

Conversation

@christallire

Copy link
Copy Markdown
Contributor

Problem

Under NativeAOT, a server that uses StaticFilesModule, WebSocketModule or ServerSentEventsModule fails to start. Without them, an unmatched request answers 500 instead of 404.

These modules map their handlers through MapGet/Map(Delegate), and Router builds its not found handler with RouteExecutorFactory.Create(Delegate). Create compiles an expression tree, which needs reflection metadata that NativeAOT trims. It throws while the module installs:

System.ArgumentException: Instance property 'Request' is not defined for type 'SimpleW.HttpSession' (Parameter 'propertyName')
   at System.Linq.Expressions.Expression.Property(Expression, String)
   at SimpleW.RouteExecutorFactory.Create(Delegate)
   at SimpleW.Router.Map(String, String, String, Delegate)

Fix

The three modules and the not found handler now map a plain HttpRouteExecutor, for example server.Router.Map("GET", path, (session, _) => HandlerAsync(session)). This runs the same handler without reflection. Under JIT, behaviour is unchanged: Map(Delegate) already compiled these handlers down to FromValueTask(handler(session)).

Verification

Existing tests (net10.0). 373 of 374 pass. The one failure, FileBrowserModuleTests.Upload_Direct_Should_Preserve_Directories_And_Replace_File, fails the same way on unpatched master (a file lock on Windows).

NativeAOT (win-x64). I used a small console that installs the three modules and sends four requests:

Request 26.1.0 under NativeAOT This branch under NativeAOT
startup ArgumentException (above) starts
GET /index.html (static) — 200
HEAD /index.html — 200
GET /ws without upgrade — 400
POST /missing (no route) — 404

The same console gives the results in the right-hand column under JIT, with and without this change.

Possible follow-ups (not in this PR)

  • MapFallback(HttpRouteExecutor): an AOT-safe fallback overload. Today only MapFallback(Delegate) exists.
  • Mark the reflection-based entry points [RequiresUnreferencedCode]/[RequiresDynamicCode], so that AOT users get a warning at the call site instead of a failure at runtime:
    • RouteExecutorFactory.Create(Delegate);
    • Map*(Delegate);
    • MapController/MapControllers;
    • SystemTextJsonEngine without a JsonSerializerContext.
  • SimpleWServer's defaults keep those reflection paths in a trimmed app even when they are never called. These are the default ControllerActionExecutorFactory (CreateControllerActionExecutor) and the default JsonEngine.

GitHub is a mirror, so feel free to pull the branch as you prefer.

…ression trees

StaticFilesModule, WebSocketModule and ServerSentEventsModule mapped their
handlers through MapGet/Map(Delegate), and Router built its not found handler
with RouteExecutorFactory.Create(Delegate). Both compile an expression tree from
reflection metadata. Under NativeAOT that metadata is trimmed:
Expression.Property(session, "Request") throws ArgumentException while the
module installs, so a server that uses one of these modules cannot start, and
an unmatched request answers 500 instead of 404.

They now map a plain HttpRouteExecutor, which runs the same handler without
reflection. Behaviour is unchanged under JIT.
@stratdev3

Copy link
Copy Markdown
Owner

Hi @christallire,

Thanks for the contribution and the detailed investigation !

I hadn't really planned for NativeAOT support in SimpleW yet, but these changes make sense.
I will review the details and see if any adjustments are needed before integrating them.
As you mentioned, other parts of the code would also need the same changes for AOT compatibilty (fallback handler, addons, etc.).

@stratdev3

stratdev3 commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

I pulled your commit and made the other changes you suggested, you can check the testing branch.

I've also published an alpha version 26.1.2-alpha.20261004-2289 which includes these changes.

@stratdev3 stratdev3 closed this Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants