dotnet core 2.1: “Found conflicts between different versions of” when referencing a web project from an xunit project

梦想与她 提交于 2019-12-10 18:21:03

问题


I was on my way to upgrade a netcore 2.0 app to 2.1 when I stumbled upon this strange thing.

If I create a web project, and then a xunit project referencing the first one, as soon as I use any Newtonsoft.Json class I get the following error:

/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

In the project I was trying to upgrade I get lots of errors like this. It seems like the xunit project is using diffente versions of the packages and that they colide.

These are the steps to reproduce the error:

$ dotnet new web -o project
$ dotnet new xunit -o test
$ cd test
$ dotnet add reference ../project/
$ dotnet clean && dotnet build

and everything works ok, but if I add this to project/Program.cs

    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
        var s = Newtonsoft.Json.JsonConvert.SerializeObject(123);
    }

Then I get the afforementioned warning.

I there some way to work this out?

BTW

$ dotnet --version
2.1.401

回答1:


I guess I found the answer in this migration guide:

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

You have to add a package reference to Microsoft.AspNetCore.App in the test project.

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.



来源:https://stackoverflow.com/questions/52072663/dotnet-core-2-1-found-conflicts-between-different-versions-of-when-referencin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!