Unable to add reference to installed NuGet package?

泪湿孤枕 提交于 2020-08-24 06:30:12

问题


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?


回答1:


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.




回答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




回答3:


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:


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.

After you have deleted it, ensure that it isn't referenced any more in Visual Studio (dependencies). Then, open the Package manager and add the package (i.e. right-click on the project, select "Manage NUGET Packages...", then switch to the Browse tab, select the package and click Install).



来源:https://stackoverflow.com/questions/50611514/unable-to-add-reference-to-installed-nuget-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!