Is there a way to create a nuget pack specifying dependencies of .NET libraries? - nuget

One of my libs has a dependency on System.Configuration but this .NET lib is not included by default in most of project types on Visual Studio. Is there a way to instruct NuGet Package Manager to add this .NET reference when installing my lib?

You can use the frameworkAssembly element in your package's nuspec file. This will cause NuGet to add a reference to the project when your package is installed.
<frameworkAssemblies>
<frameworkAssembly assemblyName="System.Configuration" />
</frameworkAssemblies>

Related

Automating .net framework package installation: resolving package dependency on other packages

Long story short: I am trying to automate package .net framework nuget updates in a solution via VS2019 package manager console and i can't get a package dependencies on other packages (Though i know the data is available because you can see it in the UI).
Long story long: I am trying to automate nuget installation for .net framework and i chose to use the package manager console in visual studio (If there is a preferred option i would love to hear about it). The problem i am trying to solve is this:
Problem input: Package A: version 1, has dependency on package B version > 1. version 2 has a dependency on package B version > 2. Package B: version 1. version 2 has a breaking change. Package C: version 1, has dependency on package B version > 1.
Problem I am trying to solve: I have these 3 packages referenced by the same project. I want to upgrade package A to version 2. Its dependency is package B version > 2. Running package A update to version 2 (via package manager UI or console) will update package B to version 2. Boom! - package C will detect it is missing a method it requires from B version 1 only at runtime!
What i am trying to do - Scripting the installation process prompting the user for these type of dependencies.
My problem - For that i want to get a package dependencies and i can't find the way to do it using the package manager console.
Appreciate some help :)
For that i want to get a package dependencies and i can't find the way
to do it using the package manager console.
We cannot use nuget manage console to get the package dependencies.In addition to UI viewing nuget package dependencies, we can get the dependencies from csproj file in the nuget project A. This is the related content in A.csproj file:
<ItemGroup>
<PackageReference Include="Antlr">
<Version>3.5.0.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging">
<Version>3.0.0</Version>
</PackageReference>
</ItemGroup>
Another is that you can view package dependencies is through the nuspec file which is created by command nuget spec (if you use nuspec files to enforce dependencies), which are the documentation you use before packaging the project.
Scripting the installation process prompting the user for these type
of dependencies.
Note: In view of your situation, you cannot use two versions of the nuget package in the same project which will cause version conflict.
Suggestions:
I wonder why you deleted the method required by C when you upgrade B. Basically, when we upgrade and modify the package, we will new features to it without removing the original data and methods to prevent references to other dependent packages in the project from using the corresponding methods.
So you could restore the methods required by the C package in the Package B version two.
If you make a major change to package B version2 and the steps of restoring the method are a bit complicated, I suggest you can make a copy of package B specifically for C packages.
Just rename the Package B version 1 and referenced by Package C to distinguish it from the second version of the B package.
Hope it could help you.

Force NuGet to reference a .net framework 4.0 assembly in a 4.5 project?

I have two projects in my solution--one targets .net 4.0, the other targets .net 4.5. They both reference the same NuGet package which contains both 4.0 and 4.5 binaries.
How can I get NuGet to reference the same version--4.0--in both projects?
You can exclude the folder of the framework that you don't want to use (ExcludeAssets), and edit the PackageTargetFallback in your project.
In your project's csproj file:
<PackageTargetFallback Condition="'$(TargetFramework)'=='net45'">
$(PackageTargetFallback);net40
</PackageTargetFallback >
When referencing the package:
<PackageReference Include={package-ID} Version={version} ExcludeAssets="lib/$(TargetFramework)"/>
This way the package won't bring the binaries that you don't want, and the ones you want will be compatible.
Note: Taking dlls with a different target framework is not recommended.

Updating Pre-release dependencies using nuget

