即使没有对包的引用,也需要 Nuget 包 [英] Nuget package is required even though there is no reference to the package

查看:19
本文介绍了即使没有对包的引用,也需要 Nuget 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的解决方案中,我有一个包含一堆相关项目的顶级项目.其中一个依赖项目需要引用 Nest nuget 包.因此,在解决方案的 nuget 包配置中,我仅在引用它的项目上安装了 Nest.

In my solution I have a top level project with a bunch of dependent projects. One of the dependent projects has a required reference to the Nest nuget package. So in our nuget package config for the solution, I have Nest installed only on the project that references it.

顶级项目引用了这个依赖项目,但没有引用 Nest 命名空间.它构建时没有警告或错误,但是,在运行时,我有一个问题说

The top level project has a reference to this dependent project, but no references to the Nest namespace. It builds without warnings or errors, however, in runtime, I have an issue saying

Could not load file or assembly 'Nest, Version=7.0.0.0, Culture=neutral, PublicKeyToken=96c599bbe3e70f5d' or one of its dependencies. The system cannot find the file specified.

当我在我的顶级项目中手动安装 Nest 包时,它毫无问题地成功了.

When I manually install the Nest package on my top level project, it succeeds without issue.

为什么会出现这种情况?不应该在依赖项目上安装 Nest 包来解决这个依赖吗?

Why does this occur? Shouldn't installing the Nest package on the dependent project resolve this dependency?

推荐答案

其中一个依赖项目需要对 Nest 的引用nuget 包.

One of the dependent projects has a required reference to the Nest nuget package.

你在依赖项目中调用了Nest的函数吗?如果 VS 发现依赖项目实际上并没有调用(需要)代码中的程序集,则 VS 不会将依赖项目的程序集复制到顶级项目中.

Did you call Nest's function in the dependent project? VS will not copy the dependent project's assembly into top-level project if it finds the dependent project doesn't actually call(need) the assembly in code.

为什么会出现这种情况?不应该在上面安装 Nest 包依赖项目解决这个依赖?

Why does this occur? Shouldn't installing the Nest package on the dependent project resolve this dependency?

我不确定问题中可用信息的原因,许多因素都会导致奇怪的行为,有时 VS 版本也会影响它...

I'm not sure the cause of the issue with info available in your question, many factors can cause the strange behavior and sometimes VS version would also affect it...

假设您有一个顶级项目 A 并且它依赖于使用 Project Reference 的项目 B.

Assuming you have a top-level project A and it depends on project B using Project Reference.

1.如果它们都针对 .net framework,请确保它们使用相同的方式来管理 nuget 包.(都使用 packages.config 或两者都使用 PackageReference)

1.If both them targets .net framework, please make sure they use the same way to manager nuget packages.(Both using packages.config or both using PackageReference)

2.如果A.net framework项目而B.net standard项目,请使确保 A 也使用 PackageReference 格式来管理 nuget 包.

2.If A is .net framework project while B is .net standard project, please make sure the A is also using PackageReference format to manage nuget packages.

原因 .net 标准(新 SDK 格式)使用 PackageReference 包,如果 A 使用 Packages.config并参考 B,构建系统会对构建过程中的不同 nuget 格式感到困惑.我们不会找到从B's输出文件夹复制到A's输出文件夹的Nest.dll.

Cause .net standard(new SDK format) uses PackageReference packages, if A uses Packages.config and reference B, the build system will be confused about the different nuget formats in the build process. And we won't find the Nest.dll copied from B's output folder to A's output folder.

对于这种情况,尝试将 PackageReference 添加到顶级 A 的项目文件中.(xx.csproj) 它将确保 A 和 B 都可以恢复为PackageReference 样式.

For this situation, try adding <RestoreProjectStyle>PackageReference</RestoreProjectStyle> into top-level A's project file.(xx.csproj) It will make sure both A and B can be restored as PackageReference style.

3.如果A是带有packageReference.net framework,并且B.netframeworkPackages.config,右键单击 packages.config 并选择 Migrate Package.config to PackageReference 按钮.你也可以从 本文档.

3.If A is .net framework with packageReference, and B is .net framework with Packages.config, right-click the packages.config and choose Migrate Package.config to PackageReference button. Also you may get some help from this document.

4.如果你的顶层项目是.net core,B项目是.net标准,在VS2017中,nest.dll不会被复制到A的输出文件夹中,你可以尝试添加;true</CopyLocalLockFileAssemblies> 到您的 A.csproj 以解决此问题.类似问题请参见此处.(并且VS2019 16.3.8已经修复了这个问题,这个问题主要出现在VS2017)

4.If your top-level project is .net core and B project targets .net standard, in VS2017, the nest.dll won't be copied into A's output folder, you can try adding <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> to your A.csproj to resolve this issue. Similar issue see here. (And VS2019 16.3.8 has fixed this issue, this issue mostly occurs in VS2017)

而且此行为也受VS版本的影响,如果您使用的是VS2017,请将其更新到最新的15.9.17以获得更好的体验.如果您使用的是 VS2019,请将其更新至 16.3.8.

And this behavior is also affected by VS version, if you're using VS2017, please update it to latest 15.9.17 for better experience. If you're using VS2019, please update it to 16.3.8.

希望有帮助:)

这篇关于即使没有对包的引用,也需要 Nuget 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