Creating NuGet using Team Foundation Server missing dependencies - c#

I have created a simple class library in .net standard 2.0 with a few nuget dependencies like Dapper.
I am using Team Foundation Server 16 to then build and package the project. My issue is, that when I then browse to my new NuGet package, it does not list it's dependencies in the NuGet package manager in VS and I have to install them manually afterwards.
Creating a nuget package of the same class library from Visual Studio 2019 locally works as intended.
My build tasks on TFS are:
Use nuget 5.4.0
NuGet restore
Build solution
Run script (A .bat file for updating version number)
NuGet pack
Publish build artifact
The NuGet pack uses default settings with command "pack" and path pointing only to .csproj file.

Creating a nuget package of the same class library from Visual Studio
2019 locally works as intended.
It's one issue about nuget pack command. When you pack the .net standard project in VS locally, it(right-click=>pack button) actually calls dotnet cli instead of nuget.exe to do the pack job.
For now, nuget pack command can't work well with those projects that use PackageReference to manage nuget packages. (Including .net framework projects with PackageReference,.net core and .net standard projects).
More details see discussions here and here.
To resolve that issue(For TFS2017 and above):
Use dotnet pack command instead of nuget pack command. And for pipeline in tfs, use dotnet restore, build, pack tasks instead of nuget restore, nuget pack tasks.
Update1 for TFS2016:
Since TFS will run those tasks in tfs agents, one alternative way is to install .net core sdk manually, and then use command-line task to execute dotnet pack command to create nuget packages.
.net core sdk download link here.
Update2:
Also, we can still use nuget pack command/task. To include those dependencies, we need to create an extra xx.nuspec file with content similar to this:
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>PackageName</id>
<version>1.0.0</version>
<title>xxx</title>
<authors>xxx</authors>
<owners>xxx</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">MIT</license>
<description>xxx</description>
<releaseNotes>xxx</releaseNotes>
<copyright>Copyright 2020</copyright>
<tags>Tag1 Tag2</tags>
<dependencies>
<dependency id="Dapper" version="1.30.0"/>
//define other dependencies manually here.
</dependencies>
</metadata>
</package>
Place this file in same directory where xx.csproj exists, and then nuget pack command/task can now create the package with dependencies.

Related

How to add a local library to a C# web application to debug?

I have a project which is published as a nuget package(No way to run standalone).
I'm installing the above said nuget package in other project and consuming it.
Now I need to debug the installed nuget package. I have the package's code on my local machine.
Is there a way where I can reference to local source code instead of downloading and installing nuget package for debugging purpose?
-I don't want to publish nuget package with symbols

target framework considerations with nuget packages?

I'm fairly new to the process of creating NuGet packages. I recently created a NuGet package via NuGetPackageExplorer. The "Package Metadata" view has a "Framework Assembly References" section with a value of "Microsoft.CSharp (Any,Verion=0.0)." This is what the nuspec file looks like:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>My.Cool.NuGet.Package</id>
<version>1.0.2</version>
<title></title>
<authors>John Smith</authors>
<owners>John Smith</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>My cool NuGet package</description>
<frameworkAssemblies>
<frameworkAssembly assemblyName="Microsoft.CSharp" />
</frameworkAssemblies>
</metadata>
</package>
Can you please explain relationships, dependencies and constraints for the following dimensions:
Target Framework version of the source .NET project for the NuGet package
frameworkAssemblies section of the nuspec file
Target Framework version of the project which installs the NuGet package
The frameworkAssembly value in the nuspec above is displayed as follows in the NuGetPackageExplorer:
Assembly name: Microsoft.CSharp
Supported frameworks: Any,Version=v0.0
Does this mean that the NuGet package is intended to be .NET framework version agnostic? Are there any scenarios where I would want to explicitly state a specific version(s) in this section? The reason I'm asking is that I installed a NuGet package in an MVC project yesterday and I was experiencing some weird behavior. Specifically:
The NuGet package showed as installed and displayed in the References list
I was able to add a using directive to the namespace of the NuGet package
However, when I attempted to run the app, the compiler displayed an error of "type of namespace not found" for these using directives. But then the compiler errors disappeared but the app wouldn't run b/c of the previous compiler errors. After I fiddled with the version settings with the MVC project and NuGet package source project, I was able to get around this error. So now I'm trying to get an understanding of the finer details of what I need to consider and configure in regards to target frameworks.
The TargetFamework of the source project for the NuGet package should be the lowest framework version you need by projects that will install it. For example if you know you will have projects in the 4.5 and 4.6 framework you make the source for the package 4.5. You can also put multiple builds for multiple frameworks inside a package. Here is the documentation about that.
The TargetFramework for the projects installing the package should be greater or equal that the version of the package. Since higher versions of the framework are compatible with lower versions. It should not allow you to install a package that is not supporting the framework you are using in the project.
The frameworkAssemblies sections just defines which framework assemblies a package is using so the references are automatically added. Check the documentation for more detailed info.

