Repository navigation
Akka.Hosting: add missing trimming annotations for Native AOT - #8655
Merged
Aaronontheweb merged 2 commits intoSep 28, 2026
Merged
Conversation
AddAkka<T> forwards T into AddHostedService<T>, WithHealthCheck<T> forwards T into ActivatorUtilities.GetServiceOrCreateInstance<T>, and LoggerConfigBuilder.LogMessageFormatter reflects on its Type value with GetConstructor - none of the three carried the DynamicallyAccessedMembers(PublicConstructors) annotation those APIs require, so a Native AOT publish trimmed away the constructor reflection metadata and AddAkka failed at startup with "no suitable constructor". Propagate the same annotation through WithDefaultLogMessageFormatter<T>, which assigns into LogMessageFormatter. Grepped Akka.Hosting, Akka.Remote.Hosting, Akka.Cluster.Hosting and Akka.Persistence.Hosting for the same pattern (AddHostedService, AddSingleton<TService,TImpl>, ActivatorUtilities, Activator.CreateInstance<T>). The other generic type parameters found (ActorRegistry's TKey/TActor, Cluster.Hosting's per-entity TKey, Persistence.Hosting's TAdapter) are only ever used as typeof(T) dictionary keys or config values, not passed into a DynamicallyAccessedMembers-requiring API, so they need no change. akka.extensions / WithExtension<T> is out of scope here - PR akkadotnet#8649 covers it. Updated the Hosting API approval baseline for the new attributes.
…r row, shorter comments The formatter getter returned an unannotated field (IL2078 under the trim analyzer). The annotations are source- and binary-compatible, so they don't need a BREAKING_CHANGES row.
This was referenced Sep 30, 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.
Summary
A Native AOT prototype for Akka.Hosting found three call sites where a generic type parameter (or reflected
Type) is passed into an API that requires[DynamicallyAccessedMembers], without the matching annotation on the Akka.Hosting side. Under trimming/Native AOT this lets the linker remove constructor reflection metadata the callee actually needs.AddAkka<T>forwardsTintoIServiceCollection.AddHostedService<T>(), which requiresPublicConstructors. Without the annotation, a Native AOT publish removesAkkaHostedService's constructor reflection metadata andhost.StartAsync()throwsInvalidOperationException: A suitable constructor for type 'Akka.Hosting.AkkaHostedService' could not be located.AkkaConfigurationBuilder.WithHealthCheck<T>forwardsTintoActivatorUtilities.GetServiceOrCreateInstance<T>(), same requirement (PublicConstructors).LoggerConfigBuilder.LogMessageFormattersetter callsvalue.GetConstructor([]), which requiresPublicConstructorson theTypepassed in;WithDefaultLogMessageFormatter<T>()assignstypeof(T)into it, so the same annotation is propagated onto itsT.Grepped
Akka.Hosting,Akka.Remote.Hosting,Akka.Cluster.HostingandAkka.Persistence.Hostingfor the same shape (AddHostedService,AddSingleton<TService,TImpl>,ActivatorUtilities,Activator.CreateInstance<T>). The other generic parameters found -ActorRegistry'sTKey/TActor,Akka.Cluster.Hosting's per-entityTKeyonWithShardRegion/WithSingleton/WithClusterClient/etc., andAkka.Persistence.Hosting'sTAdapter- are only ever used astypeof(T)dictionary keys or config values, never passed into aDynamicallyAccessedMembers-requiring API, so none of them need a change.AkkaConfigurationBuilder.AddExtensions()'sType.GetType(trimmed)forakka.extensions/WithExtension<T>is a separate, already-tracked gap (#8649) and is intentionally left alone here.Updated the Hosting API approval baseline (
CoreAPISpec.ApproveHosting.DotNet.verified.txt) for the new attributes. NoBREAKING_CHANGES_V1.6.mdrow: the annotations are source- and binary-compatible and only show up asIL2091for callers who run the trim analyzer.The formatter's backing field carries the same annotation, so its getter doesn't raise
IL2078. WithEnableTrimAnalyzer=true, Akka.Hosting's only remaining warning is theakka.extensionsIL2057that #8649 removes.Verification
dotnet build -warnaserroronAkka.Hostingand all five Hosting test projects: clean, 0 warnings.Akka.Hosting.Tests(140),Akka.Remote.Hosting.Tests(16),Akka.Cluster.Hosting.Tests(34),Akka.Persistence.Hosting.Tests(24),Akka.Hosting.TestKit.Xunit2.Tests(9),Akka.Hosting.TestKit.Tests(306) - all passing.Akka.API.TestsHosting-related approvals (ApproveHosting,ApproveRemoteHosting,ApproveClusterHosting,ApprovePersistenceHosting,ApproveHostingTestKit): all passing against the updated baseline.Akka/Akka.Hosting/Akka.DependencyInjectionviaProjectReference, callingAddAkka,WithHealthCheck<T>and the obsoleteLogMessageFormattersetter, and published it Native AOT (dotnet publish -r linux-x64 -c Release -p:PublishAot=true -p:CppCompilerAndLinker=gcc -p:LinkerFlavor=bfd):IL2091x2,IL2070x1 from the three sites above); running the binary throwsSystem.InvalidOperationException: A suitable constructor for type 'Akka.Hosting.AkkaHostedService' could not be located.out ofhost.StartAsync()- the exact reported crash.akka.extensionsone). The app gets pastAddHostedService/DI activation and intoActorSystemcreation, where it now fails on a separate, already-known, unrelated issue:Akka.Streams.Serialization.StreamRefSerializeris not yet in the built-in serializer table used whenAkka.DynamicTypeLoadingis off - a gap in Akka.Streams' AOT support, not something this PR touches.akka.extensionsgap).Test plan
dotnet build -warnaserroronAkka.Hostingand all Hosting test projectsAkka.API.TestsHosting approvals pass against updated baseline