Description
Describe the bug
Conflict between System.Text.Json Version 6.0.0 and Version 7.0.0
System.IO.FileNotFoundException: Could not load file or assembly 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
File name: 'System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
When RestSharp is installed in a netstandard2.0 library project System.Text.Json will be used with version 7.0.2 in this library, because RestSharp installs System.Text.Json version 7.0.2 for netstandard2.0.
When this library is used in a net6.0 console app the errors occurs because net6.0 uses System.Text.Json in version 6.0.0 internally and System.Text.Json 7.0.2 is not installed explicit.
To Reproduce
- Create a Library project with netstandard2.0
- Install RestSharp
- Create a wrapper class for RestSharp with custom SystemTextJsonSerializer (see WrappedClient example)
- Create a console app with .net6.0
- Add the library project to the console app
- Call the constructor of the wrapped client
Wrapped Client
using System.Text.Json;
using System.Text.Json.Serialization;
using RestSharp;
using RestSharp.Serializers.Json;
namespace LibraryA;
public sealed class WrappedClient
{
private static readonly JsonSerializerOptions SerializerOptions = new()
{
Converters =
{
new JsonStringEnumConverter()
},
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
private readonly RestClient _client;
public Client()
{
_client = new RestClient(new RestClientOptions(), configureSerialization: sc => sc.UseSerializer(() => new SystemTextJsonSerializer(SerializerOptions)));
}
}
Console App
try
{
var client = new LibraryA.WrappedClient();
Console.WriteLine("Client created");
}
catch (Exception e)
{
Console.WriteLine("Client creation failed");
Console.WriteLine(e);
throw;
}
Possible Solutiosn
I have no idea if this problem is RestSharp related. I have no idea what's the correct solution for RestSharp to fix this.
The actual dependencies are broken when a library (netstandard2.0) installs RestSharp and uses System.Text.Json e.g. to configure the serializer and the library is used in a net6.0 application.
My first thoughts are that RestSharp must fix this because RestSharp is installing the dependencies. But I have found a solution for me (see below).
So the question is: Should RestSharp fix the dependencies so it can easily be used in libraries? For example a possible solution could be installing System.Text.Json for all frameworks in Version 7.0.2. Another solution could be to install a lower version of System.Text.Json for netstandard2.0 which fits to the frameworks. I have really no idea.
Workaround / my solution for now:
The first possible workaround is installing System.Text.Json in version 7.0.2 explicit in the library project.
Another solution is to build the library project with <TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
. So the correct version of System.Text.Json will be used for net6.0