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

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>

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>

How to copy Nuget Package Dependencies

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

How to share source code via NuGet packages for use in .NET Core projects

I want to make small pieces of source code (e.g. helper classes) available for use in .NET Core projects (.csproj).
At this point I packaged the source code with NuGet in many different ways according to different blog posts and the official nuget docs. I used a nuspec file to control where my source files will end up in the nuget package, e.g.:
<files>
<file src="*.cs" target="content/LruCache" />
<file src="*.cs" target="contentFiles/cs/any/LruCache" />
</files>
I did not include any msbuild targets file or install script.
Whenever I install the NuGet package into a .NET Core project (https://learn.microsoft.com/en-us/dotnet/core/tools/csproj) I simply don't get anything there. No source files will be included into my project. I tried different settings for the <PackageReference/> node in the .csproj (PrivateAssets, etc.) without success.
Is it meant to be possible at all? If so, how should it be done?
Background:
The reason for doing this is some kind of diamond problem where we have projects B and C both using helper class A and a third project D using B and C.
In this situation I don't want to deal with assembly version conflicts when different (incompatible) versions of A have been used in B and C.
Is it meant to be possible at all? If so, how should it be done?
The answer is yes. Since you test project type is .net core. You should use contentFiles instead of content. content is used for packages.config. Check the Using the contentFiles element for content files and blog NuGet ContentFiles Demystified for more details.
So your .nuspec file should be:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MyTestCore</id>
<version>5.0.0</version>
<authors>TestContentFile</authors>
<owners>TestContentFile</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package Description</description>
<contentFiles>
<files include="any/any/Test.cs" buildAction="content" flatten="true" copyToOutput="true"/>
</contentFiles>
</metadata>
<files>
<file src="contentFiles/any/any/Test.cs" target="contentFiles/any/any/LruCache" />
</files>
</package>
The nuget package should look like:
Note: When you create a new package, do not forgot to remove the nuget cache for this package in the C:\Users\<UserName>\.nuget\packages folder, otherwise, it always install the old package.
With this method, the source files will be included into your project.
Hope this helps.

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.

Creating a basic NuGet Package

I have a small c# library that wraps Dapper.net and I want to create a nuget package for this library. I've created a folder that contains the following:
Nuget-Package\
Nuget-Package\Package.nuspec
Nuget-Package\lib\
Nuget-Package\lib\DapperWrapper.dll
Here's the nuspec
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>GoDaddy.Data</id>
<version>1.0.0</version>
<authors>Owner Name</authors>
<owners>Owner Name</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>I provide a fast and slim layer between your code and stored procedures that your application needs to use. This layer provides more straight-forward access to procs than straight ADO and less overhead than using Entity or Linq. It currently uses Dapper.Net to do some of this work.</description>
<releaseNotes>Initial Release</releaseNotes>
<copyright>Copyright 2014</copyright>
<tags>ADO Dapper Proc "Stored Procedure"</tags>
<dependencies>
<dependency id="Dapper" version="1.13" />
</dependencies>
</metadata>
</package>
I then copy the resulting DapperWrapper1.0.0.0.nupkg over to my local nuget server.
After this I attempt to install it on a console application. I open a simple console application right click on references and say manage nuget packages. I find and select my package and select install and get the following error:
Attempting to resolve dependency 'Dapper (≥ 1.13)'.
External packages cannot depend on packages that target projects.
What am I doing wrong here?
Seems too simple to fail and yet I cant seem to find a good explanation of this error anywhere.
The answer can be found here.
http://www.marcusoft.net/2011/12/creating-tools-only-nuget-package.html
Apparently nuget wont resolve dependencies unless you have both "lib" and "content" folders even if you're not using them.
Wow that's a bug IMO.
See this nuget workitem: http://nuget.codeplex.com/workitem/595
You could try adding a files section to your nuget package, referencing the files in your project.

Categories

Resources