简体   繁体   English

无法添加对已安装 NuGet 包的引用?

[英]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.我创建了一个 NuGet 包,并且能够在另一个 .NET 解决方案中成功安装它。 But I'm not able to add a reference to the NuGet package from the other .NET solution.但我无法从其他 .NET 解决方案中添加对 NuGet 包的引用。

For example, the NuGet package has a class with a namespace like MyCorp.SecurityApi .例如,NuGet 包有一个类,其命名空间类似于MyCorp.SecurityApi I'm currently not able to add a using directive for that namespace in my other .NET solution.我目前无法在其他 .NET 解决方案中为该命名空间添加 using 指令。 For example, using MyCorp.SecurityApi directive returns this compilation error:例如, using MyCorp.SecurityApi指令返回此编译错误:

The type or namespace 'MyCorp' could not be found找不到类型或命名空间“MyCorp”

Any idea what the issue might be or how to debug it?知道问题可能是什么或如何调试吗?

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:在 Nuget 包管理器控制台中使用以下命令:

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.如果您仍然无法做到这一点,请尝试将相关的 .dll 手动添加到您的项目中,看看它是否可以正常工作。 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.如果确实如此,则问题很可能出在该 nuget 包的配置上,在这种情况下,我建议您阅读这些文档并缩小问题的范围。

I would first try to do an我会首先尝试做一个

Update-Package -Id <package_name> –reinstall

as explained in Mikaal's answer.如 Mikaal 的回答中所述。

But in some cases, this could also fail because the packages folder got corrupt.但在某些情况下,这也可能会失败,因为packages文件夹已损坏。 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)..NET 中,可以在项目目录中找到此文件夹(通常,在解决方案文件*.sln所在的文件夹中)。

  • 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):.NET Core 中,您可以通过将以下行粘贴到文件资源管理器的路径中来找到它(通过WIN + E打开它,然后将上面的行粘贴到路径文本框中):

    %appdata%\\..\\..\\.nuget\\packages\\ %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.如果转到 Visual Studio 中的依赖项,包,右键单击包并从属性窗口复制路径,也可以找到路径。 Note that you might need to close Visual Studio before deleting it, as the files might be locked.请注意,您可能需要在删除之前关闭 Visual Studio,因为文件可能已被锁定。

Important: Verify that it isn't referenced any more in Visual Studio (dependencies).重要提示:验证它在 Visual Studio(依赖项)中不再被引用。 If it is, remove any dependencies.如果是,请删除所有依赖项。

Finally, open the Package manager and add the package (ie right-click on the project, select "Manage NUGET Packages...", switch to the Browse tab, select the package and click Install).最后,打开包管理器并添加包(即右键单击项目,选择“管理 NUGET 包...”,切换到浏览选项卡,选择包并单击安装)。

A problem could be due to:问题可能是由于:

  • Incompatible target framework of nuget package (MyCorp.SecurityApi) and aplication that attempts to use it nuget 包 (MyCorp.SecurityApi) 和尝试使用它的应用程序的目标框架不兼容
  • Incompatible platform architecture (ie if MyCorp.SecurityApi is x64 only it can'not be used to build x86 application)不兼容的平台架构(即如果 MyCorp.SecurityApi 仅是 x64,则不能用于构建 x86 应用程序)
  • Visibility of some classes ie if class is internal it can't be used only from MyCorp.SecurityApi assembly and can'not be used from another assembly - your application).某些类的可见性,即如果类是内部类,则它不能仅用于 MyCorp.SecurityApi 程序集,也不能用于其他程序集 - 您的应用程序)。

I suggest you to check mentioned above reasons, hope it helps to you建议您检查上述原因,希望对您有所帮助

This is the first NuGet package that I've created.这是我创建的第一个 NuGet 包。 I figured out the issue and I'm posting this for other NuGet newbies.我想出了这个问题,我正在为其他 NuGet 新手发布这个。 In "NuGet Package Explorer":在“NuGet 包资源管理器”中:

  1. Content > Add > Lib Folder内容 > 添加 > Lib 文件夹
  2. Right-click "lib" folder and select "Add Existing File..."右键单击“lib”文件夹并选择“添加现有文件...”
  3. In the select file dialog, select all files from bin\\obj of the source solution在选择文件对话框中,从源解决方案的 bin\\obj 中选择所有文件

My NuGet package now displays in project References after install.安装后,我的 NuGet 包现在显示在项目引用中。 Does anyone else here have any additional tips to optimize this process?这里有没有其他人有任何其他提示来优化这个过程?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM