You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 10+VS2026中IL2026/IL3050警告仅在dotnet format显示问题

升级到Visual Studio 2026 + .NET 10后,AOT项目IL裁剪/AOT警告在VS中不显示的问题

问题详情

  • 环境:Visual Studio 2026 + .NET 10,基于依赖注入的AOT项目
  • 异常现象:执行构建或发布操作时,IL2026、IL3050这类IL裁剪/AOT相关警告,既不在Visual Studio的构建输出中出现,也不显示在错误列表(Build + IntelliSense)里;但运行dotnet format命令时,这些警告能正常输出。
  • 历史对比:在.NET 8 + Visual Studio 2022环境下,相同代码的这类警告会在构建/发布过程及VS错误列表中正常显示。

相关代码片段:

LogManager.Setup().SetupExtensions(e => e.RegisterTarget<EventLogTarget>());
var config = new ConfigurationBuilder()
    .SetBasePath(AppContext.BaseDirectory)
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .Build() ??
        throw new InvalidOperationException("Cannot get configuration.");

var services = new ServiceCollection();
services.AddSingleton<IConfiguration>(config);
services.Configure<LicensingOptions>(config.GetSection(nameof(LicensingOptions)));
services.Configure<SettingsServiceOptions>(config.GetSection(nameof(SettingsServiceOptions)));

dotnet format输出的警告示例:

...\Program.cs(29,9): warning IL2026: Using member 'Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure<TOptions>(IServiceCollection, IConfiguration)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. TOptions's dependent types may have their members trimmed. Ensure all required members are preserved.
...\Program.cs(29,9): warning IL3050: Using member 'Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure<TOptions>(IServiceCollection, IConfiguration)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Binding strongly typed objects to configuration values may require generating dynamic code at runtime.

原因分析与验证方向

这大概率是Visual Studio 2026或.NET 10 SDK的兼容性bug,可能的触发点:

  • VS的构建警告过滤逻辑在新版本中发生变化,导致IL裁剪/AOT类警告被意外屏蔽
  • .NET 10 SDK的警告输出通道与VS的集成出现异常,而dotnet format直接读取SDK原生警告输出,因此不受影响

可以通过以下步骤验证:

  1. 调整VS项目警告设置:在项目属性的「生成」选项卡,将警告等级设为4,临时勾选「将警告视为错误」,重新构建看是否能触发警告显示
  2. 清理项目缓存:删除bin、obj目录后重新构建,排除缓存导致的异常
  3. 检查项目配置:查看.csproj文件中是否有<NoWarn>或<SuppressWarnings>节点,确认是否意外添加了IL2026、IL3050的抑制规则
  4. 查看官方反馈:核对.NET 10 SDK发布说明,或开发者社区中的类似问题反馈,确认是否为已知bug

关于忽略警告的说明

如果你的LicensingOptions和SettingsServiceOptions是自定义类,且已通过[DynamicDependency]、[Preserve]等属性确保所有必要成员被保留,可通过以下方式安全忽略警告:

  • 代码行局部抑制:
#pragma warning disable IL2026, IL3050
services.Configure<LicensingOptions>(config.GetSection(nameof(LicensingOptions)));
services.Configure<SettingsServiceOptions>(config.GetSection(nameof(SettingsServiceOptions)));
#pragma warning restore IL2026, IL3050
  • 项目全局抑制:在.csproj中添加:
<PropertyGroup>
  <NoWarn>$(NoWarn);IL2026;IL3050</NoWarn>
</PropertyGroup>

内容的提问来源于stack exchange,提问作者user31905611

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 00:45:07