创建依赖于 netstandard 包的 netcore 3.0 项目的 NuGet 包

Create a NuGet package of a netcore 3.0 project which is dependent upon netstandard packages

我的解决方案包含许多针对 netcore 3.0 的项目,其中一些项目参考了其他针对 netstandard 2.0.

的 nuget 包

我想为一个引用其他项目的项目创建一个 nuget 包。在我的 Azure DevOps 管道中,我有以下任务来构建 nupkg 文件

- task: NuGetCommand@2
  displayName: 'Create NuGet Package'
  inputs:
    command: 'pack'
    packagesToPack: 'Core.Hosting/Core.Hosting.nuspec'
    versioningScheme: 'off'
    includeReferencedProjects: true
    includeSymbols: true

随后是推送到 Azure Devops 中的工件的任务:

- task: NuGetCommand@2
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: 'reference-to-feed-is-removed'
  condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/develop'), eq('true', variables['PUSH_TO_NUGET']))

我的 nuspec 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<package >
  <metadata>
    <id>Core Hosting</id>
    <version>1.0.0-prerelease-5.0.0</version>
    <title>Core Hosting</title>
    <authors>Core Team</authors>
    <owners>My Company a/s</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <projectUrl>project-url-removed/projectUrl>
    <description>Core backend components to interact with various services in Azure.</description>
    <copyright>Copyright 2020</copyright>
    <dependencies>
      <group targetFramework=".NETCoreApp3.0">
        <dependency id="AspNetCore.Firebase.Authentication" version="2.0.1"/>
        <dependency id="Autofac" version="4.9.4"/>
        <dependency id="Autofac.Extensions.DependencyInjection" version="5.0.1"/>
        <dependency id="Autofac.Mef" version="4.1.0"/>
        <dependency id="LinqKit.Core" version="1.1.17"/>
        <dependency id="Microsoft.ApplicationInsights.AspNetCore" version="2.12.0"/>
        <dependency id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.12.0"/>
        <dependency id="Microsoft.AspNetCore.Authentication.JwtBearer" version="3.0.0"/>
        <dependency id="Microsoft.EntityFrameworkCore" version="3.1.1"/>
        <dependency id="Microsoft.EntityFrameworkCore.Cosmos" version="3.1.1"/>
        <dependency id="Microsoft.EntityFrameworkCore.Relational" version="3.1.1"/>
        <dependency id="Microsoft.Extensions.DependencyModel" version="3.1.1"/>
        <dependency id="Microsoft.Extensions.Options" version="3.1.1"/>
        <dependency id="Swashbuckle.AspNetCore" version="5.0.0"/>
        <dependency id="System.ComponentModel.Composition" version="4.6.0"/>
        <dependency id="System.Composition.AttributedModel" version="1.3.0"/>
        <dependency id="Microsoft.ApplicationInsights" version="2.12.0"/>
        <dependency id="Microsoft.AspNetCore.Mvc.Core" version="2.2.5"/>
        <dependency id="Microsoft.Azure.KeyVault" version="3.0.4"/>
        <dependency id="Microsoft.Azure.Services.AppAuthentication" version="1.3.1"/>
        <dependency id="System.Configuration.ConfigurationManager" version="4.6.0"/>
        <dependency id="FirebaseAdmin" version="1.9.1"/>
        <dependency id="Microsoft.OpenApi" version="1.1.4"/>
        <dependency id="Swashbuckle.AspNetCore.SwaggerGen" version="5.0.0"/>
        <dependency id="Microsoft.Extensions.Configuration.Abstractions" version="3.1.1"/>
        <dependency id="SendGrid" version="9.12.0"/>
        <dependency id="MassTransit.Azure.ServiceBus.Core" version="6.0.0-develop.2244"/>
        <dependency id="Microsoft.Azure.WebJobs" version="3.0.14"/>
        <dependency id="System.ComponentModel.Composition" version="4.6.0"/>
      </group>
    </dependencies>
  </metadata>
</package>

我想在我的其他 netcore 3.0 项目中使用这个 nuget 包,通过从 Nuget Package Manager 添加它可以在本地完美运行。一切都按预期构建和 运行 除了当我尝试 运行 这个项目的管道时:

trigger:
  branches:
    include:
      - "*"

pool:
  vmImage: 'ubuntu-latest'

variables:
  buildConfiguration: 'Release'
steps:

- task: UseDotNet@2
  displayName: 'Install dotnet 3.x SDK'
  inputs:
    packageType: sdk
    version: '3.x'
    includePreviewVersions: true
    installationPath: $(Agent.ToolsDirectory)/dotnet

- task: NuGetCommand@2
  displayName: 'Restore NuGet packages'
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'select'
    vstsFeed: 'feed-id-removed'

- script: dotnet test --filter FullyQualifiedName~UnitTests
  displayName: "Run Unit Tests"

- script: dotnet publish Example.Service.Host/Example.Service.Host.csproj --configuration Release -o 'output/publish' --runtime linux-x64 -f netcoreapp3.0
  displayName: "Build Example.Service"

- task: CopyFiles@2
  inputs:
    SourceFolder: ''
    Contents: 'Dockerfile'
    TargetFolder: 'output'

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: 'output'
    includeRootFolder: false
    archiveType: 'zip'
    archiveFile: 'Example.Service/$(Build.BuildId).zip'
    replaceExistingArchive: true

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: 'Example.Service/$(Build.BuildId).zip'
    ArtifactName: 'drop'

运行 我的管道中的 NuGet 恢复任务,我错误地抱怨像这样的兼容性问题:

Package AspNetCore.Firebase.Authentication 2.0.1 is not compatible with netcoreapp3.0 (.NETCoreApp,Version=v3.0). Package AspNetCore.Firebase.Authentication 2.0.1 supports: netstandard2.0

所有错误都是一样的,依赖包X不兼容netcoreapp3.0,因为它面向netstandard2.0。

我发现我很奇怪它在 Visual Studio 中构建和恢复包时在本地工作,但当我从管道 运行 时它不工作。我还认为以 netstandard2.0 为目标的库将与 netcoreapp3.0 apps

兼容

有什么方法可以让它在我的 Azure Devops 管道中运行,还是我搞砸了?

这个问题可能是由过时的 NuGet 代理版本引起的。您可以尝试使用 NuGet Install Tool 任务并将代理设置为 v5.x,然后再使用 NuGetCommand 恢复任务。

- task: NuGetToolInstaller@0
  displayName: 'Use NuGet 5.x'
  inputs:
    versionSpec: 5.x
    checkLatest: true

另一种可能的解决方法是使用 dotnet restore 而不是 nuget restore。

- task: DotNetCoreCLI@2
  inputs:
    command: restore
    projects: '**/*.csproj'
    feedsToUse: 'select'
    vstsFeed: 'azure feed'

详情可参考this thread