I'm currently develop a set of libraries that progressively add more features.
For example, in my solution, I have a Foo project which defines some basic feature set, an additional project, Foo.Web, for web specific implementations and Foo.Web.Tokens for even more specific features. Foo.Web.Tokens depends on Foo.Web which depends on Foo.
I'm attempting to build separate nuget projects so a user only needs to reference those dependencies they need. I'm versioning the assemblies with GitVersionTask, so after build, they all get the same version number and I'm using the replacement tokens for nuget when building from a project so that the nuget packages all have the same version number.
My problem is that when I try reference a prerelease version of either Foo.Web or Foo.Web.Tokens nuget is unable to resolve the dependency on Foo. If, for example, I have published a 1.1.0.0-alhpa0001 package for each of the assemblies, when I try and update Foo.Web, nuget shows this error:
Install-Package : Unable to resolve dependency 'Foo (≥ 1.1.0.0)'.
Using the -Pre argument doesn't change this. A Foo.1.1.0-alpha0001.nupkg does exist but I feel like nuget won't resolve it because it's not a stable version, and I'm letting nuget automatically detect the dependencies from the solution using the following command:
.\.nuget\NuGet.exe pack source/Foo.Web/Foo.Web.csproj -Build -Version 1.1.0.0-alpha0001 -symbols -IncludeReferencedProjects
How do I properly allow the Foo.Web prerelease package reference the Foo prerelease package of the same version?
The IncludeReferencedProjects option seems to pull the version from the assemblyinfo.cs of the referenced project.
setting the AssemblyInformationalVersion attribute to the desired nuget package version seems to work as you want it to.
eg
[assembly: AssemblyInformationalVersion("1.1.0-alpha0001")]

Preventing automatic framework targeting of portable class libraries

I have a library published on nuget that is targeting .NET Framework 4.5
The solution looks like:
Solution
└─ Project.Net45
The nuget package looks like:
─lib
└─net45
I wanted to add a portable class library, so I modifed the solution to:
Solution
└─ Project.Net45
└─ Project.Pcl (Targets Windows Store Apps and Windows Phone 8)
I had to modify the code in the portable class library to work around things that were not compatible with those targets such as the serializable attribute and Thread.Sleep.
The problem is that the portable class library is automatically set to target .NET Framework 4.5 with the following message:
"The following frameworks will be selected automatically because they
support all of the available functionality that is portable between
the other frameworks you have selected: .NET Framework 4.5 and
higher".
and the nuget package now looks like:
─lib
└─net45
└─portable-net45+wp80+win
If I install this nuget package into a library targeting .NET Framework 4.5, I would rather the net45 library be used rather than the portable class library as the net45 library is smaller and doesn't have all the extra code needed to work around the missing features in windows phone 8 etc).
Will changing the nuget package to structure below have the desired effect :
─lib
└─net45
└─portable-wp80+win
Or am I misunderstanding the usage of portable class libraries?
NuGet looks for the best match when adding references to assemblies in the NuGet package.
If your project targets .NET 4.5 then NuGet will install the net45 assembly since it considers that the best match.
The PCL assembly will be used when your NuGet package is added to a PCL project that is compatible.
If you only had the PCL assembly in your NuGet package then NuGet would add a reference to it if you installed it into a project that targets .NET 4.5. However since you have both it will pick the net45 assembly.

Do I need to add DLL's that are marked as dependencies to the nuget lib folder

I have a package and nuspec file that I build by copying the dll for the assembly in question into a lib folder which is in the same folder as my nuspec file. This all works fine and dandy, no issues here.
I have a second package which references the first via nuget so to build it's package I followed the same process but added in a dependency element into the nuspec file. When I do my copy from release to lib it also takes the dependent dll.
Since this is marked as a dependency can I remove this from by lib folder (I want it to be downloaded via nuget, not included in the current package).
I'm not sure I got your scenario exactly, but in general I could say: Depending on how you create your NuGet packages, you might not even have to specify the dependencies. Given a Visual Studio solution with the following structure:
* Solution1
- Project1
* projectfile1.csproj
- using external libraries through NuGet
- project reference to Project2
* nuspecfile1.nuspec
- Project2
* projectfile2.csproj
* nuspecfile2.nuspec
If you run nuget pack projectfile1.csproj, any NuGet packages included in Project1 will automatically be included as dependencies in your NuGet package, even if you haven't specified the dependency in your nuspec file. These dependencies will then also include the versions of the external libraries at the time of creation of the package.
As of NuGet 2.5, there is also a new feature to automatically resolve dependencies between projects in the same solution. With v2.5 you can run the following command:
nuget pack projectfile1.csproj -IncludeReferencedProjects
This will also result in a NuGet dependency to Project2. And in case Project2 isn't exposed as a NuGet package (i.e. it has no nuspec file), the Project2's dll will be included as a file in Project1's NuGet package.
After doing some testing it turns out that adding a dependency does not require the dll to be in the lib. The dependency assumes it will be resolved by Nuget. This can be confirmed by creating a new package file via the nuget GUI and adding a few dependencies. Note how they do not show up in the lib folder after save.