From 0789deb59f23e003ff3f2ba7cffa968afda1f367 Mon Sep 17 00:00:00 2001 From: christallire Date: Tue, 6 Oct 2026 19:08:16 +0900 Subject: [PATCH] fix(SimpleW): map module routes and the not found handler without expression 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. --- src/SimpleW/Router.cs | 3 ++- src/SimpleW/modules/ServerSentEventsModule.cs | 5 +++-- src/SimpleW/modules/StaticFilesModule.cs | 5 +++-- src/SimpleW/modules/WebSocketModule.cs | 5 +++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/SimpleW/Router.cs b/src/SimpleW/Router.cs index d94e74cc..59db759f 100644 --- a/src/SimpleW/Router.cs +++ b/src/SimpleW/Router.cs @@ -632,7 +632,8 @@ public void MapFallback(Delegate handler) { #region not found - private static readonly HttpRouteExecutor _notFoundExecutor = RouteExecutorFactory.Create(static (HttpSession s) => s.Response.Status(404).Text("Not Found").SendAsync()); + // a plain HttpRouteExecutor rather than RouteExecutorFactory.Create() : a Delegate handler is compiled from an expression tree, which fails under NativeAOT + private static readonly HttpRouteExecutor _notFoundExecutor = static (session, _) => session.Response.Status(404).Text("Not Found").SendAsync(); #endregion not found diff --git a/src/SimpleW/modules/ServerSentEventsModule.cs b/src/SimpleW/modules/ServerSentEventsModule.cs index fe53fc43..23a82652 100644 --- a/src/SimpleW/modules/ServerSentEventsModule.cs +++ b/src/SimpleW/modules/ServerSentEventsModule.cs @@ -127,10 +127,11 @@ public ServerSentEventsModule(ServerSentEventsOptions options) { /// /// public void Install(SimpleWServer server) { + // map an HttpRouteExecutor rather than a Delegate : a Delegate handler is compiled from an expression tree, which fails under NativeAOT // accept prefix - server.MapGet(_options.Prefix, (HttpSession session) => HandlerAsync(session)); + server.Router.Map("GET", _options.Prefix, (session, _) => HandlerAsync(session)); // accept prefix wildcard as fallback (but will enforce exact match) - server.MapGet(_options.PrefixWildCard, (HttpSession session) => HandlerAsync(session)); + server.Router.Map("GET", _options.PrefixWildCard, (session, _) => HandlerAsync(session)); _log.Info($"installed with prefix {_options.Prefix}"); } diff --git a/src/SimpleW/modules/StaticFilesModule.cs b/src/SimpleW/modules/StaticFilesModule.cs index 57997580..1cfaa7ba 100644 --- a/src/SimpleW/modules/StaticFilesModule.cs +++ b/src/SimpleW/modules/StaticFilesModule.cs @@ -219,8 +219,9 @@ public void Install(SimpleWServer server) { } // map GET + HEAD - server.MapGet(_options.PrefixWildCard, (HttpSession session) => HandlerAsync(session)); - server.Map("HEAD", _options.PrefixWildCard, (HttpSession session) => HandlerAsync(session)); + // map an HttpRouteExecutor rather than a Delegate : a Delegate handler is compiled from an expression tree, which fails under NativeAOT + server.Router.Map("GET", _options.PrefixWildCard, (session, _) => HandlerAsync(session)); + server.Router.Map("HEAD", _options.PrefixWildCard, (session, _) => HandlerAsync(session)); _log.Info($"installed with prefix {_options.Prefix}"); } diff --git a/src/SimpleW/modules/WebSocketModule.cs b/src/SimpleW/modules/WebSocketModule.cs index 59988558..d34c1703 100644 --- a/src/SimpleW/modules/WebSocketModule.cs +++ b/src/SimpleW/modules/WebSocketModule.cs @@ -191,10 +191,11 @@ public WebSocketModule(WebSocketOptions options) { /// /// public void Install(SimpleWServer server) { + // map an HttpRouteExecutor rather than a Delegate : a Delegate handler is compiled from an expression tree, which fails under NativeAOT // accept prefix - server.MapGet(_options.Prefix, (HttpSession session) => HandlerAsync(session)); + server.Router.Map("GET", _options.Prefix, (session, _) => HandlerAsync(session)); // accept prefix wildcard as fallback (but will enforce exact match) - server.MapGet(_options.PrefixWildCard, (HttpSession session) => HandlerAsync(session)); + server.Router.Map("GET", _options.PrefixWildCard, (session, _) => HandlerAsync(session)); _log.Info($"installed with prefix {_options.Prefix}"); }