DotNet CLI publish command to include packages from other NuGet source - teamcity

For one of my .Net Core project, I have a BuldProject.cmd CLI which publish the project. CLI Command I used is this
dotnet publish --framework %DotNetCoreVersionToTarget% --configuration %ConfigurationModeToUse% --force --verbosity normal
Above command build the project fine using Teamcity build server.
Recently I added a DLL to my project not from
nuget.org
but from my some other source say
nuget.mypack.com/nuget
csproj file has an entry for new DLL
<ItemGroup>
<PackageReference Include="TestCoreAPI" Version="1.0.1" />
</ItemGroup>
Now when I deploy the project using TeamCity I get the following error
error NU1101: Unable to find package TestCoreAPI. No packages exist with this id in source(s): nuget.org
The problem I see, it always looks into nuget.org for packages.
How can I modify my BuldProject.cmd CLI to look for other NuGet sources as well?

Related

Including Nuget Content Files to Target Projects Publish on .net Core Project

before i start i must state that i pretty new to this nuget package things, so come basic at me.
My app has a core project to carry some .dll and .sql files and an api project. I have made the core project a nuget package with dotnet pack and automated it on github action so it creates a new version at all prs and pushes it to my private nuget server and it is all fine till here.
When i install this core package to my api project, there is no problem either. It installs successfully and my sql files shows up on the target project as i expected(but as linked sources).
But when i try to publish this project with dotnet pack, the .sql files are not copied to output folder. I looked to the properties of the files and saw that their copy to output directory prop has been reset to do not copy and the file path was absolute.
My goal is to build and deploy this api with github actions so absolute paths are not acceptable and besides i don't want to arrange something in the target project manually.
I'm packing the core project with dotnet pack
I don't have any
.nuspec file
package config xml
nuget config
package metadata
or other things like than and i don't know how to use them.
I have tried adding to csproj file <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> and <RestoreProjectStyle>PackageReference</RestoreProjectStyle> things and they did not work.
I have tried adding to csproj file
and it did not work too.
Can anyone help me?
The only thing you need to do is that you should write this node <PackageCopyToOutput>true</PackageCopyToOutput> to every content file of the nuget net core project:
<ItemGroup>
<Content Include="Queries\Account\CheckAccountRef.sql">
<PackageCopyToOutput>true</PackageCopyToOutput>
</Content>
.....
</ItemGroup>
Then, re-pack your nuget project with dotnet pack and then reinstall this new version into your main project. Before you install, you should delete the old caches under C:\Users\xxx\.nuget\packages.
There is a similar issue about this.
You can use .nuspec file to configure all your dependencies. Please refer https://learn.microsoft.com/en-us/nuget/reference/nuspec

msbuild can't find references in projects.assets.json

I have a .net standard library project targeting .net standard 2.0 and I'm trying to build it in VSTS using MSBuild, the build server where the build agent is installed doesn't have internet access so I copied the dependencies to the C:\users\username.nuget\packages folder however MSBuild fails saying "Netsdk1064, Package microsoft.csharp, version 4.3.0 was not found".
I've tried copying the dependencies on the packages folder of the solution but still now working. I can't use Nuget restore on the build server so I was wondering if there is a way to redirect project.assets.json to look in the folder I created?
You need to check if package Netsdk1064, Package microsoft.csharp successfully copied in C:\users\username\.nuget\packages folder, since it errored this package was not found.
As workaround, you can create a nuget.config file and commit to your repo. And add the local directory where the all your packages reside under the packageSources element of nuget.config file. See below example.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="local" value="C:\users\username\.nuget\packages" />
</packageSources>
</configuration>
Check Package source sections of nuget.config for more information.
Then you can use nuget restore task to restore the packages. And Set nugetConfigPath point to the nuget.config created above.
- task: NuGetCommand#2
inputs:
command: 'restore'
restoreSolution: 'SmartFuel.sln'
feedsToUse: 'config'
nugetConfigPath: 'nuget.config'
If the nuget restore task failed to restore from the local package source. Please check the nuget version used in your pipeline. If the nuget version is 4.1.0 or older. You need to use NuGet Tool Installer task to use a higher versions of nuget. See this thread for more information.
Another possible workaround is to edit the .csproj file and manually point the referenced package to the .dll in local folder using hintpath. For example below:
<ItemGroup>
<Reference Include="DependPackage">
<HintPath>..\..\..\..\..\..\package\DependPackage.dll</HintPath>
</Reference>
</ItemGroup>

