Skip to content

Support for trimming and AOT - #7929

Merged
danielmarbach merged 18 commits into
masterfrom
small_fixes
Sep 11, 2026
Merged

Support for trimming and AOT#7929
danielmarbach merged 18 commits into
masterfrom
small_fixes

Conversation

@danielmarbach

@danielmarbach danielmarbach commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

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

  • Removed the TrimmabilityWarnings approval test and its associated approval file, now enforcing trimming warnings directly in the build via EnableTrimAnalyzer and <IsTrimmable>true</IsTrimmable> in NServiceBus.Core.csproj [1] [2] [3] [4].
  • Replaced [RequiresUnreferencedCode] with [UnconditionalSuppressMessage] and centralized assembly scanning logic in ScanAssemblyTypes methods 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

  • Added [RequiresDynamicCode] alongside the existing [RequiresUnreferencedCode] on the assembly scanning-based registration paths — EndpointConfiguration.AddHandler<T>() and AddSaga<T>(), the reflection-based MessageHandlerRegistry registration methods, SagaMetadata creation from scanned types, and the generic reflection helpers in MethodInfoExtensions — so NativeAOT builds surface IL3050 warnings whenever dynamic code is actually required. The scanned handler/saga discovery delegates in EndpointCreator keep their unconditional suppressions (justification constant renamed from TrimmingSuppressJustification to SuppressJustification) because the assembly scanning component refuses to run when dynamic code is unavailable.
  • Extended AddSagaInterceptorSuppressor and AddHandlerInterceptorSuppressor to also suppress IL3050 at call sites replaced by the source-generated, statically typed interceptor (new suppression IDs NSBS0004 and NSBS0005), 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.
  • Extended the mock trimming analyzer and added suppressor tests to verify that IL3050 is suppressed for intercepted calls and preserved for unrelated ones.

Dynamic Type Loading and Strict Mode

  • Fixed the logic in EndpointCreator.ConfigureMessageTypes so that strict mode correctly disables dynamic type loading, ensuring that message metadata registration behaves as intended in strict mode.

Message Mutator APIs and Tests

  • Added logic to the object setter in MutateIncomingMessageContext and MutateOutgoingMessageContext to clear any type declared by a previous replacement, ensuring correct precedence of the object overload [1] [2].
  • Added new tests to verify that when both typed and object setters are used, the object setter takes precedence, for both incoming and outgoing message mutators [1] [2].

Sample and Validation Improvements

  • Enhanced the TrimmedEndpoint sample 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.

@danielmarbach danielmarbach changed the title Small fixes Support for trimmable Sep 5, 2026
@danielmarbach danielmarbach added this to the 10.3.0 milestone Sep 5, 2026
@danielmarbach
danielmarbach marked this pull request as ready for review September 5, 2026 18:55
@danielmarbach
danielmarbach requested a lite review from Copilot September 5, 2026 18:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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.Core as 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.
Comment thread src/NServiceBus.Core/NServiceBus.Core.csproj Outdated
Comment thread src/NServiceBus.Core/Hosting/KeyedServices/KeyedServiceProviderAdapter.cs Outdated
Comment thread src/NServiceBus.Core/Sagas/SagaMetadata.cs Outdated
Comment thread src/NServiceBus.Core/NServiceBus.Core.csproj Outdated
@danielmarbach

This comment was marked as resolved.

@DavidBoike

This comment was marked as resolved.

@danielmarbach danielmarbach changed the title Support for trimmable Support for trimming and AOT Sep 10, 2026
@danielmarbach
danielmarbach merged commit 65ebb29 into master Sep 11, 2026
4 checks passed
@danielmarbach
danielmarbach deleted the small_fixes branch September 11, 2026 05:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants