基于Clean Architecture的.NET7项目创建NuGet包遇错求助
问题背景
我有一个采用Clean Architecture的解决方案,包含以下项目:
- Core
- 依赖Core的Application
- 依赖Application的Infrastructure
- 依赖Application和Infrastructure的Web
为创建NuGet包,我进入Web.csproj所在文件夹,在PowerShell中执行命令:
nuget pack Web/Web.csproj -IncludeReferencedProjects
打包过程看似正常,但将该NuGet包安装到其他项目时出现以下警告:
Warning NU1603 Web 1.0.0 depends on Infrastructure (>= 1.0.0) but Infrastructure 1.0.0 was not found. An approximate best match of Infrastructure 1.0.0.1 was resolved.
Warning NU1603 Web 1.0.0 depends on Application (>= 1.0.0) but Application 1.0.0 was not found. An approximate best match of Application 1.2.1 was resolved.
Warning NU1701 Package 'Infrastructure 1.0.0.1' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project.
所有项目均使用.NET7,需要解决这些警告问题。
当前项目配置
Web.csproj
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <IsPackable>true</IsPackable> <Version>1.3.2</Version> </PropertyGroup> <ItemGroup> <ProjectReference Include="..\Application\Application.csproj" /> <ProjectReference Include="..\Infrastructure\Infrastructure.csproj" /> </ItemGroup> </Project>
Application.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" /> <PackageReference Include="NSec.Cryptography" Version="22.4.0" /> <PackageReference Include="Paseto.Core" Version="1.0.7" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Core\Core.csproj" /> </ItemGroup> </Project>
Infrastructure.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Application\Application.csproj" /> </ItemGroup> </Project>
Core.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Base64-Url" Version="1.0.0" /> </ItemGroup> </Project>
问题分析
- 版本号不统一:仅Web项目指定了
<Version>属性,其他项目未配置,导致nuget pack自动生成随机版本号(如1.0.0.1),但Web包依赖的是1.0.0,版本不匹配触发NU1603警告。 - 目标框架元数据缺失:未配置打包属性的项目,NuGet无法正确识别.NET7目标框架,错误标记为.NET Framework兼容,引发NU1701警告。
- 依赖处理逻辑问题:
-IncludeReferencedProjects默认将引用项目作为独立NuGet依赖,而非合并到Web包中,若未提前发布这些依赖包,就会出现找不到指定版本的情况。
修复步骤
1. 统一所有项目的版本号
在Core、Application、Infrastructure项目的<PropertyGroup>中添加与Web一致的版本号:
<Version>1.3.2</Version>
确保所有项目版本统一,避免自动生成不一致的版本。
2. 配置打包相关属性
在Core、Application、Infrastructure的<PropertyGroup>中添加以下配置:
<IsPackable>true</IsPackable> <!-- 允许单独打包,若不想单独发布可设为false --> <PackageTargetFallback>$(PackageTargetFallback);net7.0</PackageTargetFallback> <!-- 明确目标框架兼容 -->
如果希望将所有依赖项目合并到Web包中(无需单独发布依赖包),修改Web项目的项目引用,添加<PrivateAssets>all</PrivateAssets>:
<ProjectReference Include="..\Application\Application.csproj"> <PrivateAssets>all</PrivateAssets> </ProjectReference> <ProjectReference Include="..\Infrastructure\Infrastructure.csproj"> <PrivateAssets>all</PrivateAssets> </ProjectReference>
此配置会将依赖项目的代码嵌入Web包,不再生成独立依赖项。
3. 使用dotnet pack替代nuget pack
dotnet pack更适配.NET 5+项目,执行以下命令打包:
# 若保留独立依赖包 dotnet pack Web/Web.csproj -c Release -o ./nupkgs -p:IncludeReferencedProjects=true # 若合并为单个包 dotnet pack Web/Web.csproj -c Release -o ./nupkgs -p:PackageAsSingleFile=true
4. 验证打包结果
使用NuGet Package Explorer打开生成的.nupkg文件,检查:
- 依赖项版本与配置一致
- 目标框架正确标记为net7.0
- 依赖项处理符合预期(合并或独立)
内容的提问来源于stack exchange,提问作者Szyszka947

