Support for trimming and AOT - #7929
Merged
Merged
Conversation
…an suppress it unconditionally.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The changes are cohesive, preserve trimming annotations where required, and add targeted tests/sample validation for the updated strict-mode and mutator precedence behavior.
Pull request overview
This PR tightens NServiceBus.Core’s trimming/trimmability posture and strict-mode behavior, and strengthens coverage around message mutator replacement precedence (including exercising those paths in the trimmed sample).
Changes:
- Mark
NServiceBus.Coreas trimmable and remove the trimming-warning approval test in favor of enforcing trimming correctness via build/analyzer settings. - Centralize/suppress trimming-analyzer warnings around intentional assembly scanning in routing/publisher sources.
- Ensure strict mode disables dynamic type loading, and fix message mutator precedence so the object setter clears any previously declared replacement type (with new tests + sample validation).
File summaries
| File | Description |
|---|---|
| src/TrimmedEndpoint/Program.cs | Extends the trimmed sample to exercise message mutator replacement APIs and validates replacements occurred. |
| src/NServiceBus.Core/Routing/NamespaceRouteSource.cs | Centralizes assembly type scanning behind a suppression helper to avoid analyzer noise while preserving RUC on construction. |
| src/NServiceBus.Core/Routing/MessageDrivenSubscriptions/NamespacePublisherSource.cs | Same assembly scanning centralization/suppression approach for namespace-based publisher mapping. |
| src/NServiceBus.Core/Routing/MessageDrivenSubscriptions/AssemblyPublisherSource.cs | Same assembly scanning centralization/suppression approach for assembly-based publisher mapping. |
| src/NServiceBus.Core/Routing/AssemblyRouteSource.cs | Same assembly scanning centralization/suppression approach for assembly-based routing. |
| src/NServiceBus.Core/NServiceBus.Core.csproj | Marks Core as <IsTrimmable>true</IsTrimmable>. |
| src/NServiceBus.Core/MessageMutators/MutateInstanceMessage/MutateOutgoingMessageContext.cs | Clears ReplacementMessageType when using the legacy object setter to ensure object overload precedence. |
| src/NServiceBus.Core/MessageMutators/MutateInstanceMessage/MutateIncomingMessageContext.cs | Clears ReplacementMessageType when using the legacy object setter to ensure object overload precedence. |
| src/NServiceBus.Core/EndpointCreator.cs | Fixes strict-mode semantics by ensuring dynamic type loading is disabled when strict mode is enabled. |
| src/NServiceBus.Core.Tests/TrimmedEndpointTests.cs | Updates test narrative to reflect trimming warnings being enforced via build/analyzer behavior. |
| src/NServiceBus.Core.Tests/MessageMutators/MutateInstanceMessage/MutateOutgoingMessageBehaviorTests.cs | Adds regression test ensuring object setter wins when used after typed replacement. |
| src/NServiceBus.Core.Tests/MessageMutators/MutateInstanceMessage/MutateIncomingMessageBehaviorTests.cs | Adds regression test ensuring object setter wins when used after typed replacement. |
| src/NServiceBus.Core.Tests/ApprovalFiles/TrimmabilityWarnings.ApproveTrimmabilityWarnings.approved.txt | Removes approval baseline file (test removed). |
| src/NServiceBus.Core.Tests/API/TrimmabilityWarnings.cs | Removes trimming-warnings approval test. |
Review details
- Files reviewed: 14/14 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…lso moved the delegate factory because it should only be used there and no longer be a util.
danielmarbach
force-pushed
the
small_fixes
branch
from
September 5, 2026 19:25
f06c4de to
1c723f5
Compare
…o requires dynamic code. The source generated path doesn't require that so suppress
danielmarbach
force-pushed
the
small_fixes
branch
from
September 5, 2026 21:27
95780d9 to
5966c2d
Compare
danielmarbach
commented
Sep 6, 2026
DavidBoike
reviewed
Sep 9, 2026
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
danielmarbach
force-pushed
the
small_fixes
branch
from
September 10, 2026 15:42
da99645 to
ecd8833
Compare
DavidBoike
approved these changes
Sep 10, 2026
andreasohlund
approved these changes
Sep 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request focuses on improving trimmability, AOT compliance, and dynamic type loading behavior in
NServiceBus.Core, as well as enhancing test coverage for message mutator APIs. The main changes include stricter enforcement of trimming and dynamic code (AOT) warnings, improved handling of dynamic type loading in strict mode, and the addition of new tests and sample coverage for message mutator scenarios.Trimmability and Trimming Warnings
TrimmabilityWarningsapproval test and its associated approval file, now enforcing trimming warnings directly in the build viaEnableTrimAnalyzerand<IsTrimmable>true</IsTrimmable>inNServiceBus.Core.csproj[1] [2] [3] [4].[RequiresUnreferencedCode]with[UnconditionalSuppressMessage]and centralized assembly scanning logic inScanAssemblyTypesmethods for route and publisher sources, because all the public entry points are already annotated with the corresponding RUC [1] [2] [3] [4] [5] [6].Dynamic Code Warnings and Interceptor Suppressions
[RequiresDynamicCode]alongside the existing[RequiresUnreferencedCode]on the assembly scanning-based registration paths —EndpointConfiguration.AddHandler<T>()andAddSaga<T>(), the reflection-basedMessageHandlerRegistryregistration methods,SagaMetadatacreation from scanned types, and the generic reflection helpers inMethodInfoExtensions— so NativeAOT builds surface IL3050 warnings whenever dynamic code is actually required. The scanned handler/saga discovery delegates inEndpointCreatorkeep their unconditional suppressions (justification constant renamed fromTrimmingSuppressJustificationtoSuppressJustification) because the assembly scanning component refuses to run when dynamic code is unavailable.AddSagaInterceptorSuppressorandAddHandlerInterceptorSuppressorto also suppress IL3050 at call sites replaced by the source-generated, statically typed interceptor (new suppression IDsNSBS0004andNSBS0005), since the intercepted path performs no dynamic code generation. Call sites that cannot be intercepted, such as sagas that cannot be statically parsed, keep both fallback warnings.Dynamic Type Loading and Strict Mode
EndpointCreator.ConfigureMessageTypesso that strict mode correctly disables dynamic type loading, ensuring that message metadata registration behaves as intended in strict mode.Message Mutator APIs and Tests
MutateIncomingMessageContextandMutateOutgoingMessageContextto clear any type declared by a previous replacement, ensuring correct precedence of the object overload [1] [2].Sample and Validation Improvements
TrimmedEndpointsample to exercise message mutator APIs and validate that mutator replacements are used as expected, including new checks for replaced message instances [1] [2] [3] [4].These changes collectively improve the maintainability, correctness, and trimmability of the codebase while ensuring that message mutation behaviors are robustly tested and validated.