Self-hosted Azure Devops build cant resolve packages

I have a Azure DevOps build pipeline that runs as expected on a hosted vs2017 agent, but fails on a self-hosted agent.
The error I get in the Visual Studio build step is:
C:\WINDOWS\TEMP\.NETStandard,Version=v2.0.AssemblyAttributes.cs(4,20): Error CS0400: The type or namespace name 'System' could not be found in the global namespace (are you missing an assembly reference?)
The two agents seems to run the same version of msbuild.
From the diagnostic output from msbuild I can see that the output from the ResolvePackageDependencies task contains a lot of packages where the ResolvedPath is empty, for instance:
runtime.native.System/4.3.0
Name=runtime.native.System
Path=runtime.native.system/4.3.0
ResolvedPath=
Type=package
Version=4.3.0
But the NuGet restore step seems to complete without problems.
Any suggestions for what I am missing?
I believe I had a similar issue. I ended up having to install the latest Nuget and then run Nuget on the solution including a NuGet.config file.
Add a nuget.config to your solution so it is part of your repo/pull. Mine is in the same directory as the solution file. Example below
Add a task "NuGet Tool Installer" - I install NuGet 4.4.1, just put 4.4.1 in the Version to install input.
Add a task "NuGet Installer" - Different from above. Version 0.* - I have not tried the other versions.
Set the Path to the solution. IE. $(Build.Repository.LocalPath)/Source/Sample.sln
Add the path to the Nuget config file. Example $(Build.Repository.LocalPath)/Source/nuget.config
Nuget.config contains how to get the packages. Add other locations if you get packages from other sources like a local folder or something.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--
Used to specify the default Sources for list, install and update.
See: nuget.exe help list
See: nuget.exe help install
See: nuget.exe help update
-->
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
<packageRestore>
<!-- Allow NuGet to download missing packages -->
<add key="enabled" value="True" />
<!-- Automatically check for missing packages during build in Visual Studio -->
<add key="automatic" value="True" />
</packageRestore>
Your build task should run fine now and find all the packages.
Self-hosted Azure Devops build cant resolve packages
According to the error message, it seem nuget not restore the reference from SDK.
To resolve this issue, we need update our nuget.exe version to 4.0 and above.
In the NuGet tool installer we could specify the nuget.exe version:
As you comment above, it seems you have already use nuget installer, in this case, you can try to update Visual Studio to 15.3 and above on the build server. Because VS only adds proper support for .NET Core 2.0 SDK in version 15.3.
Finally, if your project/solution is .net core/standard you can use dotnet restore and then run dotnet build to compile your app.
Hope this helps.

NuGet package from PCL

I have a PCL library I want to distribute as NuGet package.
Screenshot of the PCL application library properties page:
csproj file:
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkProfile>Profile111</TargetFrameworkProfile>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Steps to reproduce my problem:
Clone both repos from:
NuGet package
Console application + PCL library
Build the NugetPackage project + nuget spec + nuget pack
Create a local nuget package repository pointing to the project folder.
Open the NugetConsole solution and try installing the package in both projects.
Some references I used:
https://learn.microsoft.com/en-us/nuget/schema/target-frameworks
https://learn.microsoft.com/en-us/nuget/schema/nuspec
https://portablelibraryprofiles.stephencleary.com/
http://blog.stephencleary.com/2012/05/framework-profiles-in-net.html
How can I make a nuget package from a PCL library and use it in a PCL library?
I have downloaded your nuget package and test projects, I can created the nuget package and installed it to the both projects. Following are my detailed steps:
1. Create nuget package:
Download the nuget project, then build the project+nuget spec+nuget pack:
Here is the .nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>My.Package</id>
<version>1.0.0</version>
<authors>Tester</authors>
<owners>Tester</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package description</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2018</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
<files>
<file src="bin\**" target="lib\portable-net45+wp8\" />
</files>
</package>
Note: Since you are planning use this package into PCL library, so the target should be .net45 + Profile49, according to the document Portable Class Library (PCL) profiles, nuget target should be portable-net45+wp8:
2. Install that package to both projects:
Download those two test projects from GitHub, and open it with Visual Studio 2017, then copy the created package to the local nuget feed:
Open Package manager Console, install the package with following command:
install-package My.Package -source D:\LocalServer
So the nuget package is correct install in both projects.
Note: I have seen following code in your project file of console application:
<ItemGroup>
<PackageReference Include="My.Package">
<Version>1.0.0</Version>
</PackageReference>
</ItemGroup>
Please remove it, this is not a correct way to add nuget package.

