dotnet publish does not pull NuGet packages - asp.net

I am trying to automate deployment of an ASP.NET WebAPI on a Linux server using the following command:
dotnet publish --configuration Release
However, when adding a new NuGet package to the solution, and then trying to run the dotnet publish command, I get an error because the compiler does not know the new package. Is there a way to tell the dotnet command to pull all NuGet packages ? (I'm kind of looking for an equivalent for pip install -r requirements.txt in python).
For information, I add the NuGet packages via VisualStudio without compiling the solution.
Edit : it seems like, unless I build the solution in VisualStudio, just adding a NuGet packet will only add the packet name and version in the file projectname.csproj.nuget.dgspec.json, but will not add the PackageReference projectname.csproj file, hince the not pulling new packets issue.

I assume you are using some CI/CD pipeline which could publish your web application somewhere.
Feels like you are missing steps before publish:
# Restore (restores nuget packages)
run: dotnet restore
# Build
run: dotnet build --configuration Release --no-restore
# Test (if you have tests in project)
run: dotnet test --no-restore --verbosity normal
# Publish
run: dotnet publish --no-restore --no-build --framework netcoreapp3.1
May be this link may be helpful: github .net CI/CD

Related

Jenkins Dotnet Project Publishing Issue

I am trying to publish my dotnet project-api from jenkins via powershell. The problem is when I publish it from powershell it's missing around 30 files and I can't reach api. However when I publish it from visual studio manually (or from visual studio package manager console with commands) it works perfectly. What might be causing this issue?
Here are the commands that I am running to publish api ;
dotnet restore
dotnet build --configuration release
dotnet publish -c release --output "path to the publish file"
I have also tried with this to publish command yet it didn'T work;
dotnet publish dotnet publish -c Release --self-contained -r win10-x64
I have been trying to figure this issue for a long time but I couldn't find a way. I would highly appreciate any help. Thanks!!
You should try following 2 Execute Windows batch command
Command
"C:\Program Files\dotnet\dotnet.exe" restore yourProjectSLN file
[E.g:yourProjectSLN = git/FolderName/ProjectName.sln , it should be same as jenkins configuration]
Command
dotnet publish yourProject_csproj_file_location -c:Release
[E.g:yourProject_csproj_file_location = git\FolderName\ProjectName.csproj]

Azure Devops BUild scripts Are Restore Build and Test Required

In Azure Devops for a .Net core application.
I have three steps
dotnet restore
dotnet build
dotnet test
But if I simply run dotnet test that forces a restore and build. Is there any reason to have the first two steps?
You can use them as follows:
dotnet restore
dotnet build --no-restore
dotnet test --no-build
In this way, you will speed up your build as it can use result of the previous command.
This is default behavior so you don't need to always run all commands to run dotent test for instance. It is convinient and still possible to opt-out from thah behavior.

Install & run custom dotnet tool on azure devops release

I'm trying to build a Release pipeline that is triggered by a new version of a published dotnet core tool. The trigger works fine, but I'm unable to install and run the tool in my Tasks.
CURRENTLY:
Running a Command Line Task results in a 401:
dotnet tool install -g --add-source=https://pkgs.dev.azure.com/<org-name>/_packaging/Tools/nuget/v3/index.json MyApp
C:\Program Files\dotnet\sdk\3.0.100\NuGet.targets(123,5): error : Unable to load the service index for source https://pkgs.dev.azure.com/<org-name>/_packaging/Tools/nuget/v3/index.json. [C:\Users\VssAdministrator\AppData\Local\Temp\h0g1c35v.eny\restore.csproj]
C:\Program Files\dotnet\sdk\3.0.100\NuGet.targets(123,5): error : Response status code does not indicate success: 401 (Unauthorized). [C:\Users\VssAdministrator\AppData\Local\Temp\h0g1c35v.eny\restore.csproj]
The tool package could not be restored.
Tool 'myapp' failed to install. This failure may have been caused by:
* You are attempting to install a preview release and did not use the --version option to specify the version.
* A package by this name was found, but it was not a .NET Core tool.
* The required NuGet feed cannot be accessed, perhaps because of an Internet connection problem.
* You mistyped the name of the tool.
For more reasons, including package naming enforcement, visit https://aka.ms/failure-installing-tool
This leads me to believe that I'm missing something here, making it more complicated than it needs to be, or thinking about this the wrong way. The tools in the feed can be installed locally, so I believe it's my release approach.
I'm currently looking into Personal Access Tokens (PAT)
PREVIOUSLY:
If I use the .Net Core task and the custom option:
The logs show a malformed command passed to dotnet.exe:
[command]"C:\Program Files\dotnet\dotnet.exe" "dotnet tool install -g --add-source=https://pkgs.dev.azure.com/<org-name>/_packaging/Tools/nuget/v3/index.json MyApp"
or
[command]"C:\Program Files\dotnet\dotnet.exe" "tool install -g --add-source=https://pkgs.dev.azure.com/<org-name>/_packaging/Tools/nuget/v3/index.json MyApp"
I've tried varying arguments and I tend to always see the same error message:
Could not execute because the specified command or file was not found.
Possible reasons for this include:
* You misspelled a built-in dotnet command.
* You intended to execute a .NET Core program, but dotnet-dotnet tool install -g --add-source=https://pkgs.dev.azure.com/<org-name>/_packaging/Tools/nuget/v3/index.json MyApp does not exist.
* You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
Your custom dotnet command is quoted and dotnet is repeated : dotnet.exe" "dotnet tool install ..." so the command is misinterpreted.
You can use the Command Line task and set the dotnet command directly :
dotnet tool install -g --add-source=https://pkgs.dev.azure.com/<org-name>/_packaging/Tools/nuget/v3/index.json MyApp
Using the .Net Core task works perfect for us.
Since the dotnet command is quoted, you need to set tool as the command and update into arguments. NuGet credentials can be provided with NuGet Auth task if needed.
Here is my walkaround:
I firstly published my private dotnet tool nupkg file also as an universal package to the same Azure Artifacts feed.
I was able to then use Universal Package task to download the .nupkg file into $(System.DefaultWorkingDirectory)/nupkgs/. This task will handle the authorization to Azure Artifacts feeds.
steps:
- task: UniversalPackages#0
displayName: 'Download mytool.cli.universal'
inputs:
downloadDirectory: '$(System.DefaultWorkingDirectory)/nupkgs/'
vstsFeed: '63d4aa2f-3ae7-4c27-8c18-aa8e3a9ff353'
vstsFeedPackage: '916d9a27-2c07-4071-8631-377f2ac08ed7'
vstsPackageVersion: 0.2.0
I then had the DotNetCoreCLI task to install my nupkg locally in agents.
steps:
- task: DotNetCoreCLI#2
displayName: 'Install mytool as a dotnet tool'
inputs:
command: custom
custom: tool
arguments: 'install --global mytool.CLI --add-source ./nupkgs --version 0.2.0'
You need to add the nuget authenticate task before you try to access the nuget feed

Dotnet restore hook script?

Is there a way to run a custom script when my NuGet package is restored by dotnet restore command (or if this happens as a part of dotnet build)?
I want to copy a file to the user's home dir if it's not yet there.
Basically, I want to replicate an NPM install hook with .NET Core's NuGet.

Replacing project reference with NuGet packages on Azure DevOps

We have Project Reference on the .net core library. During dotnet pack on VSTS we would like to change that Project Reference to the actual NuGet Package reference from VSTS Feed.
Got nearly there using
dotnet remove ProjB.csproj reference ../../ProjA/src/ProjA.csproj
dotnet add ProjB.csproj package ProjA --no-restore
dotnet restore ProjB.csproj
dotnet pack ProjB.csproj --no-restore
Problem is in "dotnet add" with --no-restore as it adds reference with version="*":
<PackageReference Include="ProjA " Version="*" />
This causes referenced Package ProjA version in ProjB.nuspec to be incorrect (taken from the ProjA.scproj file instead of the actual version that was restored by "dotnet restore")
If not using --no-restore getting:
error: Unable to load the service index for source https://[our-team-project].pkgs.visualstudio.com/_packaging/[our-feed]/nuget/v3/index.json.
error: Response status code does not indicate success: 401 (Unauthorized).
Question: is any way to add package with correct version (without using --no-restore) or forcing "dotnet pack" to output correct dependency version in ProjB.nuspec file?
is any way to add package with correct version (without using --no-restore) or forcing "dotnet pack" to output correct dependency version in ProjB.nuspec file?
You can try to add the option [-v|--version] to specify the actual version.
Check the document dotnet add package for some details.
Synopsis
dotnet add [<PROJECT>] package <PACKAGE_NAME> [-h|--help] [-f|--framework] [--interactive] [-n|--no-restore] [--package-directory] [-s|--source] [-v|--version]
As I test, it works fine:
Note: You should publish the package ProjA to your feed before execute the dotnet restore command line.
Hope this helps.

Resources