How to make dotnet publish command use private Nuget sources

Viewed 4043

I am using VS2019 (16.4) within a company walls. I changed the VS Nuget sources and my solution compiles and runs after several requests to our internal foundation to approve Nuget packages.

Now I am trying to use the dotnet publish command, but it fails miserably with bunch of errors such as: error NU1605: Detected package downgrade: System.Runtime.Extensions from 4.3.0 to 4.1.0. Reference the package directly from the project to select a different version..

I figured it is probably not cognizant of the private Nuget sources.....so I searched around and the solution seems to be to include a NuGet.Config file at the solution root:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="Company Artifactory" value="https://repo.comany.com/artifactory/api/nuget/comany-nuget" />
    <add key="Company Eval Artifactory" value="https://repo.comany.com/artifactory/api/nuget/comany-nuget-eval" />
  </packageSources>
</configuration>

This did not help. It seems that there is a new dotnet command that will allow adding sources via the CLI: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-add-source but unfortunately our version is is 3.1.100 and the new command is applicable to 3.1.200...yes...it will take an act congress to get the new version in our company.

Any help is appreciated.

1 Answers

Thanks @omajid for your help. Here is what works for me:

In each project root folder:

  • dotnet restore
  • dotnet build --configuration Release
  • dotnet publish --no-restore -r win-x64 -c Release -o c:\somwhere /p:PublishSingleFile=true /p:DebugType=None

The above requires that we add the runtime identifier to the project file:

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
    <AssemblyVersion>0.0.2.0</AssemblyVersion>
</PropertyGroup>

Unfortunately, restore with runtime identifier did not work (same errors as in my original problem) which would probably mean that a runtime identifier is not needed in the project file:
dotnet restore --runtime win-x64

My initial observation was incorrect! Nuget sources were NOT causing the restore problems in publish....it was the runtime identifier. I still do not know why the error was manifested the way it did ...but calling dotnet publish with --no-restore switch seems to have done it for me.

The problem remains as to why dotnet restore --runtime win-x64 fails.

Related