【问题标题】:Package reference not restored with Condition未使用 Condition 恢复包引用
【发布时间】:2021-04-13 14:09:30
【问题描述】:

我有一个私有 NuGet 包,我只想在配置设置为“调试”以外的其他内容时使用它,否则我想使用我引用的 DLL。

解决办法是:

<Choose>
    <When Condition="'$(Configuration)' == 'Debug'">
        <ItemGroup>
            <Reference Include="Foo">
               <HintPath>My_DLL.dll</HintPath>
            </Reference>
        </ItemGroup>
    </When>
    <Otherwise>
        <ItemGroup>
            <PackageReference Include="Foo" Version="0.1" />
        </ItemGroup>
    </Otherwise>
</Choose>

在 Visual Studio 中,当我将配置从发布切换到调试时,反之亦然。

问题是当我让构建服务器恢复并构建它时。
dotnet restore -f /p:Configuration=Release 似乎不包含我的包引用,但是当我将这些相同的包添加到其他包所在的 &lt;ItemGroup&gt; 时,构建服务器会正​​确恢复私有 NuGet 包。

问题是,我怎样才能让构建(服务器)解析我的 NuGet,就像 Visual Studio 一样?

注意:与dotnet restore not using PackageReference condition不一样

【问题讨论】:

    标签: c# .net visual-studio azure-devops yaml


    【解决方案1】:

    不要被dotnet restore 弄糊涂了。它恢复项目的 (nuget) 依赖项和 (nuget) 工具。依赖项和工具都应该包含在 nuget 包中。基本上该命令使用 nuget.exe 来完成该过程。 对于 csproj,我会采用以下方法:

    <Project Sdk="Microsoft.NET.Sdk">
      
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup Condition=" '$(Configuration)' != 'Debug' ">
        <PackageReference Include="log4net" Version="2.0.12" />
      </ItemGroup>
      <ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
        <Reference Include="log4net">
          <HintPath>..\FileRefs\log4netlocal.dll</HintPath>
        </Reference>
      </ItemGroup>
    </Project>
    

    项目结构:

    CustomItemGroupSolution
    │   CustomItemGroup.sln
    │
    ├───CustomItemGroup
    │       CustomItemGroup.csproj
    │       CustomItemGroup.csproj.user
    │       Program.cs
    │
    └───FileRefs
            log4netlocal.dll
    

    对于构建,以下命令将执行正确的工作:

        dotnet build -c Release
        dotnet build -c Debug
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      相关资源
      最近更新 更多