dotnet core 2.1:从xunit项目引用Web项目时,“不同版本之间发现冲突”

时间:2018-08-29 08:15:06

标签: .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

1 个答案:

答案 0 :(得分:11)

我想我在此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的程序包引用。

  

Microsoft.AspNetCore.App的程序包引用添加到MyApp.Tests。   有关更多信息,请参见Integration testing is hard to set up and may break on shared framework servicing

相关问题