protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

System.Runtime.CompilerServices.Unsafe

Open amastrobera opened this issue 4 years ago • 2 comments

hello,

I am using Visual Studio 2019, Windows 10, .Net Framework 4.8, in C#.

I have my proto model.proto that compiles into Model.pb.cs, and I use it in my program.

The program in question simply fills the data model and saves it to file.

using PMOD = MyProtobuf.Model;

using NLog;
using NetMQ;
using NetMQ.Sockets;
using System;
using System.Text;
using System.IO;

var req = PMOD.MyDataModel();
// fill up req with all data ...
using (var file_output = File.Create("path/to/save/PB/file/into")) {
   using (var stream_output = new Google.Protobuf.CodedOutputStream(file_output)) {
      req.WriteTo(stream_output); <---- this one gives errors
   }
}

the relative .csproj is something like that

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <PlatformTarget>x64</PlatformTarget>
    <OutputType>Library</OutputType>
  </PropertyGroup>

  <ItemGroup>
      <Protobuf Include="../../protobuf/model.proto" Link="model.proto" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="coverlet.msbuild" Version="3.0.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Google.Protobuf" Version="3.15.6" />
    <PackageReference Include="Grpc" Version="2.36.4" />
    <PackageReference Include="MathNet.Numerics" Version="4.15.0" />
    <PackageReference Include="MathNet.Spatial" Version="0.6.0" />
    <PackageReference Include="NetMQ" Version="4.0.1.6" />
    <PackageReference Include="NLog" Version="4.7.9" />
    <PackageReference Include="System.Text.Json" Version="5.0.2" />
  </ItemGroup>

  etc etc ... with files to compile

this is the error

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.

It looks like Google.Protobuf needs System.Runtime.CompilerServices.Unsafe >= 4.5, and System.Text.Json needs >= 5.0. So I added it from NuGet :

<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />

--> I still get the same error.

later I checked this post and tried to apply the same solution doing either gacutil /i System.Runtime.CompilerServices.Unsafe.dll or saving the config into the app.config (that wasn't needed before).

--> I still get the same error.

What should I do ?

amastrobera avatar Aug 11 '21 13:08 amastrobera

Apparently it is due to incorrect versioning done at System.Runtime.CompilerServices.Unsafe.

The System.Runtime.CompilerServices.Unsafe.dll from nuget package for 4.5.2 shows version 4:0:4:1 in manifest.

And when Google.protobuff compiles with System.Runtime.CompilerServices.Unsafe 4.5.2 it is reflected in its manifest.

.assembly extern System.Runtime.CompilerServices.Unsafe { .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) // .?_....: .ver 4:0:4:1 }

naiksujit avatar Mar 15 '22 11:03 naiksujit

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago.

github-actions[bot] avatar Feb 18 '24 10:02 github-actions[bot]

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

github-actions[bot] avatar Mar 03 '24 10:03 github-actions[bot]