9

I created a NuGet package and I was able to successfully install it in another .NET solution. But I'm not able to add a reference to the NuGet package from the other .NET solution.

For example, the NuGet package has a class with a namespace like MyCorp.SecurityApi. I'm currently not able to add a using directive for that namespace in my other .NET solution. For example, using MyCorp.SecurityApi directive returns this compilation error:

The type or namespace 'MyCorp' could not be found

Any idea what the issue might be or how to debug it?

5
  • Make sure it's added as a reference in your solution explorer Commented May 30, 2018 at 18:44
  • thanks tj. the nuget package reference shows as installed in nuget package manager but it doesn't show up as a project reference as you alluded to. any idea what the issue might be or how to debug? Commented May 30, 2018 at 18:49
  • Right click references -> Add Reference: Then check that list for your package. It might have just not automatically checked it. Commented May 30, 2018 at 18:50
  • I just tried a manual "add reference" but I didn't see the nuget package assembly listed. it seems like there may potentially be a bigger issue with the way in which my nuget package is configured. any idea what I should look for or what might be causing this disconnect? Commented May 30, 2018 at 18:56
  • 2
    What is the target framework version of your package? Does it match the framework version of the solution/project where it won't install? How about the solution/project where it does install? Commented May 30, 2018 at 19:06

4 Answers 4

7
+400

Make sure you double check the “namespace” name with the “References” in your solution explorer, whether it exists or not. If it doesn’t you should consider reinstalling. Use the following command in Nuget Package Manager Console:

Update-Package -Id <package_name> –reinstall

Or this to restrict the re-install to a particular project only:

Update-Package <package_name> -ProjectName MyProject -reinstall

If you’re still unable to do that, try manually adding your relevant .dll to your project and see if it works properly. If it does than most probably the problem lies with the configuration of that nuget package, in which case I would recommend you to go through these docs and narrowing down the problem.

Sign up to request clarification or add additional context in comments.

Comments

2

A problem could be due to:

  • Incompatible target framework of nuget package (MyCorp.SecurityApi) and aplication that attempts to use it
  • Incompatible platform architecture (i.e. if MyCorp.SecurityApi is x64 only it can'not be used to build x86 application)
  • Visibility of some classes i.e. if class is internal it can't be used only from MyCorp.SecurityApi assembly and can'not be used from another assembly - your application).

I suggest you to check mentioned above reasons, hope it helps to you

Comments

2

I would first try to do an

Update-Package -Id <package_name> –reinstall

as explained in Mikaal's answer.

But in some cases, this could also fail because the packages folder got corrupt. Depending on the platform, it is located in different paths:

  • In .NET, this folder can be found within the project directory (typically, in the same folder where the solution file *.sln is).

  • In .NET Core, you can find it by pasting the following line into the file explorer's path (open it via WIN + E, then paste the line above in the path textbox):

    %appdata%\..\..\.nuget\packages\

There, try to find the package and delete the folder and its contents. You can also find the path if you go to dependencies in Visual Studio, Packages, right-click on the package and copy the path from the properties window. Note that you might need to close Visual Studio before deleting it, as the files might be locked.

Important: Verify that it isn't referenced any more in Visual Studio (dependencies). If it is, remove any dependencies.

Finally, open the Package manager and add the package (i.e. right-click on the project, select "Manage NUGET Packages...", switch to the Browse tab, select the package and click Install).

Comments

0

This is the first NuGet package that I've created. I figured out the issue and I'm posting this for other NuGet newbies. In "NuGet Package Explorer":

  1. Content > Add > Lib Folder
  2. Right-click "lib" folder and select "Add Existing File..."
  3. In the select file dialog, select all files from bin\obj of the source solution

My NuGet package now displays in project References after install. Does anyone else here have any additional tips to optimize this process?

4 Comments

If you are creating a NuGet package from a Visual Studio solution you can issue the 'NuGet pack' command in Package Manager Console ('Tools'->'NuGet Package Manager'->'Package Manager Console'). This is assuming you have a NuGet spec file already created. If you do not have a NuGet spec file, you will have to create that first. A template can be generated by issuing 'NuGet spec' in the Package Manager console. NuGet pack will automagically create the necessary folder structures for the NuGet package itself.
thanks barry. what do I need to install or configure in order to exec 'NuGet spec' in the PM console? the cmd does not seem to be available by default in VS2017 and I tried installing NuGet.VisualStudio hoping that would help but I still get the same error in the PM console: "Nuget : The term 'NuGet' is not recognized as the name of a cmdlet, function, script file, or operable program."
So in the Package Manager console you can call command line tools, like DOS commands. If you intend on calling 'NuGet spec', it is the same as calling 'NuGet.exe spec'. This means that the executable NuGet.exe will need to be in the current working directory or in the system path. You can also call it using the fully qualified path too - as in 'c:\whatever\mypath\NuGet.exe spec'. You may need to search your system for a copy of NuGet.exe. I typically seek out the newest version from Microsoft and copy to a well known location for these efforts to simplify finding the program...
You would think Microsoft would install NuGet.exe in the system path, or at least update the path to include NuGet.exe, but alas they do not. This means sometimes the NuGet.exe cannot be found in the Package Manager console when the working directory is that of your current project. You can edit your system path variable to include the folder where NuGet.exe resides to have it available in Package Manager Console for any project. Beware, Microsoft likes to download many copies of the NuGet.exe program, and each may be a different version - so caution...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.