I have a .NET 4.5.2 library using Visual Studio 2017 (v 15.9.7).
In my library project (i.e. csproj), I've added a Nuget dependency on CEFSharp for WPF (which itself includes other dependencies).
I've converted my library Nuget references to use the newer "PackageReference" mode.
When running nuget pack from the command line, I expect that the created *.nupkg would automatically include references to the CEFSharp components that my library references.
It doesn't look like this is the case. When I open the nupkg as a zip, I can't see any references to CEFSharp.
Do I really need to make my library's nuspec file redundantly declare the CEFSharp dependency even though my csproj already essentially defines the CEFSharp dependency?
If you're generating your nuget from a nuspec file then yes, this file must declare the dependencies to other nugets. For instance:
<dependencies>
<group>
<dependency id="CefSharp.Wpf" version="71.0.0" exclude="Build,Analyzers" />
</group>
</dependencies>
Related
I have a project that is called framework and I have modules projects which depends on this framework project.
The problem is: Some developers on my team will have this framework code and others won't. I want to know if there is a way that if Visual Studio doesn't find this reference, it will automatically get this frameworks libs from nuget package.
You could use Conditions to include the project only if it does exist:
<ProjectReference Include="..\..\Framework\Framework.csproj" Condition="Exists('..\..\Framework\Framework.csproj')" />
<PackageReference Include="MyCompany.Framework" Version="1.0.0" Condition="!Exists('..\..\Framework\Framework.csproj')" />
You have two options to reference other projects:
Project References
This is typically done using a monorepo approach and most times all projects are part of the same solution.
If you want to reference projects from other repositories, Git sub modules may be an option https://git-scm.com/book/en/v2/Git-Tools-Submodules
Project references look like this in your my-project.csproj file:
<ProjectReference Include="..\..\Modules\Core\Core.csproj" />
NuGet Packages
Here you build your framework and publish it as NuGet package. Then you reference the package (not the project) when needed and Visual Studio will download the specified version from your NuGet repository.
Two options to setup your own NuGet repository:
https://learn.microsoft.com/en-us/azure/devops/artifacts/get-started-nuget?view=azure-devops
https://www.sonatype.com/nexus-repository-oss
You can also use NuGet packages from local file system How do I install a NuGet package .nupkg file locally? (personally I'd not recommend this for most scenarios)
Reference NuGet packages in your my-project.csproj file:
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
Or using visual studios package manager: https://learn.microsoft.com/en-us/nuget/quickstart/install-and-use-a-package-in-visual-studio
Note that you have to add your NuGet repository feed first: https://learn.microsoft.com/en-us/azure/devops/artifacts/nuget/consume?view=azure-devops
See also
Is it possible to reference a project that exists in the solution and use NuGet package reference as fall-back if not in .NET Core?
I have came across this line of code in a code and its quite confusing
One of my client send a dependancy file that contains
<ItemGroup>
<PackageReference Include="xyzrefrence" Version="1.3.0" />
and said it is console application. I created same kind of application but within packages.config
I found this thing
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="xyz" version="1.7.7.7" targetFramework="net452" />
My question is that where the dependacy file located with the client setting(First settings)
The reference (<ItemGroup><PackageReference.....) to the dependency can be
found by editing the Visual Studio project file. *.csproj
The dependency file itself can be found in your project's /bin/debug or /bin/release folders.
Where is PackageReference located in console application C#
There are two nuget mamangement format(Packages.config and PackageReference) to intall nuget packages.
In fact, PackageReference is a new nuget management format for new sdk projects(net standard and net core) since VS2017 while Packages.config is an old tranitional nuget management format for net framework projects.
However, you should note that for traditional framework projects, Microsoft made a concession to use the new SDK's pacakgeReference format, but there are still various compatibility issues.--------(net frameowork projects can use both of them while net core/net standard projects can only use PackageReference).
If you use a net framework project, you can change these two format before you install nuget packages at the beginning by Tools-->Options-->NuGet Package Manager-->General-->Package Management.
And you should specifiy this format before you install the first nuget package at the beginning and when you specify this format, the nugets you install later will use this method by default and cannot be changed.
My question is that where the dependacy file located with the client
setting(First settings)
1) If you use a net framework console project with PackageReference, l am afraid that you cannnot see the depenencies of the nuget. The old sdk projects with PackageReference does not support showing the depenencies of the nuget packages due to several compatibility issues.
2) If you use a net core console project, you can see the dependencies in the Solution Explorer and the latest new sdk projects does support this. It has a new behavior that you can see every nuget package's depenencies under its branch in the Soluton Explorer.
Besides, since you use a framework project with packages.config, you can only see all of them(the premise is that this nuget package has dependencies.) in the packages.config file or in the xxxx.csproj file but it cannot subdivide dependencies for every nuget package.
In additon, if you still want to show the depenencies of the net framework projects with PackageReference, l suggest you could post a feature request in our User Voice forum(DC)-suggest a feature to get Micorosft's attention.
Is there any instructions to add nugets and references in project template?
The best way is to add nuget without version.
Nugets will be added not from nuget.org but from internal corporate resource.
What I mean. In the SolutionExplorer picture red boxes are references from nugets and blue boxes are references were included using Add reference.
After I used Export template I don't see any references in zip folder.
And when I create new project with my template I see errors on nuget references and no errors on references from PC(but their absolute pathes are not in the project so I can't distribute template to other developers).
Specific instructions for creating a project template with nuget packagaes are documented at nuget.org. Unfortunately, it is not possible to add a nuget package to a project template without a version number:
The wizard supports multiple elements. Both the id and
version attributes are required. An important consequence of this is
that a specific version of a package will be installed even if a newer
version is available in the online package feed.
The reason for this behavior is that a future version of a package
might introduce a change that is not compatible with the project/item
template. The choice to upgrade the package to the latest version
using NuGet is left to the developer who is in the best position to
assume the risks of upgrading the package to the latest version.
vstemplate (this is required to invoke package download at template inflation):
<WizardExtension>
<Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
<FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName>
</WizardExtension>
package list:
<WizardData>
<packages>
<package id="jQuery" version="1.6.2" />
</packages>
</WizardData>
There are additional options documented on the nuget site linked above, such as creating a VSIX template.
Regarding nugets, use package manager console and add (for eg: Install-Package Newtonsoft.Json) and then click enter.
For the references, according to the image, it may refer to the GAC and not the local bin folder of the solution. The best way is always to maintain the references in the Bin/Reference folder and then refer from it.
In this case, you need to import the dll again.
Hope it helps!
I'm providing a C#/.NET class library to clients within my company as a NuGet package. My library depends upon some other 3rd-party libraries (e.g. Newtonsoft's excellent Json.NET).
I'm assuming that the standard way NuGet works is that my package would only include references to other NuGet packages I depend upon. The client's Developer Studio will automatically download them when they install my NuGet package in their project.
Question #1:
Can I be certain that Developer Studio will download the versions of
those NuGet packages that I was building against and not the 'latest'
versions?
Question #2:
Will it cause problems if their project also uses a 3rd-party library
that I'm using (like Json.NET), especially if they are using a different version? Does this 'just work', or do I need to do something about this?
Apologies if this is spelled out somewhere, but I've not been able to find specific answers to these questions.
That's clearly laid out in the Nuspec Reference
Specifically:
Specifying Dependencies
Starting from version 2.0, package dependencies can be specified to vary according to the framework profile of the target project. The element contains a set of elements. Each group contains zero or more element and a target framework attribute. All dependencies inside a group are installed together if the target framework is compatible with the project's framework profile.
<dependencies>
<group>
<dependency id="RouteMagic" version="1.1.0" />
</group>
<group targetFramework="net40">
<dependency id="jQuery" />
<dependency id="WebActivator" />
</group>
<group targetFramework="sl30">
</group>
</dependencies>
Where version is:
The range of versions acceptable as a dependency. Typically this is
just a version number which represents a minimum version. However a
more explicit version range syntax is supported.
(update: updated references to more recent version of nuget)
I am trying to create a Nuget package from my project following this guide http://docs.nuget.org/docs/creating-packages/creating-and-publishing-a-package
I have successfully created a nuspec and nupkg. My project contains a dependency to Json.NET which I installed via Nuget. However, I want to specify a specific version of Json.NET to use, version 4.0.7. I added the below to my nuspec:
<dependencies>
<dependency id="Newtonsoft.Json" version="[4.0.7]" />
</dependencies>
When I run nuget pack it seems to detect I have a packages.config
Using 'MyProject.nuspec' for metadata.
Found packages.config. Using packages listed as dependencies.
This seems to completely ignore my defined dependency in the nuspec as installing the Nuget package lists the dependencies as >= 4.0.7 which pulls in the latest version 4.0.8.
How can I stop this or preferably keep Nuget pulling in dependencies from the packages.config but allow me to overwrite specific dependencies?
I hit the same issue. You need to define an exact version like this
<dependencies>
<dependency id="Newtonsoft.Json" version="[4.0.7,4.0.7]" />
</dependencies>
So that will ensure when the project pulls in the dependencies it will be = 4.0.7 not >= 4.0.7
The way you specified your version is correct; as shown in our versioning docs, [1.0] means 'version == 1.0'. The behavior you're describing would be a bug, but I couldn't reproduce the bug. What I did:
Created a class library
Added Json.NET via NuGet (it installed 4.0.8)
Exec'd nuget spec
Added <dependencies><dependency id="Newtonsoft.Json" version="[4.0.7]" /> to the .nuspec
Exec'd nuget pack
Opened the package in Package Explorer; it shows the dependency as '= 4.0.7'
Installed my package in a new project; it shows the dependency as '= 4.0.7' and installs 4.0.7
Perhaps you aren't using the latest version of nuget.exe or the NuGet Visual Studio extension? When you say it "lists the dependency", where are you seeing that? When your package is installed, in Package Explorer, or somewhere else?