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

开发MSBuild混淆插件遇版本兼容问题:Microsoft.Bcl.AsyncInterfaces冲突

MSBuild插件开发依赖版本冲突问题

我正在开发一款可在数秒内混淆输出文件的MSBuild插件,目前遇到了阻碍开发的版本冲突问题:部分依赖的NuGet包要求使用Microsoft.Bcl.AsyncInterfaces 7.0.0以及Microsoft.Extensions.Configuration、Microsoft.Extensions.Configuration.Abstractions、Microsoft.Extensions.Configuration.Json等Microsoft.Extensions系列7.0.0版本。但我无法降级这些NuGet包,原因是:

  • 降级后无法正常使用IOptions功能

核心代码实现

配置注册扩展方法

public static ServiceCollection AddConfigurations(this ServiceCollection source)
{
    var configuration = new ConfigurationBuilder()
        .AddJsonFile("protections.json", true, true)
        .Build();
    // 若使用Microsoft.Extensions.. 6.0.0,输出'0'
    // 使用Microsoft.Extensions.. 7.0.0时一切正常
    Console.WriteLine(configuration.GetSection("Protections").Get<List<ProtectionSetting>>().Count);
    source.AddOptions()
        .Configure<ProtectionSettings>(options => configuration.Bind(options));
    return source;
}

ProtectionSettings类

public class ProtectionSettings
{
    public List<ProtectionSetting> Protections { get; set; }
}

ProtectionSetting类

public class ProtectionSetting
{
    [JsonRequired] public string Name { get; set; }
    public bool Enabled { get; set; }
    [JsonIgnore] public bool Disabled => Enabled == false;
}

protections.json配置文件

{
  "Protections": [
    {
      "Name": "NameHere",
      "Enabled": false
    },
    {
      "Name": "NameHere2",
      "Enabled": true
    }
  ]
}

临时方案尝试

使用6.0.0版本的Microsoft.Extensions系列包时,会出现配置值读取异常(输出计数为0),同时MSBuild提示需要Microsoft.Bcl.AsyncInterfaces 6.0.0,这显然不是可行方案。另外我尝试通过程序集绑定重定向解决版本冲突,但没有效果:

App.config绑定重定向配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces"
                                  publicKeyToken="cc7b13ffcd2ddd51"
                                  culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="6.0.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 03:31:36