Replacing project reference with NuGet packages on Azure DevOps - .net-core

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.

Related

dotnet publish does not pull NuGet packages

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

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

Error "dotnet : Could not find any project in `C:\**." when running "dotnet add package Microsoft.AspNetCore.Authentication.MicrosoftAccount"

I have visual studio 2019 and i created a new asp.net core 2.2 project. now i am following these steps Configure Microsoft Account Authentication to enable external login to our web application. but when i run this command:-
dotnet add package
Microsoft.AspNetCore.Authentication.MicrosoftAccount
i got this error:-
PM> dotnet add package
Microsoft.AspNetCore.Authentication.MicrosoftAccount dotnet : Could
not find any project in C:\Users\*****\source\repos\MSlogintest\. At
line:1 char:1
+ dotnet add package Microsoft.AspNetCore.Authentication.MicrosoftAccount
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Could not find ...\MSlogintest`.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError Usage: dotnet add package [options]
Arguments: The project file to operate on. If a
file is not specified, the command will search the current directory
for one. The package reference to add.
Options: -h, --help Show command line help.
-v, --version The version of the package to add. -f, --framework Add the reference only when targeting a specific framework. -n, --no-restore
Add the reference without performing restore preview and compatibility
check. -s, --source The NuGet package source
to use during the restore. --package-directory The
directory to restore packages to. --interactive
Allows the command to stop and wait for user input or action (for
example to complete authentication).
as follow:-
and when i access the folder, i found that there is a VS project folders, as follow:-
so why the error is saying that it can not find any project?
can anyone advice on this error please?
Thanks
I encountered the same issue and found the fix. First of all, read the error message completely and carefully. It says:
"Could not find any project ...."
Which means it was expecting the project information. If you further watch the message closely, it has mentioned the correct usage of this command i.e.
Usage: dotnet add <PROJECT> package [options] <PACKAGE_NAME>
In the argument, enter your project name and run it again. This time, you should see the success message similar to:
info : Adding PackageReference for package 'Microsoft.xxxxxxxx.xxxxxxxx' into project 'C:\Users\xxx\source\repos\ProjectFolder\xxxxxxxx.csproj'.
Got this working on powershell (no IDE). The solution was somewhere hidden in the comments but here is the easy one:
dotnet add <project> package <packageName>
In this case I believe it would be:
dotnet add ContosoUniversity package Microsoft.AspNetCore.Authentication.MicrosoftAccount
cd into the particular project you want to add the package to and type your dotnet command again
> cd project Directory
> dotnet add package Microsoft.AspNetCore.Authentication.MicrosoftAccount
Had the same problem, when i was fiddling around with the nuget commandline it at some point proposed i should retry with elevated privileges.
Specifically i tried manually pointing it one level deeper in the folder structure. (As suggested by DavidG)
I thought: okay, weird, but i am out of good ideas, so lets try that one.
Restarted as administrator, worked.
And by it worked i mean it just worked, no manual fiddling involved. I just used the graphical package manager option that just somehow became available.
So my best guess is i somehow messed up something when installing visual studio. Because i already tried just creating a new project.
Nuget option as admin
You may be clicking on the wrong tab on the nuget site. Make sure you select Package Manager tab. The error you get happens when you click on the .net cli tab and use it in the package manager console window.
Try this in your command terminal, you should into a directory project:
dotnet add package <package.name>
This command is for .NET core. I used it for an old project because it's the first one shown on nuget.org.
The old command should be like this:
Install-Package [package name]

DocFx Error after installing dotnet core 2.1.300

I've create a simple console application along with a simple docfx project. When I run the DocFX command to generate the documentation I get the following metadata warning and becuasue the warning leads to the documentation not being generated.
[18-06-05 05:52:41.715]Warning:MetadataCommand.ExtractMetadataWorkspace failed with: [Failure] Msbuild failed when processing the file 'E:\Files\tmp\docx\docfx_project\src\src.csproj' with message: C:\Program Files\dotnet\sdk\2.1.300\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets: (198, 5): Error loading lock file 'E:\Files\tmp\docx\docfx_project\src\obj\project.assets.json' : Object reference not set to an instance of an object.
later in the output you see
[18-06-05 05:52:42.467]Warning:[MetadataCommand.ExtractMetadata]No metadata is generated for src.
Anyone else run into this problem? How can I get metadata to generate?
To recreate the project is pretty simple:
goto dotnet cli tools command line
docfx init
cd src
dotnet new console
cd ..
docfx
I have installed and using the dotnet sdk 2.1.300
With .net 2.1 there is a breaking change in the use of tools, which are now installed and used globally like this:
dotnet watch
dotnet user-secrets
dotnet sql-cache
dotnet dev-certs
The developers of docfx are currently working on a version which supports .net 2.1 (docfx v3): https://github.com/dotnet/docfx/pull/2829
Here you can see the current status of implemenation: https://github.com/dotnet/docfx/projects/1
So the answer to your question is: as of yet you can't use docfx with .net 2.1

No executable found matching command "dotnet-test-xunit"

I am trying to run a xunit test project. I've followed the below steps to do that so, but however I've been getting "No executable found matching command dotnet-test-xunit" error.
Steps:
1. Created a folder named "Tests" in the local drive.
2. Keeping the "Tests" folder as current directory in the command line, I ran the following commands:
a) dotnet new --type xunittest
b) dotnet restore
c) dotnet test
3. Finally, while executing the "dotnet test" command, I got "dotnet-test-xunit" not found.
My .NET(dotnet) version: 1.0.0-preview2-003131
Also, when I searched for the "dotnet-test-xunit" package in the nuget repository (https://www.nuget.org/packages?q=dotnet-test-xunit), it is not found.
Can anyone guide me through this ? Thanks in advance :)
The dotnet-test-xunit package has been unlisted.
.NET Core projects that are project.json-based are no longer supported (preview2 and earlier versions).
Newer versions of .NET Core use the xunit package.

Resources