question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 5 months ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
eerhardtcommented, Apr 18, 2023

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.

1reaction
heathscommented, Apr 18, 2023

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to AOT warnings - .NET
Ideally, all applications that use native AOT should have no AOT warnings. If there are any AOT warnings, ensure there are no behavior...
Read more >
ASP.NET Core support for native AOT
NET Core features are compatible with native AOT. ... An app that issues AOT warnings during publishing is not guaranteed to work correctly....
Read more >
What's new in ASP.NET Core 8.0
This section describes new features for minimal APIs. See also the section on native AOT for more information relevant to minimal APIs.
Read more >
Native AOT deployment overview - .NET
Learn what native AOT deployments are and why you should consider using it as part of the publishing your app with .NET 7...
Read more >
Prepare .NET libraries for trimming
There are two ways to find trim warnings in your library: Enable project-specific trimming using the IsTrimmable property.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found