@@ -14,7 +14,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1414 context . RegisterPostInitializationOutput ( c =>
1515 c . AddSource (
1616 "ClientInitialization.g.cs" ,
17- """
17+ $$ """
1818 // <auto-generated/>
1919 #nullable enable
2020 using System;
@@ -51,15 +51,21 @@ internal static partial IServiceCollection AddCabazureClient(
5151 . Where ( e => e != null )
5252 . Collect ( ) ;
5353
54+ var aotSupport = context . CompilationProvider
55+ . Select ( ( options , _ ) => options . GetTypeByMetadataName (
56+ TypeConstants . DynamicallyAccessedMembersAttribute ) is { } ) ;
57+
5458 context . RegisterSourceOutput (
55- endpoints ,
59+ endpoints . Combine ( aotSupport ) ,
5660 GenerateInitialization ) ;
5761 }
5862
5963 public static void GenerateInitialization (
60- SourceProductionContext context ,
61- ImmutableArray < EndpointReferenceDescriptor > endpoints )
64+ SourceProductionContext context ,
65+ ( ImmutableArray < EndpointReferenceDescriptor > , bool ) args )
6266 {
67+ var ( endpoints , aotSupport ) = args ;
68+
6369 var source = new StringBuilder ( ) ;
6470 source . AppendLine ( "// <auto-generated/>" ) ;
6571 source . AppendLine ( "#nullable enable" ) ;
@@ -68,6 +74,7 @@ public static void GenerateInitialization(
6874 . Select ( e => e . Namespace )
6975 . OfType < string > ( )
7076 . Append ( "System" )
77+ . Append ( "System.Diagnostics.CodeAnalysis" )
7178 . Append ( "System.Net.Http" )
7279 . Append ( "System.Text.Json" )
7380 . Append ( "System.Collections.Generic" )
@@ -90,12 +97,16 @@ public static void GenerateInitialization(
9097 source . AppendLine ( ) ;
9198 }
9299
100+ var aotAttribute = aotSupport
101+ ? "[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] "
102+ : string . Empty ;
103+
93104 source . AppendLine ( $$ """
94105 namespace Microsoft.Extensions.DependencyInjection
95106 {
96107 internal static partial class ClientInitialization
97108 {
98- internal static partial IServiceCollection AddCabazureClient<TOptions>(
109+ internal static partial IServiceCollection AddCabazureClient<{{ aotAttribute }} TOptions>(
99110 this IServiceCollection services,
100111 string clientName,
101112 Action<JsonSerializerOptions>? jsonOptions,
0 commit comments