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

目标框架为net7.0时,System.Net.Http为何显示版本4.3.0?

核心疑问:目标框架设为net7.0时,System.Net.Http为何显示早期版本4.3.0?

我正在梳理.NET SDK的依赖解析逻辑,目前存在认知盲区。我的项目配置如下,目标框架明确设置为net7.0:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <RuntimeIdentifier>osx-x64</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NSubstitute" Version="4.3.0" />
  </ItemGroup>
</Project>

现象细节

执行dotnet restore后,查看project.assets.json的依赖图,发现依赖链中出现了早期版本的系统库:

{
  "version": 3,
  "targets": {
    "net7.0": {
      "Castle.Core/4.4.1": {
        "type": "package",
        "dependencies": {
          "NETStandard.Library": "1.6.1",
          // ... 省略其他内容
        }
      },
      "NETStandard.Library/1.6.1": {
        "type": "package",
        "dependencies": {
          // ... 省略其他内容
          "System.Net.Http": "4.3.0",
          // ... 省略其他内容
        }
      }
    }
  }
}

即使以自包含方式执行dotnet publish,生成的deps.json中仍然显示System.Net.Http的版本为4.3.0,但实际对应的程序集版本是7.0.0:

{
  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v7.0/osx-x64",
    "signature": ""
  },
  "compilationOptions": {},
  "targets": {
    ".NETCoreApp,Version=v7.0": {},
    ".NETCoreApp,Version=v7.0/osx-x64": {
      "IfDotNoIssue/1.0.0": {
        "dependencies": {
          "NSubstitute": "4.3.0",
          "runtimepack.Microsoft.NETCore.App.Runtime.osx-x64": "7.0.5"
        }
      },
      "runtimepack.Microsoft.NETCore.App.Runtime.osx-x64/7.0.5": {
        "runtime": {
          // ... 省略其他内容
          "System.Net.Http.dll": {
            "assemblyVersion": "7.0.0.0",
            "fileVersion": "7.0.523.17405"
          }
        }
      },
      "NETStandard.Library/1.6.1": {
        "dependencies": {
          // ... 省略其他内容
          "System.Net.Http": "4.3.0",
          // ... 省略其他内容
        }
      }
    }
  }
}

我的困惑

我原本理解NuGet遵循最低适用版本原则,针对net7.0目标框架应该自动锁定到7.0.0版本的系统依赖,但实际情况并非如此。我遗漏了哪些关键逻辑?为什么依赖文件中显示的版本和实际运行的程序集版本不一致?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 13:53:22