将两个项目打包为NuGet包时出现引用缺失问题
问题描述
我有一个使用Azure队列的MailService包,已将队列相关逻辑放在单独的Testing.Storage.Queues项目中。使用以下Azure Pipeline任务将两个项目打包为一个NuGet包:
- task: NuGetCommand@2 displayName: "Pack MailService" inputs: command: 'pack' packagesToPack: '**/Testing.MailService.csproj' packDestination: '$(Build.ArtifactStagingDirectory)' versioningScheme: 'off' allowPackageConflicts: true includeReferencedProjects: true
生成的包在dotPeek中显示MailService引用了队列项目,但在其他解决方案中使用该包时,出现Azure.Storage.Queues未找到的异常,而该引用确实存在于被依赖的Storage.Queues项目中。
两个项目的.csproj内容如下:
Testing.MailService.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <Version>1.0.0</Version> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" /> <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="7.0.0" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Testing.Storage.Queues\Testing.Storage.Queues.csproj" /> </ItemGroup> </Project>
Testing.Storage.Queues.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Azure.Storage.Queues" Version="12.13.0" /> <PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" /> <PackageReference Include="Testing.Utilities.OperationResult" Version="1.4.6" /> </ItemGroup> </Project>
尝试相关解决方案后,Azure Pipeline出现版本冲突警告:
C:\Program Files\dotnet\sdk\7.0.201\Microsoft.Common.CurrentVersion.targets(2352,5): warning MSB3277: Found conflicts between different versions of "Azure.Core" that could not be resolved. [D:\a\1\s\src\Infrastructure\Testing.Functions\Testing.Functions.csproj] C:\Program Files\dotnet\sdk\7.0.201\Microsoft.Common.CurrentVersion.targets(2352,5): warning MSB3277: There was a conflict between "Azure.Core, Version=1.24.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8" and "Azure.Core, Version=1.28.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8". [D:\a\1\s\src\Infrastructure\Testing.Functions\Testing.Functions.csproj] C:\Program Files\dotnet\sdk\7.0.201\Microsoft.Common.CurrentVersion.targets(2352,5): warning MSB3277: C:\Users\VssAdministrator\.nuget\packages\azure.core\1.24.0\lib\net5.0\Azure.Core.dll [D:\a\1\s\src\Infrastructure\Testing.Functions\Testing.Functions.csproj]
根本原因
- NuGet打包依赖缺失:使用
includeReferencedProjects: true时,NuGet只会将引用项目的程序集嵌入到最终包中,但不会自动将引用项目的NuGet依赖(如Azure.Storage.Queues)添加为最终包的依赖项声明。因此,当其他项目安装这个NuGet包时,不会自动拉取Azure.Storage.Queues,导致运行时找不到相关程序集。 - 版本冲突:尝试手动添加依赖后出现的
Azure.Core版本冲突,是因为不同NuGet包对Azure.Core的版本要求不一致(比如Azure.Storage.Queues 12.13.0依赖较高版本的Azure.Core,而其他依赖可能要求较低版本),而NuGet的依赖解析无法自动调和这些差异。
解决方案
方法一:正确配置打包以包含依赖项
不需要手动添加依赖,而是通过修改项目配置让NuGet自动将引用项目的依赖传递到最终包中:
- 在
Testing.MailService.csproj的项目引用中明确依赖传递(默认已开启,可显式确认):<ProjectReference Include="..\Testing.Storage.Queues\Testing.Storage.Queues.csproj"> <Private>True</Private> </ProjectReference> - 修改Azure Pipeline的NuGet打包任务,添加还原步骤确保依赖被正确解析:
- task: NuGetCommand@2 displayName: "Pack MailService" inputs: command: 'pack' packagesToPack: '**/Testing.MailService.csproj' packDestination: '$(Build.ArtifactStagingDirectory)' versioningScheme: 'off' allowPackageConflicts: true includeReferencedProjects: true restoreSolution: '$(Solution)' # 替换为你的解决方案路径,确保依赖还原 - 或者直接在
Testing.MailService.csproj中显式添加Azure.Storage.Queues的包引用(推荐,避免依赖传递的不确定性):<PackageReference Include="Azure.Storage.Queues" Version="12.13.0" />
方法二:解决Azure.Core版本冲突
如果出现版本冲突警告,可以通过以下方式强制统一版本:
- 在解决方案的
Directory.Packages.props文件中(无则新建)添加全局版本锁定:<Project> <PropertyGroup> <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> </PropertyGroup> <ItemGroup> <PackageVersion Include="Azure.Core" Version="1.28.0" /> <!-- 使用冲突中较高的版本,兼容性更好 --> </ItemGroup> </Project> - 或者在
Testing.MailService.csproj中直接指定Azure.Core的版本:<ItemGroup> <PackageReference Include="Azure.Core" Version="1.28.0" /> </ItemGroup> - 清理本地NuGet缓存并重新构建:
dotnet nuget locals all --clear dotnet restore
内容的提问来源于stack exchange,提问作者user113000
相关产品推荐
相关产品推荐

