When a C# project moved from an earlier version of Visual Studio to Visual Studio 2022, AOT (Ahead Of Time) publish starts to fail with the error message
RuntimeIdentifier is required for native compilation. Try running dotnet publish with the -r option value specified.
This is cuased by old settings in the project. To eliminate the problem
- Open the ~\.nuget\packages\microsoft.dotnet.ilcompiler\8.0.15\build directory in File Explorer
- Rename the file Microsoft.NETCore.Native.Publish.targets to Microsoft.NETCore.Native.Publish.targets.OLD
- At the bottom of the Microsoft.NETCore.Native.targets file delete the line
<Import Project="$(MSBuildThisFileDirectory)Microsoft.NETCore.Native.Publish.targets" Condition="'$(NativeCompilationDuringPublish)' != 'false'" />
- Right-click the project, select Publish
- On the Publish page click the Publish button