Warm tip: This article is reproduced from stackoverflow.com, please click
.net-core .net-standard azure-devops c# nuget

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

发布于 2020-04-15 09:50:18

My solution consist of many projects targeting netcore 3.0, some of these projects have reference to other nuget packages which targets netstandard 2.0.

I want to create one nuget package of a project referencing other projects. In my Azure DevOps pipeline I have the following task to build the nupkg file

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

Followed by a task to push to artifacts in 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']))

My nuspec file looks like this:

<?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>

I want to use this nuget package in my other netcore 3.0 project, which works perfectly locally by adding it from the Nuget Package Manager. Everything build and run as expected except when I try to run the pipeline for this project:

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'

Running the NuGet restore task in my pipeline, I errors complaining about compatability issues like this one:

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

All of errors are the same, the dependent package X is not compatible with netcoreapp3.0 as it targets netstandard2.0.

I find i quite strange that it works locally when building and restoring packages in Visual Studio, but not when I run it from the pipeline. I also thought that libraries which targets netstandard2.0 would be compatible with netcoreapp3.0 apps

Is there a way I can get this to work in my Azure Devops pipeline or have I screwed up big time?

Questioner
Tobias Moe Thorstensen
Viewed
112
Levi Lu-MSFT 2020-02-02 23:32

The issue is probably caused by an obsolete version of NuGet agent. You can try using NuGet Install Tool task and setup the agent to the v5.x, before using NuGetCommand restore task.

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

Another possible fix is to use dotnet restore instead of nuget restore.

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

You can refer to this thread for more details.