NuGet restoring PostSharp package before the build begins

I am using PostSharp and I have the following target description in my project file:
<Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">
<Error Condition="!Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://www.postsharp.net/links/nuget-restore." />
<Error Condition="Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore." />
</Target>
As far as I understand, this is added to the project when PostSharp is referenced through NuGet, and the error conditions check the following:
The first error condition breaks the build when PostSharp is not available (i.e. NuGet did not restore it successfully).
The second error condition breaks the build when PostSharp was successfully restored by NuGet on the last build but was therefore not included in the project, so therefore a rebuild is necessary.
BUT, if I have the following configuration in NuGet.Config and .csproj file, is the second error condition even necessary?
NuGet.Config file:
<configuration>
<packageRestore>
<!-- Allow NuGet to download missing packages -->
<add key="enabled" value="True" />
<!-- Automatically check for missing packages during build in Visual Studio -->
<add key="automatic" value="True" />
</packageRestore>
...
</configuration>
.csproj file:
<RestorePackages>true</RestorePackages>
As far as I understand, NuGet will then restore the missing packages BEFORE the build even starts. The second error condition will essentially break the build for no reason at all.
Note: I am using Visual Studio 2013 and NuGet 2.8.
It depends on how the restore is done and which version of NuGet you have installed. It looks like the error messages are trying to cover three scenarios:
Building without the MSBuild based package restore enabled (which is configured inside Visual Studio by right clicking the solution and selecting Enable Package restore).
Building outside of Visual Studio when the MSBuild based package restore is not enabled.
Building with Visual Studio using an old version of NuGet which does not support the automatic restore before a build.
If you are using the MSBuild based package restore then the restore will occur during the build and the PostSharp files will not be imported at this point so the $(PostSharp30Imported) will be empty and the second error message will be displayed. At least I suspect that is the case.
If you building from the command line and not using the MSBuild based package restore then you would see the first error message if the NuGet packages were missing.
If you are not using the MSBuild based package restore, and are building from within Visual Studio with a recent version of NuGet, then you are correct that the packages will be restored before anything is built at all. So the PostSharp imports should be available to MSBuild before it is even executed.
As PostSharp dlls are required during msbuild loading (so targets referencing this dlls are available during build) they must be available during final call to msbuild.
While in VS it is acceptable to click build twice, I was using PostSharp in CI environment, and requirement to call build on solution two times was frustrating (first build restore nugets but also failed build due to error).
I ended up with separate build steps:
Restore nuget Packages (this downloads PostSharp packages and return success code to environment):
NuGet.exe restore SolutionWithProjectsUsingPostSharp.sln
Build solution.
You need to edit the second error condition in the target in the csproj
<Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">
<Error Condition="!Exists('....\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://www.postsharp.net/links/nuget-restore." />
<Error Condition="Exists('....\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore." />
</Target>>
I have answered in detail in a different post at SO
We are using 'old' MSBuild-Integrated package restore ( .nuget\NuGet.targets file is present) and normally do not store in source control packages, but rely on build to restore them for each build.
But for PostSharp on TeamCity build server I am getting error :
The build restored NuGet packages. Build the project again to include
these packages in the build.
The simplest way is to explicitly include in source control packages\PostSharp.VerXXX.
Alternatively solution could be migrating to automatic package restore,
as it was advised in Issue Using PostSharp 3.x with NuGet Auto Restore
Right click on the solution, Manage Nuget packages; and remove the packages you dont want .
This error also shows up , when trying to restore the packages from the web. Just connect your self to the internet and then try opening the project.
The errors went away for me on following the above steps.

Resources