Edit

Share via


IL3058: Referenced assembly is not annotated for AOT compatibility

Cause

A project has <VerifyReferenceAotCompatibility>true</VerifyReferenceAotCompatibility> set, and one or more referenced assemblies don't have the IsAotCompatible assembly metadata attribute set to true.

Rule description

When you publish with Native AOT using <PublishAot>true</PublishAot> or mark your project as AOT-compatible with <IsAotCompatible>true</IsAotCompatible>, you can optionally enable verification that all referenced assemblies are also annotated for AOT compatibility. This helps ensure that all dependencies in your project are annotated for AOT compatibility.

To enable this verification, set the VerifyReferenceAotCompatibility property to true in your project file:

<PropertyGroup>
  <PublishAot>true</PublishAot>
  <VerifyReferenceAotCompatibility>true</VerifyReferenceAotCompatibility>
</PropertyGroup>

When this property is enabled, the analyzer checks that all referenced assemblies have been built with <IsAotCompatible>true</IsAotCompatible>, which adds the assembly-level attribute [assembly: AssemblyMetadata("IsAotCompatible", "True")] to the assembly.

Example

// Assembly reference: MyLibrary.dll (built without <IsAotCompatible>true</IsAotCompatible>)

public class Program
{
    public static void Main()
    {
        var obj = new MyLibrary.SomeClass();
    }
}
warning IL3058: Referenced assembly 'MyLibrary' is not built with `<IsAotCompatible>true</IsAotCompatible>` and may not be compatible with AOT.

How to fix violations

You have several options to fix this warning:

  • Update the referenced library to be built with <IsAotCompatible>true</IsAotCompatible>. This is the preferred approach if you control the library source code. The IsAotCompatible property marks the assembly as compatible with Native AOT and enables AOT-specific analysis.
  • Disable the verification by setting <VerifyReferenceAotCompatibility>false</VerifyReferenceAotCompatibility> in your project file if you're confident that the library works correctly with Native AOT even without the attribute.

See also