[FEATURE REQ] Resolve Native AOT Warnings in Azure.Core
See original GitHub issueLibrary name
Azure.Core
Please describe the feature.
In .NET 8 our goal is to enable a subset of ASP.NET functionality to support publishing with Native AOT. For more information on this goal, see ASP.NET Core updates in .NET 8 Preview 3 - ASP.NET Core support for native AOT and Support publishing ASP.NET Core API apps with Native AOT. This work is being done with a focus on enhancing .NET for “Cloud Native” applications. One important aspect for cloud apps is being able to monitor and diagnose the deployed application. With this in mind, our intent is to enable Open Telemetry support for Native AOT (for Microsoft employees, see the internal work item). Getting the Azure.Monitor.OpenTelemetry.Exporter compatible with AOT is important so those Open Telemetry events can be exported to Azure Monitor.
Azure.Monitor.OpenTelemetry.Exporter
has a dependency on Azure.Core
and parts of Azure.Core
are not compatible with Native AOT. The best way to prepare a library for Native AOT is following the guidance in:
- https://learn.microsoft.com/dotnet/core/deploying/native-aot/
- https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming
Using that information, we should resolve the Native AOT warnings coming from Azure.Core, starting first with the code used by Azure.Monitor.OpenTelemetry.Exporter
. To get the Native AOT warnings for the Azure.Core library, you can follow these steps (taken from the above docs):
- Using the latest .NET 8 SDK - https://dotnet.microsoft.com/download/dotnet/8.0 (7.0 can be used, but 8.0 has more of the core System libraries annotated, so it is preferred).
dotnet publish
the following application:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<PublishAot>true</PublishAot>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.0.0-beta.10" />
<!-- Analyze the whole assembly -->
<TrimmerRootAssembly Include="Azure.Monitor.OpenTelemetry.Exporter" />
<!-- Update this dependency to its latest, which has all the annotations -->
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.0-*" />
</ItemGroup>
</Project>
System.Console.WriteLine("Hello World");
- Analyze the warnings that are emitted. We should resolve all the warnings coming from
Azure.Core
.
I have done the above with the current code, and here are the warnings I am seeing from Azure.Core
:
ILC : Trim analysis warning IL2080: Azure.Core.Pipeline.ActivityExtensions.ActivitySourceStartActivity(Object,String,Int32,DateTimeOffset,ICollection`1<KeyValuePair`2<String,Object>>,IList,String,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String,BindingFlags,Binder,Type[],ParameterModifier[])'. The field 'Azure.Core.Pipeline.ActivityExtensions.ActivitySourceType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2080: Azure.Core.Pipeline.ActivityExtensions.ActivitySourceStartActivity(Object,String,Int32,DateTimeOffset,ICollection`1<KeyValuePair`2<String,Object>>,IList,String,String): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String,BindingFlags)'. The field 'Azure.Core.Pipeline.ActivityExtensions.ActivityContextType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2077: Azure.Core.Pipeline.ActivityExtensions.ActivitySourceStartActivity(Object,String,Int32,DateTimeOffset,ICollection`1<KeyValuePair`2<String,Object>>,IList,String,String): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Activator.CreateInstance(Type)'. The field 'Azure.Core.Pipeline.ActivityExtensions.ActivityContextType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : AOT analysis warning IL3050: Azure.Core.Pipeline.ActivityExtensions.ActivitySourceStartActivity(Object,String,Int32,DateTimeOffset,ICollection`1<KeyValuePair`2<String,Object>>,IList,String,String): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2080: Azure.Core.Pipeline.ActivityExtensions.CreateActivityLink(String,String,ICollection`1<KeyValuePair`2<String,Object>>): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String,BindingFlags)'. The field 'Azure.Core.Pipeline.ActivityExtensions.ActivityContextType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2080: Azure.Core.Pipeline.ActivityExtensions.CreateActivityLink(String,String,ICollection`1<KeyValuePair`2<String,Object>>): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Type.GetConstructor(Type[])'. The field 'Azure.Core.Pipeline.ActivityExtensions.ActivityLinkType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2077: Azure.Core.Pipeline.ActivityExtensions.CreateActivitySource(String): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'System.Activator.CreateInstance(Type,Object[])'. The field 'Azure.Core.Pipeline.ActivityExtensions.ActivitySourceType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : AOT analysis warning IL3050: Azure.Core.Pipeline.ActivityExtensions.CreateLinkCollection(): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2080: Azure.Core.Pipeline.ActivityExtensions.CreateTagsCollection(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicParameterlessConstructor' in call to 'System.Type.GetConstructor(Type[])'. The field 'Azure.Core.Pipeline.ActivityExtensions.ActivityTagsCollectionType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2080: Azure.Core.Pipeline.ActivityExtensions.ActivitySourceHasListeners(Object): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String,BindingFlags)'. The field 'Azure.Core.Pipeline.ActivityExtensions.ActivitySourceType' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2026: Azure.Core.Pipeline.ClientDiagnostics.ExtractAzureErrorContent(String): Using member 'System.Text.Json.JsonSerializer.Deserialize<ClientDiagnostics.ErrorResponse>(String,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : AOT analysis warning IL3050: Azure.Core.Pipeline.ClientDiagnostics.ExtractAzureErrorContent(String): Using member 'System.Text.Json.JsonSerializer.Deserialize<ClientDiagnostics.ErrorResponse>(String,JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2075: Azure.Core.Pipeline.ClientDiagnostics.GetResourceProviderNamespace(Assembly): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperty(String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2026: Azure.Core.Pipeline.DiagnosticScope.ActivityAdapter.Start(): Using member 'System.Diagnostics.DiagnosticSource.Write(String,Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The type of object being written to DiagnosticSource cannot be discovered statically. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2026: Azure.Core.Pipeline.DiagnosticScope.ActivityAdapter.MarkFailed(Exception): Using member 'System.Diagnostics.DiagnosticSource.Write(String,Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The type of object being written to DiagnosticSource cannot be discovered statically. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2026: Azure.Core.Pipeline.DiagnosticScope.ActivityAdapter.Dispose(): Using member 'System.Diagnostics.DiagnosticSource.Write(String,Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The type of object being written to DiagnosticSource cannot be discovered statically. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2026: Azure.Core.Serialization.JsonObjectSerializer.SerializeToBinaryDataInternal(Object,Type): Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(Object,Type,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : AOT analysis warning IL3050: Azure.Core.Serialization.JsonObjectSerializer.SerializeToBinaryDataInternal(Object,Type): Using member 'System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(Object,Type,JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2075: Azure.Core.Pipeline.HttpPipelineSynchronousPolicy.HttpPipelineSynchronousPolicy(): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String,BindingFlags,Binder,Type[],ParameterModifier[])'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2026: Azure.Core.Diagnostics.AzureCoreEventSource.RequestRetrying(String,Int32,Double): Using member 'System.Diagnostics.Tracing.EventSource.WriteEvent(Int32,Object[])' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : Trim analysis warning IL2026: Azure.RequestFailedException.TryExtractErrorContent(Response,ResponseError&,IDictionary`2<String,String>&): Using member 'System.Text.Json.JsonSerializer.Deserialize<RequestFailedException.ErrorResponse>(String,JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
ILC : AOT analysis warning IL3050: Azure.RequestFailedException.TryExtractErrorContent(Response,ResponseError&,IDictionary`2<String,String>&): Using member 'System.Text.Json.JsonSerializer.Deserialize<RequestFailedException.ErrorResponse>(String,JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. [C:\Users\eerhardt\source\repos\AzureAotCompatibility\AzureAotCompatibility\AzureAotCompatibility.csproj]
See the above 2 documentation links for more information on these warnings, and how to address them. (Or reach out to me and I will gladly help as well.)
cc @KrzysztofCwalina @jsquire @annelo-msft @pallavit @tg-msft @Yun-Ting
Issue Analytics
- State:
- Created 5 months ago
- Comments:9 (9 by maintainers)
Top GitHub Comments
My first goal is to get Open Telemetry in an ASP.NET app working with no warnings. Getting the whole
Azure.Core
assembly annotated (for example marking the APIs that are inherently incompatible with [RequiresUnreferencedCode] or [RequiresDynamicCode as necessary]) would be the next step, but wouldn’t be blocking me. I don’t think it is necessarily a goal to get the whole Azure.Core assembly fully compatible with AOT. There can be APIs that are not trimming/AOT compatible, as long as they are annotated as such. If those APIs aren’t used, they will be trimmed and the user won’t see any warnings from them.The advantage of annotating incompatible APIs is that any callers of the incompatible APIs will get warnings where they call the incompatible API. This is preferred because callers know immediately what they are doing isn’t trim/AOT-compatible, and there is a custom message you can use to explain why it isn’t compatible.
When the incompatible code isn’t annotated, the dev won’t get warnings from their code (at build time). But instead, they will see the warnings at publish-time coming from within the Azure.Core library itself - which they can’t fix.
On a related note, we should also work on trimming: #24238. The whole assembly doesn’t need to be AOT-compatible if we properly annotate what won’t work (and people don’t use it).