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

为何我的NuGet包会自动添加Castle.Core作为依赖项?

问题:Castle.Core.3.3.3为何会随自定义NuGet包自动安装?

我创建了一个仅包含单个类和接口的基础.NetStandard2.1类库,未添加任何依赖项或NuGet包。构建解决方案后提交至Azure DevOps,通过构建Pipeline编译类库、生成NuGet包并发布到Azure Artifacts。

随后创建.Net 8控制台应用,通过NuGet包管理器从Azure Artifacts加载该包时,选中状态显示无依赖项(符合预期),但安装时却会同时安装Castle.Core.3.3.3与我的NuGetTestlib.1.0.0。更新Pipeline的YAML配置添加版本控制后,新版本包不再包含Castle.Core引用,但仍不清楚该引用的来源。


类库代码

public class Calculator : ICalculator
{
    public int Compute(int number1, int number2, char operation)
    {
        switch (operation)
        {
           case '+':
               return number1 + number2;
               break;
           case '-':
               return number1 - number2;
               break;

           default:
               throw new Exception($"Operation '{operation}' is not supported.");
       }
    }
}

public interface ICalculator
{
    public int Compute(int number1, int number2, char operation);
}

类库项目文件

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

更新内容

原Pipeline YAML配置

- task: DotNetCoreCLI@2
  displayName: 'Pack $(ProjectName)'
  inputs:
    command: 'pack'
    arguments: '--configuration $(buildConfiguration)'
    packagesToPack: '$(ProjectPath)'
    nobuild: true
    versioningScheme: 'off'

- task: NuGetAuthenticate@1

- task: NuGetCommand@2
  displayName: 'Publish $(ProjectName)'
  inputs:
    command: push
    feedsToUse: 'select'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: 'MyProject/NugetSharedTest'
    versioningScheme: 'off'
    allowPackageConflicts: true

更新后的Pipeline YAML配置

- task: DotNetCoreCLI@2
  displayName: 'Pack $(ProjectName)'
  inputs:
    command: 'pack'
    arguments: '--configuration $(buildConfiguration)'
    packagesToPack: '$(ProjectPath)'
    nobuild: true
    versioningScheme: byBuildNumber

- task: NuGetAuthenticate@1

- task: NuGetCommand@2
  displayName: 'Publish $(ProjectName)'
  inputs:
    command: push
    feedsToUse: 'select'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: 'MyProject/NugetSharedTest'
    versioningScheme: byEnvVar
    versionEnvVar: BUILD_BUILDNUMBER
    allowPackageConflicts: false

可能的原因分析

  • 旧版本包的残留依赖:原Pipeline中allowPackageConflicts: true允许包冲突,且versioningScheme: 'off'未启用版本控制,多次构建后旧的包含错误依赖的包未被完全替换,导致安装时拉取到了带有Castle.Core依赖的旧版本包。
  • 构建环境污染:Azure DevOps代理机器可能残留了之前其他项目构建时的Castle.Core包,在无版本控制的打包流程中,错误将其纳入了自定义包的依赖项。
  • NuGet缓存问题:本地或Azure Artifacts的缓存中保留了带有错误依赖的旧包版本,安装时优先获取了缓存中的旧包而非新生成的干净包。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 02:59:57