Include referenced project in nuget package - c#

I have a solution with 3 projects (using netstandard1.4). Project A contains shared code. Project B is a server side library and project C is a client side library. Project B and C include project A as project reference.
Now I want to publish project B and project C as a nuget package.
The Problem is the nuget packages for project B and C do not contain the code / dll from project A. It looks like project B and C want project A also as a nuget package.
How can I pack the project B and C as standalone nuget packages? I don’t want to publish project A as a nuget package.

How can I pack the project B and C as standalone nuget packages? I don’t want to publish project A as a nuget package.
Since you are using .NET Standard 1.4, you could not use the direct way "dotnet pack" to include the project references. Because dotnet pack will pack only the project and not its P2P references, you can get the detail info from the document dotnet-pack and the issue on GitHub:
NuGet dependencies of the packed project are added to the .nuspec file, so they're properly resolved when the package is installed. Project-to-project references aren't packaged inside the project. Currently, you must have a package per project if you have project-to-project dependencies.
If you want to pack project B and C contain the code / dll from project A, you can use NuGet.exe to create the package B and C by adding project reference assemblies to the .nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>TestProjectB</id>
<version>1.0.0</version>
<authors>Tester</authors>
<owners>Tester</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package description</description>
<releaseNotes>Test sample for netstandard package.</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
<files>
<file src="bin\Debug\netstandard1.4\TestProjectB.dll" target="lib\netstandard1.4\TestProjectB.dll" />
<file src="bin\Debug\netstandard1.4\TestProjectA.dll" target="lib\netstandard1.4\TestProjectA.dll" />
</files>
</package>
In this case, you can pack the project B and C as standalone nuget packages, do not need publish project A as a nuget package.

Related

Excluding files from Nuget package

I have a few projects, which could be a console application, Web Application, WebAPI, MVC etc) and a normal Class Library (not Core/.Net 6) which contains Newtonsoft.Json dll (latest version).
Each of the applications have their own version of Newtonsoft.Json dll that has been added manually for that project. In doing this it means an older app could have used version 1.0 the next application could have used v 2.0 and later apps could have used a much later version etc
I would like to share this class library through a Nuget package but i have added the latest version of Newtonsoft.Json dll into my own class library.
When i create a .nuspec file i would like to exclude this file but using this code
<files>
<file src="bin\Release\Newtonsoft.dll" target="\" />
</files>
within the .nuspec file doesnt seem to work, reading https://learn.microsoft.com/en-us/nuget/reference/nuspec suggests to use the exclude but i cant get the syntax right.
What am i missing here?
In your nuspec include this:
<dependencies>
<dependency id="Newtonsoft.Json" version="[3.5,14)" />
</dependencies>
Where 3.5 is the oldest version you support and 14 is the newest you do not support.
Then you can leave out newtonsoft.dll from you own package and the project you install the nuget into will be able to determine which newtonsoft to use.

Creating NuGet using Team Foundation Server missing dependencies

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.

Migrate from PCL to .netstandard

I'm trying to figure out how to migrate a project from PCL to .netstandard 1.2.
I have a solution where I have a PCL project (portable45-net45+win8+wpa81) and a .Net Standard (netstandard1.2) project which has all it files linked into the PCL project.
Currently we create a nuget package from the PCL project using a nuspec file.
Now what would be the best approach to have both available in 1 nuget package?
I find the use of nuget pack vs dotnet pack and mixing multiple frameworks and project types (csproj) very confusing.
Also appearantly there is a new csproj format for VS2017+ projects, should I convert the PCL project?
Eventually the nuget should only contain the .netstandard1.2 project but we want to take both up the dependency tree during migration.
what would be the best approach to have both available in 1 nuget package?
You can still use .nuspec file to accomplish this, just need include the dll files from PCL project and .Net Standard project into different frameworks.
Following is my test .nuspec file, you can check it for details:
<?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="TestPCL\bin\Debug\TestPCL.dll" target="lib\portable-net45+wp8\" />
<file src="TestStandard\bin\Debug\netstandard1.2\TestStandard.dll" target="lib\netstandard1.2\" />
</files>
</package>
When you install this package to the PCL project and .Net Standard project, nuget will select the DLL file under the corresponding framework to your project.

.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.

Local NuGet package source not showing package

I'm stumped. I've followed the directions to set up a local NuGet Package Source as closely as I could, but my package refuses to show up in the NuGet Package manager. The screenshot below shows:
I've created the NuGet package
I put the package in C:\Packages
I created a package source Local that points to C:\Packages
In the Package Manager, I've selected Local as the package source
But it cannot find the package. What am I missing?
Here's my .nuspec for reference, with the assembly name removed.
<?xml version="1.0"?>
<package >
<metadata>
<id>$AssemblyName%$</id>
<version>1.2.0-beta2</version>
<title>$AssemblyName$</title>
<description>Orders Messages</description>
<language>en-US</language>
</metadata>
<files>
<file src="bin\Debug\$AssemblyName$.dll" target="lib\net40" />
</files>
</package>
Your NuGet package is a pre-release based on the version since the version in your .nuspec is:
1.2.0-beta2
So you need to check the Include prerelease check box before your NuGet package will appear in the NuGet Package Manager. Without this checked only stable versions will be displayed.

Categories

Resources