Nuget pull wrong version of System.Runtime.CompilerServices.Unsafe.dll

Viewed 390

I am trying to use System.Text.Josn (5.0.1) which has internal dependency to System.Runtime.CompilerServices.Unsafe.dll Version=4.0.4.1.

Nuget has installed Version=5.0.0.0 of System.Runtime.CompilerServices.Unsafe.dll which cause the following runtime exception : 

System.IO.FileNotFoundException
  HResult=0x80070002
  Message=Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
  Source=System.Memory
  StackTrace:
   at System.MemoryExtensions.AsSpan(String text)
   at System.Text.Json.Serialization.Converters.StringConverter.Write(Utf8JsonWriter writer, String value, JsonSerializerOptions options)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteCore[TValue](JsonConverter jsonConverter, Utf8JsonWriter writer, TValue& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteCore[TValue](Utf8JsonWriter writer, TValue& value, Type inputType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.WriteCoreBytes[TValue](TValue& value, Type inputType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.SerializeToUtf8Bytes[TValue](TValue value, JsonSerializerOptions options)

I have 2 Questions : 1- Why Nuget install the wrong version of System.Runtime.CompilerServices.Unsafe

2- As i see Nuget installed upgraded version of System.Runtime.CompilerServices.Unsafe, which i am thinking it should work according to nuget description : System.Text.Josn (5.0.1) needs System.Runtime.CompilerServices.Unsafe version at least Version=4.0.4.1, which means the upgraded ones should work too.

But it seems not A Code example is posted bellow.

Client / Server Nuget dependencies :

    <ItemGroup>
        <PackageReference Include="Google.Protobuf">
          <Version>3.19.1</Version>
        </PackageReference>
        <PackageReference Include="Grpc">
          <Version>2.41.1</Version>
        </PackageReference>
        <PackageReference Include="Grpc.Tools">
          <Version>2.41.1</Version>
        </PackageReference>
        <PackageReference Include="System.Text.Json">
          <Version>5.0.1</Version>
        </PackageReference>
      </ItemGroup>

Grpc Client Code implementation

    public class GrpcClient
    {
        public GrpcClient()
        {
            Channel channel = new Channel("localhost", 5001, ChannelCredentials.Insecure);
            myClient = new ApiTest.ApiTestClient(channel);
        }

        public string UnaryCall()
        {
            var bytes = JsonSerializer.SerializeToUtf8Bytes("Hallo");

            var request = new Request { Data = ByteString.CopyFrom(bytes) };
            var response = myClient.SayHello(request);
            var result = JsonSerializer.Deserialize<string>(response.ToByteArray());
            return result;
        }

        private readonly ApiTest.ApiTestClient myClient;
    }

Service Code Implementation

    public class ApiTestServices : ApiTest.ApiTestBase
    {
        public override Task<Response> SayHello(Request request, ServerCallContext context)
        {
            var result = JsonSerializer.Deserialize<string>(request.Data.ToByteArray());
            var bytes = JsonSerializer.SerializeToUtf8Bytes<string>(result + "Anas");
            return Task.FromResult(new Response { Data = ByteString.CopyFrom(bytes) });
        }
    }


**Server Code Implementation**

    public class GrpcService
    {
        public GrpcService()
        {
            var services = new List<ServerServiceDefinition> { ApiTest.BindService(new ApiTestServices()) };

            var server = new global::Grpc.Core.Server
                                   {
                                           Ports = { new ServerPort("localhost", 5001, ServerCredentials.Insecure) }
                                   };

            services.ToList().ForEach(item => server.Services.Add(item));
            server.Start();
        }
    }

Any help would be appreciated Thanks

0 Answers
Related