目标框架为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
相关产品推荐
相关产品推荐

