'Generated gRPC C# code compilation error: Invalid token in class, record, struct, or interface member declaration

I'm building a gRPC service with some proto files that are designed by a 3rd party. The service is built with .NET 5 and Grpc.AspNetCore 2.34.0.

When I'm adding the proto files to the project and try to build it, it fails with the error Invalid token '643_' in class, record, struct, or interface member declaration.

The C# code that is generated from the proto file seems to be the problem:

/// <summary>Field number for the "_64_3" field.</summary>
    public const int 643FieldNumber = 4;
    private static readonly pb::FieldCodec<ulong?> _single_643_codec = pb::FieldCodec.ForStructWrapper<ulong>(34);
    private ulong? 643_;
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
    public ulong? 643 {
      get { return 643_; }
      set {
        643_ = value;
      }
    }

Class members are not supposed to begin with numbers. I can temporarily fix the compilation errors by putting an underscore before the member names. This is no solution ofc because the C# code is regenerated with every build of the project. This is what the relevant piece of the proto looks like:

message X_256B_IDType {
   google.protobuf.UInt64Value _64_0 = 1 [(validate.rules) = {message:{required:true}}];
   google.protobuf.UInt64Value _64_1 = 2 [(validate.rules) = {message:{required:true}}];
   google.protobuf.UInt64Value _64_2 = 3 [(validate.rules) = {message:{required:true}}];
   google.protobuf.UInt64Value _64_3 = 4 [(validate.rules) = {message:{required:true}}];
}

Is there a way to influence how the class members are generated using the Grpc.AspNetCore package? In any case, I have to adhere to the proto and not change the message contracts from an external viewpoint.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source