How to copy Nuget Package Dependencies - c#

We are currently refactoring an old win forms app which uses a framework as referenced project across the whole application. I am fairly new to nuget and .net so I hope you don't mind my newbiew question.
We created a nuget package from the csproj (Syncing.nupgk). That package references other packages like Newtonsoft or Consul. When I build the project, my syncing.dll gets copied to the bin folder of the win forms project, but other dlls (newtonsoft.dll, consul.dll) are copied outside in a packages folder with a structure lib\net45 etc.
How I can tell visual studio that I also need the newtonsoft.dll in the bin folder of the project as it is a dependencie of my syncing.dll
thx!

I usually use nuspec file for this purpose. In your case, you would have Syncing.csproj.nuspec which looks something like this:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>Syncing</id>
<authors></authors>
<owners></owners>
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<licenseUrl></licenseUrl>
<projectUrl></projectUrl>
<iconUrl></iconUrl>
<description></description>
<language></language>
<tags></tags>
<dependencies>
<group targetFramework=".NETStandard2.0">
<dependency id="Newtonsoft.Json" version="12.0.2" exclude="Build,Analyzers" />
...
</group>
</dependencies>
</metadata>
</package>
You can look at these two links for more info:
- https://learn.microsoft.com/en-us/nuget/reference/nuspec
- This is how I'm using .nuspec file to publish the instrumentation library on https://github.com/borkke/opentracing-csharp-mongo/blob/master/src/OpenTracing.Contrib.Mongo/OpenTracing.Contrib.Mongo.csproj.nuspecnuget

Related

How do i remove dll(s) from Nuget package?

Im using Visual Studio 2022 and created a Nuget Package using this article
https://arsenshnurkov.github.io/gentoo-mono-handbook/building-nupkg.htm
I run nuget pack and i see the nupg file and upload it to our Azure Artifacts. Below is the spec file
<package >
<metadata>
<id>myProject.csproj</id>
<version>1.0.0</version>
<authors>user</authors>
<owners>user</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Test package 1</description>
<releaseNotes>Summary of changes.</releaseNotes>
<copyright>Copyright 2023</copyright>
<tags>Tag test</tags>
</metadata>
</package>
I noticed when i install this pack into a test application it includes some dlls that are not required for the end project to run.
After some research there are suggestions to change the .csproj to exclude it from Nuget when packing following this article https://www.jocheojeda.com/2019/07/22/how-to-exclude-package-dependencies-in-a-nuget-package/
but he writes the exact question i have in mind but no example of how to do this. In the article he quotes
the answer depend on how now you create your NuGet package, in this case, I’m going to focus my answer on excluding the dependency in a package created by the info in the csproj file (there is a different approach if you use the nuspec file).
I dont want to amend the csproj file but just the nuspec file. How could i leave out files that i dont want to have bundled with my package by amending the nuspec file?
In order to include/exclude files from nuget packaging, you can add the section to nuspec config file as described here.
Following your example, the files would exclude log files from your package. Using wildcards, you can exclude any other file/dll:
<package>
<metadata>
<id>myProject.csproj</id>
<version>1.0.0</version>
<authors>user</authors>
<owners>user</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Test package 1</description>
<releaseNotes>Summary of changes.</releaseNotes>
<copyright>Copyright 2023</copyright>
<tags>Tag test</tags>
</metadata>
<files>
<file src="*.*" exclude="*.log" />
</files>
</package>

Create Nuget package for different platforms, architectures and .net versions

I have a C# 7.3 project Foo which I want to publish as a Nuget package. I want my package be available for different architectures (x86 & x64), different platforms (Windows & Linux) and different .Net versions (4.8 framework & 5.0).
Foo C# code itself does not contain any architecture-specific code, it consists only of pure C# 7.3. But this project uses my custom platform-specific .dll and .so on Linux (say ExternalLib.dll / ExternalLib.dll.so). I have one of them for each Windows x64, Windows x86 and etc.
I know that in the NuGet package you place runtime-specific components in /runtimes folder structure like /runtimes/win10-x64/native/ExternalLib.dll. As documentation says, these will be used only for runtime and I need to specify compile-time references.
I built my Foo project in Any CPU configuration for each net4.7 and net5.0 and placed each Foo.dll in /lib folder. So my final folder structure for the module is
Project.nuspec
/lib
/net4.7/Foo.dll
/net5.0/Foo.dll
/runtimes
/win10-x64/native/ExternalLib.dll
/win10-x86/native/ExternalLib.dll
/linux-x64/native/ExternalLib.dll.so
Project.nuspec:
<?xml version="1.0" encoding="utf-8"?>
<package>
<metadata>
/* */
<dependencies>
<group targetFramework=".NETFramework4.8" />
<group targetFramework="net5.0" />
</dependencies>
</metadata>
</package>
I build a package using the nuspec pack Project.nuspec command. There are no errors from it but when I add this package to any .net project and try to use any functionality from it, VS can not find anything from my library.
What am I doing wrong?
I finally did it. After a research, this is my approach:
If you write a C# library, use only .NetStandard, so both .Net and .NetFramework applications can use your library. I used .NetStandard2.0 which can be used from both .Net5.0 and .NetFramework4.8. See this for all compatible versions.
Clear NuGet cache
Provide xml documentation (optional)
Final .nuspec file:
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
...
<dependencies>
<group targetFramework="netstandard2.0" />
</dependencies>
</metadata>
<files>
<!--Path to documentaion and AnyCPU builded dll-->
<file src="Doc\Api.xml" target="lib\netstandard2.0"/>
<file src="bin\x86\Release\netstandard2.0\Foo.dll" target="lib\netstandard2.0"/>
<!--Path to x86 builded ExternalLib and Foo-->
<file src="build\x86-Release\ExternalLib.dll" target="runtimes\win-x86\native" />
<file src="bin\x86\Release\netstandard2.0\Foo.dll" target="runtimes\win-x86\native" />
<!--Path to x64 builded ExternalLib and Foo-->
<file src="build\x64-Release\ExternalLib.dll" target="runtimes\win-x64\native" />
<file src="bin\x64\Release\netstandard2.0\Foo.dll" target="runtimes\win-x64\native" />
<!--Path to x64 builded ExternalLib(for linux) and Foo(same as windows)-->
<file src="build\x64-Release\ExternalLib.so" target="runtimes\linux-x64\native\ExternalLib.dll.so" />
<file src="bin\x64\Release\netstandard2.0\Foo.dll" target="runtimes\linux-x64\native" />
</files>
</package>

