.net-core - dotnet 核心 2.1 : "Found conflicts between different versions of" when referencing a web project from an xunit project

标签 .net-core asp.net-core-2.0

当我偶然发现这个奇怪的事情时,我正在将 netcore 2.0 应用程序升级到 2.1。

如果我创建一个 web 项目,然后创建一个引用第一个的 xunit 项目,只要我使用任何 Newtonsoft.Json 类,我就会收到以下错误:

/media/data/sas/devel/opt/dotnet-sdk-2.1.401/sdk/2.1.401/Microsoft.Common.CurrentVersion.targets(2110,5): 
warning MSB3277: Found conflicts between different versions of "Newtonsoft.Json" that could not be resolved.  
These reference conflicts are listed in the build log when log verbosity is set to detailed. 
[/media/data/sas/devel/apps/tmp/dotnet-error/test/test.csproj]
  test -> /media/data/sas/devel/apps/tmp/dotnet-error/test/bin/Debug/netcoreapp2.1/test.dll

在我试图升级的项目中,我遇到了很多这样的错误。似乎 xunit 项目正在使用包的不同版本并且它们发生冲突。

这些是重现错误的步骤:
$ dotnet new web -o project
$ dotnet new xunit -o test
$ cd test
$ dotnet add reference ../project/
$ dotnet clean && dotnet build

一切正常,但如果我将其添加到 project/Program.cs
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
        var s = Newtonsoft.Json.JsonConvert.SerializeObject(123);
    }

然后我得到了上述警告。

我有办法解决这个问题吗?

顺便提一句
$ dotnet --version
2.1.401

最佳答案

我想我在这个 migration guide 中找到了答案:

https://docs.microsoft.com/en-us/aspnet/core/migration/20_21?view=aspnetcore-2.1#rules-for-projects-targeting-the-shared-runtime

您必须添加对 Microsoft.AspNetCore.App 的包引用在测试项目中。

Add a package reference for Microsoft.AspNetCore.App to MyApp.Tests. For more information, see Integration testing is hard to set up and may break on shared framework servicing.

关于.net-core - dotnet 核心 2.1 : "Found conflicts between different versions of" when referencing a web project from an xunit project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52072663/

相关文章:

c# - ASP.NET Core 2 无法解析 Microsoft EntityFrameworkCore DbContext 类型的服务

c# - UnmanagedExports 等效 c#.Core2

c# - 如何引用和上传Azure Blob存储中的文件?

azure - .NET Core Azure WebJob 跟踪日志仅记录 1 个条目

logging - 如何配置 ILogger 以将日志输出到特定文件?

.net - 在 Visual Studio 2015 中安装 Net Core 2.0 构建后出现问题

c# - asp.net mvc core 2在 View 模型中选择值始终为0

c# - Url.Action 从哪里获取这些值?

c# - Windows .NET Core的Windows窗体设计器和WPF设计器

c# - 如何生成从 0 到给定上限的正态分布的随机正 float ?