netnr 2026-05-09

.csproj 项目配置文件

<PropertyGroup>
    <!-- 使用部分裁剪模式 -->
    <TrimMode>partial</TrimMode>
    
    <!-- JSON 序列化的反射支持 .NET8+ -->
    <JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
    
    <!-- MVC 模型元数据支持 .NET9+ -->
    <MvcEnhancedModelMetadataSupport>true</MvcEnhancedModelMetadataSupport>
</PropertyGroup>

<!-- 忽略裁剪 -->
<ItemGroup>
    <!-- SqlClient -->
    <TrimmerRootAssembly Include="System.Private.CoreLib" />

    <!-- MVC .NET9+ -->
    <TrimmerRootAssembly Include="Microsoft.AspNetCore.Mvc.Core" />
    <TrimmerRootAssembly Include="System.ComponentModel.TypeConverter" />
</ItemGroup>

实践描述

  • JsonSerializerIsReflectionEnabledByDefault .NET8+
  • 组件 Microsoft.Data.SqlClient 需要 System.Private.CoreLib
  • 另外根据下面这个 Issue 提到 MVC 不支持修剪,如果修剪功能运行正常,纯粹是意外惊喜。
  • ref: https://github.com/dotnet/aspnetcore/issues/58926
  • ref: https://aka.ms/aspnet/trimming
  • 附:在实践过程中根据错误日志按需增加 TrimmerRootAssembly 忽略的项
登录写评论