c# nuget package doesn't show up in references in VS

I want to create a C# app that does things with the powershell. I found many solutions on the internet how to do that like this. The most answeres to this use the Powershell class from 'Microsoft.WSMan.Runtime'. Now when I search this package in nuget and install it, it doesnt show up in the references list in visual studio and also the using statement or the 'quick fix' on a PowerShell object doesnt find it.
Did I install something wrong or do I need something else too?
Edit for more infos:
.net Version 4.6.1
Tried 'Microsoft.WSMan.Runtime'v 7.0.0 and 7.0.3
'Microsoft.WSMan.Runtime' is downloaded and avaible at './packages/Microsoft.WSMan.Runtime.7.0.0'
'packages.config' does contain the entry for 'Microsoft.WSMan.Runtime'.
I found the assembly within the package targets .net core 3.1 instead of .net framework 4.6.1. So you can't see the reference in solution explorer.
It's by design of the package author, you can download the package manually, rename the name from xx.nupkg to xx.zip to check the content of the package.
The structure of the package:
And the content of the Microsoft.WSMan.Runtime.nuspec file:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
...
<dependencies>
<group targetFramework=".NETCoreApp3.1" />
</dependencies>
<contentFiles>
<files include="**/*" buildAction="None" copyToOutput="true" flatten="false" />
</contentFiles>
</metadata>
</package>

Created nuget package's dll can not be accessable from package's loaded project

I want to create a nuget package of my .net core class library.
I copied the nuget.exe file to the root folder of my Solution
Run nuget spec and Solution.nuspec file created. I modified it as below:
<?xml version="1.0"?>
<package >
<metadata>
<id>Solution</id>
<version>1.0.0</version>
<authors>cc Team</authors>
<owners>cc</owners>
<licenseUrl></licenseUrl>
<projectUrl></projectUrl>
<iconUrl></iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description></description>
<releaseNotes></releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>c# .net</tags>
<dependencies>
<dependency id="NETStandard.Library" version="1.6.1" />
</dependencies>
<references>
<reference file="Solution.dll" />
</references>
</metadata>
<files>
<file src="Solution.dll" target="lib\netstandard1.6\Solution.dll"/>
file's node src value outpupath of my solution. (bin\Release\netstandard1.6\Solution.dll)
Run nuget pack Solution.nuspec and Solution.nupkg file created.
I put it to my package source.
Create a new .net core class library and select my Solution nuget package from local package source. It added to my project. There is no error or warning. But When I try to access a class from my Solution nuget package, it couldn't be found.I dowloaded 'Nuget Package Explorer' and opened my Solution.nupkg. Icould see Solution.dll and Solution.pdb file under lib --> netstandart1.6.
Then I searched about the error and found this :
"scripts": {
"postcompile": [
"dotnet pack --no-build --configuration %compile:Configuration%"
]
}
After compile my Solution project it creates Solution.1.0.0-0.nupkg file and Solution.1.0.0-0.symbols. I put it to my local package source but these packages are not listed in the 'Nuget Package Manager'.
Do you have any idea?
My experience is creating package from solution sometimes crappy. Creating the package from the project file could solve the problem.
After I clean 'C:\Users\XXX.nuget\packages\Solution' folder and add package again fix my problem.
Because there is bad formatted nutget package which I tried before.

*.symbols.nupkg file generated when publishing a .*nupkg file

I'm using TFS2012 to publish a nuget package of a C# Visual Studio 2012 project. In this solution are many projects which I can successfully publish to the specific repository. For one project, which I added yesterday, the publisher creates the *.nupkg file as usual, but also creates a *.symbols.nupkg as well.
I cannot figure out, which setting in the project may cause this issue.
*.nuspec-file of the project:
<?xml version="1.0"?>
<package>
<metadata>
<id>xxx.xxx.xxx</id>
<version>0.1.1</version>
<title></title>
<authors>xxx</authors>
<owners>xxx</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description></description>
<releaseNotes>
- first release
</releaseNotes>
<copyright>Copyright 2016</copyright>
</metadata>
<files>
<file src="bin\$configuration$\$id$.pdb" target="lib\net40\" />
</files>
</package>
The result in the repository looks like this:
File 1: xxx.xxx.xxx.0.1.1.nupkg
File 2: xxx.xxx.xxx.0.1.1.symbols.nupkg
But it should look like this:
File 1: xxx.xxx.xxx.0.1.1.nupkg
EDIT 1:
I use the external Tools of Visual Studio 2012 to publish to the repository:
Command: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe
Arguments: $(ProjectDir)$(ProjectFileName) /t:Publish /p:xxxPackageSource=\\tfs2012\Repository
EDIT 2:
Furthemore I tried to build the package locally and published it to a local directory. Same result

Categories

Resources