[原创]aot 中Rd.xml 文件格式语法中文说明以及处理错误的实战案例

Rd.xml 文件格式

CoreRT 提前编译器通过编译应用程序入口点及其传递依赖项来发现要编译的方法和要生成的类型。如果应用程序使用反射,编译器可能会丢失类型。有关该问题的更多信息,请参阅AOT 模式中的反射。可以补充一个 rd.xml 文件来帮助 ILCompiler 找到应该分析的类型。该文件与 .NET Native 使用的 rd.xml 文件类似,但更受限制。

最小的 Rd.xml 配置

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="mscorlib" />
  </Application>
</Directives>

ILCompiler 支持 2 个顶级指令ApplicationLibrary. 现在它们都可以互换使用,只需定义实际装配配置发生的区域。您可以在指令中放置多个<Assembly>标签<Application>以单独配置每个程序集。

装配指令

有 3 种形式可以配置组件

  • 仅模块元数据;
  • 所有类型;
  • 模块元数据和选定类型。

模块元数据只需要<Assembly>带有程序集短名称的简单标记。

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="mscorlib" />
  </Application>
</Directives>

程序集中的所有类型都需要添加Dynamic带有 value 的属性Required All注意:这是此属性的唯一可用值。

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="mscorlib" Dynamic="Required All" />
  </Application>
</Directives>

请注意,如果您在程序集中有泛型类型,则生成的代码中不会出现特定的实例化,如果您需要包含一个,那么您应该使用嵌套<Type>标记包含这些实例化。

模块元数据和选定类型选项基于仅模块元数据模式,其中添加了<Type>标签<Assembly>

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="MonoGame.Framework">
      <Type Name="Microsoft.Xna.Framework.Content.ListReader`1[[System.Char,mscorlib]]" Dynamic="Required All" />
    </Assembly>
  </Application>
</Directives>

类型指令。

类型指令提供了一种指定所需类型的方法。开发人员在这里有两个选择:

  • 采取所有类型方法;
  • 选择应该植根的方法。

采用所有类型方法:

<Type Name="Microsoft.Xna.Framework.Content.ListReader`1[[System.Char,mscorlib]]" Dynamic="Required All" />

示例如何指定类型名

// just int
System.Int32
// string[]
System.String[]
// string[][]
System.String[][]
// string[,]
System.String[,]
// List<int>
System.Collections.Generic.List`1[[System.Int32,System.Private.CoreLib]]
// Dictionary<int, string>.KeyCollection - notice all the generic arguments go to the nested type
System.Collections.Generic.Dictionary`2+KeyCollection[[System.Int32,System.Private.CoreLib],[System.String,System.Private.CoreLib]]

请注意,将泛型类型放在此处可能没有意义,因为代码是通过泛型类型的特定实例化生成的。无效场景示例:

// List<T>
System.Collections.Generic.List`1

要选择应植根的方法,请添加嵌套<Method>标签。

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="System.Private.CoreLib">
      <Type Name="System.Collections.Generic.Dictionary`2[[System.Int32,System.Private.CoreLib],[System.String,System.Private.CoreLib]]">
        <Method Name="EnsureCapacity" />
      </Type>
    </Assembly>
  </Application>
</Directives>

<Parameter>或者,如果您只想要特定的重载,您可以指定可选标签。例如:

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="System.Private.CoreLib">
      <Type Name="System.Collections.Generic.Dictionary`2[[System.Int32,System.Private.CoreLib],[System.String,System.Private.CoreLib]]">
        <Method Name="EnsureCapacity">
          <Parameter Name="System.Int32, System.Private.CoreLib" />
        </Method>
      </Type>
    </Assembly>
  </Application>
</Directives>

或者如果你想实例化泛型方法,你可以通过<GenericArgument>.

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
    <Assembly Name="System.Private.CoreLib">
      <Type Name="System.Array">
        <Method Name="Empty">
          <GenericArgument Name="System.Int32, System.Private.CoreLib" />
        </Method>
      </Type>
    </Assembly>
  </Application>
</Directives>

请注意,方法通过它们的方法名称和参数来区分。返回值的类型未在方法签名中使用。

生根结构编组数据

Type指令还支持一个MarshalStructure属性。唯一受支持的MarshalStructure值为Required All。指定MarshalStructure="Required All"将确保预先生成结构编组数据结构。

这可以修复如下代码:

using System;
using System.Runtime.InteropServices;

Console.WriteLine(GetTypeSize(typeof(MyClass)));

// This is in a separate method since the compiler would be able to analyze `Marshal.SizeOf(typeof(MyClass))`,
// but since a method call is involved, the compiler will lose track of the specific type.
static int GetTypeSize(Type t) => Marshal.SizeOf(t);

[StructLayout(LayoutKind.Sequential)]
public class MyClass
{
    public string Field;
}

上面的代码会抛出“System.Runtime.InteropServices.MissingInteropDataException:MyClass 缺少结构编组数据。要启用结构编组数据,请将 MarshalStructure 指令添加到应用程序 rd.xml 文件。”。要修复此异常,可以使用以下 RD.XML:

<Directives>
  <Application>
    <Assembly Name="repro">
      <Type Name="MyClass" MarshalStructure="Required All" />
    </Assembly>
  </Application>
</Directives>

生根委托编组数据

与上面的结构编组数据类似,该Type指令也支持MarshalDelegate属性。唯一受支持的MarshalDelegate值为Required All。指定MarshalDelegate="Required All"将确保预生成委托编组数据结构。

<Directives>
  <Application>
    <Assembly Name="repro">
      <Type Name="MyDelegate" MarshalDelegate="Required All" />
    </Assembly>
  </Application>
</Directives>

MyDelegate上面的 XML 将强制为assembly中的委托类型生成委托编组数据repro

https://github.com/dotnet/runtimelab/issues/2037

https://github.com/dotnet/runtimelab/issues/702#issuecomment-1279622783

假设错误如下,

System.Reflection.MissingMetadataException: 'System.Text.Json.Serialization.Converters.IEnumerableOfTConverter<System.Collections.Generic.IEnumerable<Swashbuckle.AspNetCore.SwaggerUI.UrlDescriptor>,Swashbuckle.AspNetCore.SwaggerUI.UrlDescriptor>' is missing metadat

拆开理解

image.png

那么语法是:

xxx

实战2 错误

Connection id "0HMLEANFSAK03", Request id"0HMLEANFSAK03:00000004": An unhandled exception was thrownby the application.System.Reflection.MissingMetadataException: 'System.Text.Json.Serialization.Converters.EnumConverter<Swashbuckle.AspNetCore.SwaggerUI.ModelRendering>' is missing metadata. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=392859
提取错误类

System.Text.Json.Serialization.Converters.EnumConverter<Swashbuckle.AspNetCore.SwaggerUI.ModelRendering>

对应的是:<Type Name="System.Text.Json.Serialization.Converters.EnumConverter`1[[Swashbuckle.AspNetCore.SwaggerUI.UrlDescriptor,Swashbuckle.AspNetCore.SwaggerUI]]" Dynamic="Required All" />

aot文件生效的定义

<ItemGroup>
  <RdXmlFile Include="Properties\Default.rd.xml" />
 </ItemGroup>

补充

<Type Name="System.Text.Json.Serialization.Converters.IEnumerableOfTConverter`2[[System.Collections.Generic.IEnumerable`1[[Swashbuckle.AspNetCore.SwaggerUI.UrlDescriptor,Swashbuckle.AspNetCore.SwaggerUI]],mscorlib],[Swashbuckle.AspNetCore.SwaggerUI.UrlDescriptor,Swashbuckle.AspNetCore.SwaggerUI]]"  Dynamic="Required All" />
            <Type Name="System.Text.Json.Serialization.Converters.EnumConverter`1[[Swashbuckle.AspNetCore.SwaggerUI.ModelRendering,Swashbuckle.AspNetCore.SwaggerUI]]" Dynamic="Required All" />
            <Type Name="System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1[[Swashbuckle.AspNetCore.SwaggerUI.DocExpansion,Swashbuckle.AspNetCore.SwaggerUI]]" Dynamic="Required All" />
            <Type Name="System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1[[System.Nullable`1[[System.Int32,mscorlib]],mscorlib]]" Dynamic="Required All" />
            <Type Name="System.Text.Json.Serialization.Converters.NullableConverter`1[[System.Int32,mscorlib]]" Dynamic="Required All" />

2022-10-17

根据运行报错的信息直接复制进来加上[]然后指定 程序集就知道如何编写rd.xml了

辅助编写文档http://go.microsoft.com/fwlink/?LinkID=392859

.net7.0预览版r2配置aot

7.0 r2 支持如下这样配置 而老版本6.0不支持builder.Services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.AddContext<MyJsonContext>());

7.0需要删除<PackageReference Include="Microsoft.DotNet.ILCompiler;" Version="7.0.0-*" />
改用 属性启动。
加入如下字段:<PublishAot>true</PublishAot>

<PropertyGroup>
        <PublishAot>true</PublishAot>
        <TargetFramework>net7.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <GenerateDocumentationFile>True</GenerateDocumentationFile>
        <Platforms>AnyCPU;x64;ARM64;ARM32</Platforms>
        <PlatformTarget>AnyCPU</PlatformTarget>
        <ErrorReport>send</ErrorReport>
        <IsPackable>false</IsPackable>
        <GeneratePackageOnBuild>False
<PropertyGroup>

7.0的web core api似乎不支持http:*:端口号了,否则提示不支持的url,
7.0目前需要预览版开发工具才能切换,而且切换后 正式版本开发工具将无法打开,因为不兼容修改后的applaunch.jsonhttps://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/
兼容性https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/incompatibilities

优化体积https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options?pivots=dotnet-7-0
.net7的重大变化https://learn.microsoft.com/en-us/dotnet/core/compatibility/7.0?source=recommendations

image.png

| Assembly.CodeBase | 引发PlatformNotSupportedException。 |
| Assembly.EscapedCodeBase | 引发PlatformNotSupportedException。 |
| Assembly.GetFile | 抛出IOException。 |
| Assembly.GetFiles | 抛出IOException。 |
| Assembly.Location | 返回一个空字符串。 |
| AssemblyName.CodeBase | 退货null。 |
| AssemblyName.EscapedCodeBase | 退货null。 |
| Module.FullyQualifiedName | 返回值为 的字符串<Unknown>或引发异常。 |
| Marshal.GetHINSTANCE | 返回 -1。 |
| Module.Name | 返回值为 的字符串<Unknown>。 |

修复常见场景的建议:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值