0

我们正在从 NetFramework 项目切换到具有多个 TargetFrameworks 的 NetCore 项目。如果 TargetFramework 包含为 net40 或 net35 编写的代码,则这需要 Directory.Build.Props 文件。Directory.Build.Props 中引用的 NuGet 包来自 NuGet.org。

由于 NuGetCommand 是为 NetFramework 构建的,我们正尝试在 yaml 管道中从使用 NuGetCommand 切换到 DotNetCoreCLI。但是,DotNetCoreCLI 'restore' 似乎没有在 Directory.Build.props 中包含 NuGet 包。恢复任务有效,但构建任务失败并出现以下错误:

  Determining projects to restore...
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net20.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net20/1.0.0/microsoft.netframework.referenceassemblies.net20.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net40.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net40/1.0.0/microsoft.netframework.referenceassemblies.net40.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net40.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net40/1.0.0/microsoft.netframework.referenceassemblies.net40.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net20.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net20/1.0.0/microsoft.netframework.referenceassemblies.net20.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
##[error]C:\Program Files\dotnet\sdk\5.0.202\NuGet.targets(131,5): Error : Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net40.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net40/1.0.0/microsoft.netframework.referenceassemblies.net40.1.0.0.nupkg'.
Response status code does not indicate success: 401 (Unauthorized).

NuGet 命令:

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'config'
    nugetConfigPath: 'NuGet.config'

DotNetCoreCLI

- task: DotNetCoreCLI@2
  displayName: DotNetCoreCLI NuGet restore
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    feedsToUse: 'config'
    nugetConfigPath: 'NuGet.config'

NuGet.Config 包括以下内容

  <packageSources>
    ...
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json"/>
  </packageSources>

目录.Build.props

<Project>
  <PropertyGroup>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
    <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
    <PackageReference Include="jnm2.ReferenceAssemblies.net35" Version="1.0.1" PrivateAssets="all" />
  </ItemGroup>
</Project>
4

1 回答 1

0

--no-restore在您的 dotnet 构建任务中使用选项来禁用隐式还原。

这是文档:dotnet restore 命令在某些显式还原有意义的场景中仍然有用,例如 Azure DevOps Services 中的持续集成构建或需要显式控制何时发生还原的构建系统。

此外,dotnet restore内部使用NuGet.exe与 .NET Core SDK 一起打包的版本。dotnet restore只能恢复 .NET Core 项目.csproj文件中指定的包。如果您的解决方案中还有 Microsoft .NET Framework 项目或用于package.json指定依赖项,则还必须使用 NuGet 任务来恢复这些依赖项。请参阅本文档

于 2021-04-20T06:03:12.577 回答