Skip to content

[FEATURE REQ] Resolve Native AOT Warnings in Azure.Core #35572

Closed
@eerhardt

Description

@eerhardt

Library 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:

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):

  1. 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).
  2. 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");
  1. 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

Metadata

Metadata

Assignees

Labels

Azure.CoreClientThis issue is related to a non-management packagefeature-requestThis issue requires a new behavior in the product in order be resolved.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions