Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 753
opensas Avatar asked Dec 03 '22 11:12

opensas


1 Answers

I guess I found the answer in this migration guide:

https://learn.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.

like image 69
opensas Avatar answered Dec 20 '22 23:12

opensas