Why nuget from VSTS nuget feed wants to install the same package multiple times and throwing error?

I have a REST Api implemented using Durable Function App in Azure, which uses netstandard2.0, and I have other class libraries in this solution also using netstandard2.0. One of them is contracts projects.
There is another solution where the UAT tests placed for this solution. The reason for this is that Specflow is still not available for .Net Core, it is available for .Net Framework only. Being able to test the Api I need the contracts for the Api. That is the reason I need to include the REST Api solution Contracts project (this one is netstandard2.0). This dependency is managed by nuget.
When I would like to install the Contracts package (using netstandard2.0) in Specflow project (which uses .Net Framework 4.7.1) I get the error below.
What I tried:
I restarted Visual Studio a few times because I experienced that sometimes it gets crazy when .Net Core or .Net Standard projects are in use. Restarting did not help.
I opened the project from JetBrains Rider, but it did not see the netstandard2.0 nuget package in the repository. I believe it is due to that the project uses .Net Framework 4.7.1 and not Net Standard 2.0
I saw the error Cannot create "C:\...\REST.Api.dll" because a file or directory with the same name already exists.. So, I deleted all packages under the solution packages directory. I also deleted cache in Visual Studio. But, the next time I started Visual Studio and tried to install package the same error occurred again.
I also tried to install package using console on my machine (nuget.exe install ...). The error is the same.
How it is possible to resolve this issue? I read that from .Net Framework 4.6 and above and .Net Core it is possible to reference .Net Standard libraries.
Attempting to gather dependency information for package 'REST.Api.1.0.0-20180305-154726' with respect to project 'MasterData.Features.HttpClient', targeting '.NETFramework,Version=v4.7.1'
Gathering dependency information took 861.8 ms
Attempting to resolve dependencies for package 'REST.Api.1.0.0-20180305-154726' with DependencyBehavior 'Lowest'
Resolving dependency information took 0 ms
Resolving actions to install package 'REST.Api.1.0.0-20180305-154726'
Resolved actions to install package 'REST.Api.1.0.0-20180305-154726'
GET https://xyzrepository.visualstudio.com/_packaging/689a2aa0-1e6a-4f39-8389-250b4ad44db8/nuget/v3/flat2/REST.api/1.0.0-20180305-154726/REST.api.1.0.0-20180305-154726.nupkg
Retrieving package 'REST.Api 1.0.0-20180305-154726' from 'Sayusi Ando's Digital Library Packages'.
OK https://xyzrepository.visualstudio.com/_packaging/689a2aa0-1e6a-4f39-8389-250b4ad44db8/nuget/v3/flat2/REST.api/1.0.0-20180305-154726/REST.api.1.0.0-20180305-154726.nupkg 2343ms
Installing REST.Api 1.0.0-20180305-154726.
Error downloading 'REST.Api.1.0.0-20180305-154726' from 'https://xyzrepository.visualstudio.com/_packaging/689a2aa0-1e6a-4f39-8389-250b4ad44db8/nuget/v3/flat2/REST.api/1.0.0-20180305-154726/REST.api.1.0.0-20180305-154726.nupkg'.
Cannot create "C:\Users\username\.nuget\packages\REST.api\1.0.0-20180305-154726\lib\netstandard2.0\REST.Api.dll" because a file or directory with the same name already exists.
GET https://xyzrepository.visualstudio.com/_packaging/689a2aa0-1e6a-4f39-8389-250b4ad44db8/nuget/v3/flat2/REST.api/1.0.0-20180305-154726/REST.api.1.0.0-20180305-154726.nupkg
OK https://xyzrepository.visualstudio.com/_packaging/689a2aa0-1e6a-4f39-8389-250b4ad44db8/nuget/v3/flat2/REST.api/1.0.0-20180305-154726/REST.api.1.0.0-20180305-154726.nupkg 462ms
Installing REST.Api 1.0.0-20180305-154726.
Error downloading 'REST.Api.1.0.0-20180305-154726' from 'https://xyzrepository.visualstudio.com/_packaging/689a2aa0-1e6a-4f39-8389-250b4ad44db8/nuget/v3/flat2/REST.api/1.0.0-20180305-154726/REST.api.1.0.0-20180305-154726.nupkg'.
Cannot create "C:\Users\username\.nuget\packages\REST.api\1.0.0-20180305-154726\lib\netstandard2.0\REST.Api.dll" because a file or directory with the same name already exists.
GET https://xyzrepository.visualstudio.com/_packaging/689a2aa0-1e6a-4f39-8389-250b4ad44db8/nuget/v3/flat2/REST.api/1.0.0-20180305-154726/REST.api.1.0.0-20180305-154726.nupkg
OK https://xyzrepository.visualstudio.com/_packaging/689a2aa0-1e6a-4f39-8389-250b4ad44db8/nuget/v3/flat2/REST.api/1.0.0-20180305-154726/REST.api.1.0.0-20180305-154726.nupkg 464ms
Installing REST.Api 1.0.0-20180305-154726.
Install failed. Rolling back...
Package 'REST.Api.1.0.0-20180305-154726' does not exist in project 'MasterData.Features.HttpClient'
Package 'REST.Api.1.0.0-20180305-154726' does not exist in folder 'C:\VSTS\REST.QA.Features\src\packages'
Executing nuget actions took 3.51 sec
Error downloading 'REST.Api.1.0.0-20180305-154726' from 'https://xyzrepository.visualstudio.com/_packaging/689a2aa0-1e6a-4f39-8389-250b4ad44db8/nuget/v3/flat2/REST.api/1.0.0-20180305-154726/REST.api.1.0.0-20180305-154726.nupkg'.
Cannot create "C:\Users\username\.nuget\packages\REST.api\1.0.0-20180305-154726\lib\netstandard2.0\REST.Api.dll" because a file or directory with the same name already exists.
Time Elapsed: 00:00:05.5462783
The root of the problem is that the target properties conflicted with another filename. (names are shortened)
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<!-- package stuff -->
<dependencies>
<group targetFramework=".NETStandard2.0" />
</dependencies>
</metadata>
<files>
<file src="..." target="lib\netstandard2.0\Contracts.dll" />
<file src="..." target="lib\netstandard2.0\Contracts.dll" />
<file src="..." target="lib\netstandard2.0\Contracts.dll" />
</files>
</package>

