ChatGPT解决这个技术问题 Extra ChatGPT

Best way to add NuGet packages as project references in VS2012?

Ok. Really quick question, I'm probably just being thick.

If you right-click on a projects 'references' folder then 'Manage NuGet Packages' you can then install packages and they are automatically referenced by the project you clicked on. But then I want to add the same reference to other projects in the same solution. If you repeat previous but click on another project the only option is to uninstall the package (since it's already installed), what is the proper way to add an existing (already installed) NuGet package as a project reference? Am I to add the reference using the normal dialog and 'browse' to the dll in the packages folder? Use one method to add the first reference then another for subsequent references? That doesn't seem right.


D
Dom

You can use Manage NuGet packages for Solution... by:

right-clicking on solution

Tools > Library Package Manager > Manage NuGet packages for Solution

https://i.stack.imgur.com/0z8kw.png


So to install/remove packages from the system you use the project level context menu, and to add/remove references from a project you use the solution level context menu. Right. Thanks for your answer. Any idea exactly what causes a project to be checked in the 'Select Projects' dialog, I have a project that is checked but doesn't have a reference to the package and when I un-check it, it just comes back.
k
kristianp

The other answer here didn't help me. Here's what I did, I don't know any other way to do it. I'm using VS 2013.

I installed the package in the Package Manager Console, e.g. PM> Install-Package Newtonsoft.Json

In the Solution Explorer, right click references, select Add Reference

Click Browse, and navigate to the dll in the packages directory under the folder of the solution.

This adds a relative path for the reference to the project, so it should work with other people getting the code from source control into different directories.


There is one problem with this approach: when in future you'll want to update this package, Nuget won't handle this in your project automatically. As a result you'll end up with different projects referencing different versions of the same package. And this will lead to exceptions in runtime. So it's preferably to add references through Nuget package manager and not manually.
J
Jan W

I had the same issue. The solution posted by kristianp has worked as expected, it can be a bit cumbersome however, when dealing with lots of packages. The solution, that worked for me was just to edit the myProjectName.csproj file in the notepad, and just to copy-paste references from another project like so:

...

<ItemGroup>
    <Reference Include="Microsoft.AspNet.SignalR.Core">
      <HintPath>..\packages\Microsoft.AspNet.SignalR.Core.2.1.2\lib\net45\Microsoft.AspNet.SignalR.Core.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.AspNet.SignalR.SystemWeb">
      <HintPath>..\packages\Microsoft.AspNet.SignalR.SystemWeb.2.1.2\lib\net45\Microsoft.AspNet.SignalR.SystemWeb.dll</HintPath>
    </Reference>

    ...

Hope this helps someone. It saved me a lot of clicking :)


D
Dr. Ogden Wernstrom

Nowadays I use the Package Manager Console (View->Other Windows->Package Manager Console).

Chances are all you'll ever need are the following two commands:

install-package <package-name> [-version <version-number>] [-project <project-name]

and

update-package <package-name> [-reinstall] [-version <version-number>] [-project <project-name>]

where

Parts within square brackets [ ] are optional.

'install-package' will install to the specified project, or if not specified the one selected in the 'Default Project' drop down at the top of the command window.

'update-package' will apply changes to every project unless a project is specified.

'-reinstall' means uninstall then install the package again at the same version number.

At first I thought the behaviour of these commands was a little weird, but with experience I see that they are most useful like this. Although personally I would ditch the 'Default Project' drop down entirely.