How to restore nuget private feed using MSBuild in Visual Studio - c#

I'm configuring an existing project (dotnet core) to use private nuget repository.
I've setup my project with the structure as follows:
Project1
Project2
Project3
.sln
nuget.config
I'm able to restore my packages if i'm to run a "dotnet restore" commands but not while using Visual Studio's build (MSBuild).
I would like to be able to restore my package while using Visual Studio's MSBuild.(not using cli)
If possible, i'd like to avoid using msbuild's Exec task to run dotnet restore before build.
I've tried them in both Visual Studio 2017 and 2019

You will need to run the MSBuild Restore target before the actual target. MSBuild.exe provides a built-in way to execute this target, then flush all caches (needed to avoid incremental build issues due to freshly restored packages) and then perform the actual build with the the -r (long: -restore) argument:
e.g.:
msbuild -r -p:Configuration=Release the.sln

Related

Could not load file or assembly after installing new NuGet package on another system

I'm currently trying to develop a cross platform supported .NET 5.0 application. I use Visual Studio on Windows to debug and develop, and then I use WSL in the same project folder to run dotnet publish with the --runtime linux-x64 argument.
Even if I remove the \bin and \obj directories in my project, I get an Could not load file or assembly on the newly installed NuGet package, when trying to do dotnet publish --platform linux-x64 via WSL. Running with F5 in Visual Studio works perfectly fine.
Here's the output:
$ dotnet publish -c Release -o publish -p:PublishReadyToRun=true -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true -p:IncludeNativeLibrariesForSelfExtract=true --runtime linux-x64 Project.sln
Microsoft (R) Build Engine version 16.10.1+2fd48ab73 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
Restored /mnt/c/Users/morten/Desktop/Project/Project.csproj (in 327 ms).
Project -> /mnt/c/Users/morten/Desktop/Project/bin/Release/net5.0/linux-x64/Project.dll
Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
Project -> /mnt/c/Users/morten/Desktop/Project/publish/
So as you can see, it takes it 327ms to restore the packages, even after removing the two directories. Something tells me there's an underlying cache hidden somewhere.
I tried doing dotnet restore as well as --force, but the time to restore is still roughly the same. If I do cat *.csproj I can definitely see my new NuGet package.
I tried clearing the NuGet cache by doing dotnet nuget locals all --clear, but that didn't help either. It took about 5 seconds to restore the packages this time, though.
Where do I go from here?

Is there any way to view Nuget package sources available for visual studio directly from command line

How do I view Nuget package sources in visual studio through command line, WITHOUT going to package manager setting in visual studio? Also, dotnet cli also shows nuget package sources by running "dotnet nuget list source" command. Are these sources outputted by the dotnet cli the same nuget source in VS?
You can download the nuget.exe here: https://www.nuget.org/downloads
Then run this command: nuget.exe sources List
Or you can directly check the NuGet.Config file in %appdata%\NuGet(the full path: C:\Users\ {username} \AppData\Roaming\NuGet)

dotnet restore on .NET Framework project not working

I have a .NET project with the following in its .csproj
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
When I run dotnet restore project-file.csproj I get the following:
Nothing to do. None of the projects specified contain packages to restore.
Why is this? I thought the dotnet cli could work with non Core projects too?
I have nuget packages that are referenced, so I expect the cli to go and download the nuget packages.
dotnet cli works properly with .NET Framework only if the project was created from dotnet new command. If you create project from Visual Studio the structure of .csroj files will be different and you usually cannot run cli commands towards them
Visual Studio only allows you to run nuget commands from nuget console (package manager console) within Visual Studio itself.
If you want to do nuget restore from command line:
Download nuget executable from https://www.nuget.org/downloads
(it is not a installer/package, but actual executable!)
Save it to a folder of your choice and add it to the PATH.
Then, as suggested above: nuget restore solutionname.sln (but this time you don't have to run Visual Studio!)

new .net core visual studio 2017 project nuget restore in CI build definition

So currently, I am working on a .net core project in Visual Studio 2017. And I am also need to setup CI (Continuous Integration) in VSTS.
Here is my nuget restore step in my build definition
But my build is failing because it couldn't restore any packages. Here is the log of its. It said that "None of the projects in this solution specify any packages". I know that in VS2017 they changed the way nuget packages are being handled.
Anyone has an idea or experience about this?
2017-03-22T20:25:11.9517911Z MSBuild auto-detection: using msbuild version '14.0' from 'C:\Program Files (x86)\MSBuild\14.0\bin'.
2017-03-22T20:25:11.9674167Z Nothing to do. None of the projects in this solution specify any packages for NuGet to restore.
2017-03-22T20:25:11.9830425Z ##[section]Finishing: NuGet restore **/*.sln
I don't want to use dotnet restore because Microsoft said that
.NET Core's dotnet restore command doesn't currently support encrypted
credentials. To use VSTS NuGet feeds with dotnet restore, you'll need
to specify a Personal Access Token in plain text.
so I don't want to store my Personal Access Token.
https://www.visualstudio.com/en-us/docs/package/nuget/auth#net-core
You need to use NuGet 4.0.
Download it form the official NuGet site. https://dist.nuget.org/index.html
Put it in a folder on the machine where you have the build agent.
In the Advanced settings of the NuGet restore step set the NuGet version to Custom.
In the Path to NuGet.exe specify the path where you have put the NuGet.exe file. This is the path on the machine where the build agent is running. I tried with a relative path but was not able to get it working so in the end I have put an absolute path. This might be a problem for you if you have multiple agents with different paths so you might need to figure out how to correctly specify the relative path.
The Nuget restore task just includes Nuget.exe 3.3 and 3.5, so there isn’t the option of 4.0. You can check the files in [build agent folder]\tasks\NuGetInstaller\[version]\ node_modules\nuget-task-common\NuGet folder.
You can build a custom build/release task to include nuget.exe 4.0 and use this task instead. More information, you can refer to Add a build task.
On the other hand, you can add Nuget.exe to the source control, then map it to the build agent (Repository tab of build definition) and specify the path with built-in variable (e.g. $(build.sourcesdirectory))

MSbuild Package via Command line not including all my Assemblies

I am trying to setup a deploy script to do a publish of our website rather than have to open VS to do a deploy/publish.
When i do a publish via VS it will include all referenced assemblies correctly however when I do it via the command line it will not. I am at a lose for what I am missing
My MSBuild command is:
msbuild "myproject.csproj" /T:Package;ResolveReferences /P:Configuration=Debug
/P:DeployOnBuild=True /P:DeployTarget=MSDeployPublish /P:CreatePackageOnPublish=True
/P:MSDeployPublishMethod=RemoteAgent /P:WebProjectOutputDire="PreCompiled"

Categories