nuget pack with -IncludeReferencedProjects flag don't work when NET Framework project has project reference to netstandard project

I have 2 libraries:
first library(First.csproj) - netstandard1.3
second library(Second.csproj) - .Net framework 4.6.1
Second.csproj has project reference to First.csproj.
Compilation into VS2017 working without problems.
After that i want to create nuget package for Second.csproj with dependency to First.csproj, but command
nuget pack Second.csproj -IncludeReferencedProjects
Failed with error
Error occurred when processing file 'E:\blabla\First.csproj': Unable to find 'bin\Release\First\bin\Release\'. Make sure the project has been built.
Unable to find 'bin\Release\First\bin\Release\'. Make sure the project has been built.
My nuget version is latest: NuGet Version: 4.4.1.4656
How can i correctly pack my .NET Framework 4.6.1 project with adding dependency to netstandard library?
It's a known bug of nuget: https://github.com/NuGet/Home/issues/4054.
Ugly workaround:
1) Manually add dependency on package of First.csproj into .nuspec file of Second.csproj
<dependencies>
<dependency id="First" version="actual version of First here" />
</dependencies>
2) Use nuget pack without flag -IncludeReferencedProjects

.net core 1.0 visual studio referencing external dll

with the release of the .net core i have been trying to build a simple project, however whenever i try and add a dll reference in my project i get the following message
".Net Core Projects only support Referencing .Net Framework assemblies in this release To Reference other assemblies they need to be included in nuget package and reference that package"
i was getting this message in RC2 but not in RC1, is anyone else having this issue and does anyone know how to resolve it? i have not been able to find anything relating to this other than a git issue ticket https://github.com/aspnet/Home/issues/1612
For referencing external dll in .net core you need to create you own nuget package.
The NuGet docs show how to create a package from a dll:https://docs.nuget.org/create/hosting-your-own-nuget-feeds . You can put that nuget package in a local folder and use that as a feed: [https://docs.nuget.org/create/hosting-your-own-nuget-feeds]
For this you need to edit the nuspec file and add the following code in the nuspec file.
<package>
*******--Some code--*****
<metadata>
<references>
<reference file="xxx.dll"/>
</references>
</metadata>
<--For addig reference of external dll-->
<files>
<file src="path\*.dll" target="lib\netCoreApp1.0"/>
</files>
Now create .nupkg file and install this package in your project.
Hope this solution works for you.

Categories

Resources