How to add references and nugets in visual studio project template? - c#

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!

Related

Visual studio references show some libraries not loaded

I have been working maven projects since 2 two years.First of all, I import maven projects to eclipse then
thanks to my pom.xml some dependencies is installed and I see these dependencies on my maven dependencies folder on eclipe project explorer.It connected to my .M2 folder.
Right now , I import a .Net framework project (which is written with C#) in my visual studio program.The solution explorer (references section) show many dependencies(they gives error).Then I found these libraries then I created a folder which is called nugetPackages and I connected my nuget packages manager to that folder(tools/options/nuget package manager/package sources).I reopen my project and libraries on references section does not give any error.My question is where does that libraries names come from ? There are any file like that pom.xml.
I hope I can explain my situation.
Are the libraries (DLLs) that you want to reference in your project NuGet packages or just some framework libraries? If they are Nuget packages they should be described in packages.config file http://prntscr.com/1140xn0 for the classic .NET application (.net framework 4.5 for example) In a .Net Core applications the Nuget packages that are required are defined in the file .csproj file http://prntscr.com/11412iq. In both cases, you can download all required NuGet packages by right clicking on the .sln file and selecting Restore Nuget Packages http://prntscr.com/1140zsk This will automatically create the nugget folder and download all required Nuget packages. If you have manually added dlls you need to navigate to them and select them by right-clicking References - Add reference http://prntscr.com/114142x

Nuget Pack not including new files

I've added new models and helpers to my project, but when I run nuget pack and install the package in my project I don't see the new items in the Assembly Explorer nor can I use them. Am I missing something during the build that wouldn't include new files added to the package?
I'm using visual studio 2017 and the nuget cli
At a guess, you didn't change the package version when you changed the code and packed again. NuGet is designed such that the package id/version produces an immutable package. This means it's valid to download a package, say Newtonsoft.Json 12.0.1 from nuget.org just once, and every time you use that package in any project on the same computer you can re-use the same download, rather than having to download it every time you restore/build the project.
This causes some people problems when they're trying to test their packages. One option is to take advantage of Semantic Versioning and use 1.0.1-preview.1, 1.0.1-preview.2, so every single build of the project has a unique version number. In addition, or instead, you could use a nuget.config to set the globalPackagesFolder to a different location that gets cleared every time you change the package. If you delete the "cache", it can't reuse the old contents. But this only works if you control the machines that use the package. Once you publish the package where anyone else can use the package, you will cause problems if you change the contents, which is why nuget.org doesn't allow deleting packages, only unlisting them.
However, another possible solution is to just not use packages and simply use project references. Some people have the misconception that if you have two packages, one depends on the other, that they need to use package references to make sure NuGet dependency information flows. That's not correct. If you pack with the MSBuild pack targets (highly recommended, and the default option for SDK style projects), NuGet will always convert project references into NuGet dependencies. nuget pack will convert project references into dependencies when the other project also has its own nuspec file. When you test your project with project references, you never have to worry about immutable packages. Just pack when it's ready, but it's not needed for testing.
Maybe you have a .nuspec file (at the same level than your .csproj) that you need to edit to include new files?

Nuget Package Restore, upon editing Package.Config, in Text Editor?

When merging two projects, we're in need of Install-Package several 100 times. Doesn't Nuget support package restore, once Package.config is edited. The way it works like charm for Node JS package?
Once package.config is edited, why can't we fire Update-Package to
restore all packages?
Is it supported in the successor Dotnet CLI?
While packages.config tells NuGet which packages to download and extract (for which target framework), the process of installing package references may also modify the .csproj file. So if you update only one of these after a merge, you could find yourself trouble.
The successor of this mechanism is PackageReference (NuGet blog post), which replaces packages.config and only requires listing the referenced packages and versions (and even some additional MSBuild logic could be used by projects to manage shared versions / packages etc.). However, there is no migration tool available, so you'd need to uninstall all packages and add the package references you need. Some NuGet features also changes - for example the support fo content files (=> files copied into the project directory) was removed which may still be used by some projects (e.g. web projects assuming that jQuery or other JS libraries would be acquired in this way).

What's the difference between a dll and nuget package?

First of all, I apologize if this is a basic question. I tried looking this up, but for some reason, I got more confused. So, I decided to ask here. Is a dll file and a nuget package the same? Are they both just being referenced in the project?
When you add features to your project via a nuget package, you're just adding files to your project. It can be javascript files (like jQuery), DLLs that your project references (like Newtonsoft JSON), or a whole bunch of things (like Entity Framework or Owin/SignalR) -- anything really.
The advantage of using the nuget package system is that it tracks it all for you. It notifies you if your added packages received an update, it removes the files and unreferences them if you take the package off your project. It handles all of that for you, so you don't have to track the files that the nuget package added, place them in special folders, make sure they get copied in your builds, all that micromanaging stuff.
From the docs, https://www.nuget.org.
"What is NuGet? NuGet is the package manager for the Microsoft
development platform including .NET. The NuGet client tools provide
the ability to produce and consume packages. The NuGet Gallery is the
central package repository used by all package authors and consumers."
A package can contain one or more dlls in addition to other assets such as config files etc.
You can add libraries via reference into your project but you would not notice when they were updated.
NuGet is a Visual Studio extension that makes it easy to pull in not only libraries but components, and most importantly their configuration into your visual studio project. It will help you manage your packages installed on your project and it will notify you when the package has new version released.
Let's say I created my own DLL, I could add my own DLL by reference. However, it won't be available in NuGet until I package and publish it first to make it available at the NuGet Package Gallery.

Could not locate assembly "EntityFramework"

I've been working on this MVC 3 application on my home computer for a while now. I'm out of town on a different computer, so I got the project from my source control. This new computer didn't have MVC 3 yet, so I installed it after I copied the project to the new computer.
A lot of my dlls (like MvcContrib.dll) were missing since I didn't set Copy Local to true when I first created the project on my home computer. So I've been going in and downloading all the missing dlls and adding them to my project.
The only one I can't find is EntityFramework.dll. I can't find a download for it, and I don't see it as a .NET dll when I try to add a reference.
I'm getting this warning when I try to build:
Could not resolve this reference. Could not locate the assembly "EntityFramework"
This can also happen when you manually remove the EntityFramework reference in a project.
If you've lost the reference you can remove the entry in packages.config for EntityFramework
<packages>
<package id="EntityFramework" version="5.0.0" targetFramework="net40" />
</packages>
After removal you are able to re-install the package through the Package Manager (Manage NuGet Packages)
Download and install Framework 4.0 and it's included.
http://www.microsoft.com/download/en/details.aspx?id=17851
You need to download and install Entity Framework separately:
http://blogs.msdn.com/b/adonet/archive/2011/04/11/ef-4-1-released.aspx
If you have not - install the Nuget extension in Visual Studio.
Install ASP .NET MVC 3 Tools Update (using WebPI is easiest)
Then install the EntityFramework Package.
Other packages are likely available for items such as MvcContrib, which may be missing.
Using Nuget will not only enable you to have the packages with the source (in the packages directory), but will also help you keep things up to date. External dependencies you rely on should ideally be included with your solution so situations like yours do not occur.
As The Evil Greebo noted, you will need to go here to obtain the Visual Studio tooling. While the Nuget package will provide the code level support, the tooling is in the installer. I use both, so if there is an update to the package, it will show up in the Nuget update notices.
I resolved a similar problem myself by downloading the ASP.NET MVC Tools Update. Any chance you are just missing that on this other computer?
If EntityFramework was installed previously you can also manually add reference with browse. Select the following file in your project folder: \packages\EntityFramework.5.0.0\lib\net4x\EntityFramework.dll
Then add the following if missing in your packages.config:
<packages>
<package id="EntityFramework" version="5.0.0" targetFramework="net4x" />
</packages>

Categories