Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to add reference to installed NuGet package?

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?

like image 713
user9393635 Avatar asked May 30 '18 18:05

user9393635


3 Answers

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).

like image 196
Matt Avatar answered Nov 14 '22 14:11

Matt


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.

like image 40
Mikaal Anwar Avatar answered Nov 14 '22 15:11

Mikaal Anwar


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

like image 2
Michael Ushakov Avatar answered Nov 14 '22 15:11

Michael Ushakov