.net 警告:发现同一依赖程序集的不同版本之间存在冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:39:46  来源:igfitidea点击:

Warning: Found conflicts between different versions of the same dependent assembly

.netwarnings

提问by ollifant

I am currently developing a .NET application, which consists of 20 projects. Some of those projects are compiled using .NET 3.5, some others are still .NET 2.0 projects (so far no problem).

我目前正在开发一个 .NET 应用程序,它由 20 个项目组成。其中一些项目是使用 .NET 3.5 编译的,其他一些项目仍然是 .NET 2.0 项目(到目前为止没有问题)。

The problem is that if I include an external component I always get the following warning:

问题是,如果我包含一个外部组件,我总是会收到以下警告:

"Found conflicts between different versions of the same dependent assembly".

What exactly does this warning mean and is there maybe a possibility to exclude this warning (like using #pragma disable in the source- code files)?

这个警告究竟是什么意思,是否有可能排除这个警告(比如在源代码文件中使用#pragma disable)?

回答by Brian Low

This warning means that two projects reference the same assembly (e.g. System.Windows.Forms) but the two projects require different versions. You have a few options:

此警告意味着两个项目引用相同的程序集(例如System.Windows.Forms),但两个项目需要不同的版本。您有几个选择:

  1. Recompile all projects to use the same versions (e.g. move all to .Net 3.5). This is the preferred option because all code is running with the versions of dependencies they were compiled with.

  2. Add a binding redirect. This will suppress the warning. However, your .Net 2.0 projects will (at runtime) be bound to the .Net 3.5 versions of dependent assemblies such as System.Windows.Forms. You can quickly add a binding redirect by double-clicking on error in Visual Studio.

  3. Use CopyLocal=true. I'm not sure if this will suppress the warning. It will, like option 2 above, mean that all projects will use the .Net 3.5 version of System.Windows.Forms.

  1. 重新编译所有项目以使用相同的版本(例如将所有项目移至 .Net 3.5)。这是首选选项,因为所有代码都使用它们编译时使用的依赖项版本运行。

  2. 添加绑定重定向。这将抑制警告。但是,您的 .Net 2.0 项目将(在运行时)绑定到依赖程序集的 .Net 3.5 版本,例如System.Windows.Forms. 您可以通过双击 Visual Studio 中的错误来快速添加绑定重定向。

  3. 使用CopyLocal=true. 我不确定这是否会抑制警告。与上面的选项 2 一样,这意味着所有项目都将使用 System.Windows.Forms 的 .Net 3.5 版本。

Here are a couple of ways to identify the offending reference(s):

以下是识别违规引用的几种方法:

  • You can use a utility such as the one found at https://gist.github.com/1553265
  • Another simple method is to set Build output verbosity (Tools, Options, Projects and Solutions, Build and Run, MSBuild project build output verbosity, Detailed) and after building, search the output window for the warning, and look at the text just above it. (Hat tip to pauloyawho suggested this in the comments on this answer).
  • 您可以使用一种实用程序,例如在https://gist.github.com/1553265 中找到的实用程序
  • 另一种简单的方法是设置构建输出详细程度(工具、选项、项目和解决方案、构建和运行、MSBuild 项目构建输出详细程度、详细),构建后,在输出窗口中搜索警告,并查看其正上方的文本. (给在这个答案的评论中提出这一点的保洛亚的帽子提示)

回答by Matt Hamilton

Basically this happens when the assemblies you're referencing have "Copy Local" set to "True", meaning that a copy of the DLL is placed in the bin folder along with your exe.

基本上,当您引用的程序集将“Copy Local”设置为“True”时,就会发生这种情况,这意味着 DLL 的副本与您的 exe 一起放置在 bin 文件夹中。

Since Visual Studio will copy all of the dependencies of a referenced assembly as well, it's possible to end up with two different builds of the same assembly being referred to. This is more likely to happen if your projects are in separate solutions, and can therefore be compiled separately.

由于 Visual Studio 也会复制引用程序集的所有依赖项,因此最终可能会引用同一程序集的两个不同版本。如果您的项目位于单独的解决方案中,则更有可能发生这种情况,因此可以单独编译。

The way I've gotten around it is to set Copy Local to False for references in assembly projects. Only do it for executables/web applications where you need the assembly for the finished product to run.

我解决它的方法是将 Copy Local 设置为 False 以用于装配项目中的引用。仅对需要运行成品的程序集的可执行文件/Web 应用程序执行此操作。

Hope that makes sense!

希望这是有道理的!

回答by user1477388

I wanted to post pauloya's solution they provided in the comments above. I believe it is the best solution for finding the offending references.

我想发布他们在上面的评论中提供的 pauloya 的解决方案。我相信这是查找违规引用的最佳解决方案。

The simplest way to find what are the "offending reference(s)" is to set Build output verbosity (Tools, Options, Projects and Solutions, Build and Run, MSBuild project build output verbosity, Detailed) and after building, search the output window for the warning. See the text just above it.

查找“违规引用”的最简单方法是设置构建输出详细程度(工具、选项、项目和解决方案、构建和运行、MSBuild 项目构建输出详细程度、详细信息),并在构建后搜索输出窗口对于警告。请参阅其上方的文字。

For example, when you search the output panel for "conflict" you may find something like this:

例如,当您在输出面板中搜索“冲突”时,您可能会发现如下内容:

3>  There was a conflict between "EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" and "EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".
3>      "EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" was chosen because it was primary and "EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" was not.

As you can see, there is a conflict between EF versions 5 and 6.

如您所见,EF 版本 5 和 6 之间存在冲突。

回答by Gorgsenegger

I had the same problem with one of my projects, however, none of the above helped to solve the warning. I checked the detailed build logfile, I used AsmSpy to verify that I used the correct versions for each project in the affected solution, I double checked the actual entries in each project file - nothing helped.

我的一个项目遇到了同样的问题,但是,以上都没有帮助解决警告。我检查了详细的构建日志文件,我使用 AsmSpy 来验证我在受影响的解决方案中为每个项目使用了正确的版本,我仔细检查了每个项目文件中的实际条目 - 没有任何帮助。

Eventually it turned out that the problem was a nested dependency of one of the references I had in one project. This reference (A) in turn required a different version of (B) which was referenced directly from all other projects in my solution. Updating the reference in the referenced project solved it.

最终发现问题是我在一个项目中的引用之一的嵌套依赖。此参考 (A) 反过来需要不同版本的 (B),该版本直接从我的解决方案中的所有其他项目中引用。更新引用项目中的引用解决了它。

Solution A
+--Project A
   +--Reference A (version 1.1.0.0)
   +--Reference B
+--Project B
   +--Reference A (version 1.1.0.0)
   +--Reference B
   +--Reference C
+--Project C
   +--Reference X (this indirectly references Reference A, but with e.g. version 1.1.1.0)

Solution B
+--Project A
   +--Reference A (version 1.1.1.0)

I hope the above shows what I mean, took my a couple of hours to find out, so hopefully someone else will benefit as well.

我希望以上内容能说明我的意思,我花了几个小时才弄明白,所以希望其他人也能从中受益。

回答by Tiago Gouvêa

On Visual Studio if you right click on the solutionand Manage nuget packagestheres a "Consolidate"tab which sets all the packages to the same version.

在 Visual Studio 上,如果您右键单击解决方案管理 nuget 包,则会有一个“合并”选项卡,它将所有包设置为相同的版本。

回答by MoMo

I just had this warning message and cleaned the solution and recompiled (Build -> Clean Solution) and it went away.

我刚刚收到此警告消息并清理了解决方案并重新编译(构建 - > 清理解决方案),然后它就消失了。

回答by Phil50

I had the same issue and I resolved by changing the following in web.config.

我遇到了同样的问题,我通过在 web.config 中更改以下内容来解决。

It happened to me because I am running the application using Newtonsoft.Json 4.0

这发生在我身上,因为我正在使用 Newtonsoft.Json 4.0 运行应用程序

From:

从:

<dependentAssembly>
  <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>

To:

到:

<dependentAssembly>
  <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="4.5.0.0" />
</dependentAssembly>

回答by Bill

I have another way to do this if you're using Nuget to manage your dependencies. I've discovered that sometimes VS and Nuget don't match up and Nuget is unable to recognize that your projects are out of sync. The packages.config will say one thing but the path shown in References - Properties will indicate something else.

如果您使用 Nuget 来管理您的依赖项,我还有另一种方法可以做到这一点。我发现有时 VS 和 Nuget 不匹配并且 Nuget 无法识别您的项目不同步。package.config 会说一件事,但参考文献 - 属性中显示的路径将指示其他内容。

If you're willing to update your dependencies, do the following:

如果您愿意更新依赖项,请执行以下操作:

  1. From Solution Explorer, right click the Project and click 'Manage Nuget Packages'

  2. Select 'Installed packages' tab in left pane Record your installed packages You may want to copy your packages.config to your desktop first if you have a lot, so you can cross check it with Google to see what Nuget pkgs are installed

  3. Uninstall your packages. Its OK, we're going to add them right back.

  4. Immediately install the packages you need. What Nuget will do is not only get you the latest version, but will alter your references, and also add the binding redirects for you.

  5. Do this for all of your projects.

  6. At the solution level, do a Clean and Rebuild.

  1. 在解决方案资源管理器中,右键单击项目并单击“管理 Nuget 包”

  2. 在左侧窗格中选择“已安装的包”选项卡记录您已安装的包如果您有很多包,您可能希望首先将您的packages.config 复制到您的桌面,以便您可以与 Google 交叉检查以查看安装了哪些 Nuget pkgs

  3. 卸载您的软件包。没关系,我们马上把它们加回来。

  4. 立即安装您需要的软件包。Nuget 不仅会为您提供最新版本,还会更改您的引用,并为您添加绑定重定向。

  5. 为您的所有项目执行此操作。

  6. 在解决方案级别,执行清理和重建。

You may want to start with the lower projects and work your way to the higher level ones, and rebuild each project as you go along.

您可能希望从较低的项目开始,然后逐步升级到较高级别的项目,并在进行过程中重建每个项目。

If you don't want to update your dependencies, then you can use the package manager console, and use the syntax Update-Package -ProjectName [yourProjectName] [packageName] -Version [versionNumber]

如果您不想更新依赖项,则可以使用包管理器控制台,并使用语法 Update-Package -ProjectName [yourProjectName] [packageName] -Version [versionNumber]

回答by Jon Limjap

This actually depends on your external component. When you reference an external component in a .NET application it generates a GUID to identify that component. This error occurs when the external component referenced by one of your projects has the same name and but different version as another such component in another assembly.

这实际上取决于您的外部组件。当您在 .NET 应用程序中引用外部组件时,它会生成一个 GUID 来标识该组件。当您的一个项目引用的外部组件与另一个程序集中的另一个此类组件具有相同的名称但版本不同时,就会发生此错误。

This sometimes happens when you use "Browse" to find references and add the wrong version of the assembly, or you have a different version of the component in your code repository as the one you installed in the local machine.

当您使用“浏览”查找引用并添加错误版本的程序集,或者您的代码存储库中的组件版本与本地计算机中安装的组件版本不同时,有时会发生这种情况。

Do try to find which projects have these conflicts, remove the components from the reference list, then add them again making sure that you're pointing to the same file.

一定要尝试找出哪些项目存在这些冲突,从引用列表中删除组件,然后再次添加它们,确保您指向同一个文件。

回答by Neelam Prajapati

=> check there will be some instance of application installed partially.

=> 检查是否会部分安装一些应用程序实例。

=> first of all uninstall that instance from uninstall application.

=> 首先从卸载应用程序中卸载该实例。

=> then,clean,Rebuild,and try to deploy.

=> 然后,清理,重建,并尝试部署。

this solved my issue.hope it helps you too. Best Regards.

这解决了我的问题。希望它也能帮助你。此致。