protocol-buffersgrpcprotobuf-netgrpc-c#

Protobuf Compiler Error When There's a Period in the csharp_namespace Value


I am trying to add my protos to an existing common library so my clients and servers can import it and stay in sync with any changes.

I added Grpc.AspNetCore, Google.Protobuf, and Grpc.Tools to the project:

<PackageReference Include="Google.Protobuf" Version="3.25.2" />
<PackageReference Include="Grpc.AspNetCore" Version="2.60.0" />
<PackageReference Include="Grpc.Tools" Version="2.61.0">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

(but I'm pretty sure the only one needed is Grpc.AspNetCore)

I included the .proto file in the project, as both client and server:

<ItemGroup>
   <Protobuf Include="./Protos/MyProto.proto" GrpcServices="Both" ProtoRoot="./Protos/" />
</ItemGroup>

I created the file:

syntax = "proto3";
package myproto_grpc;

option csharp_namespace = "Projectname.Library.Namespace.gRPC";

service MyService {
    rpc Message (MessageRequest) returns (Reading);
}

message MessageRequest {}

message Reading{
    string id = 1;
    int32 value = 2;

    enum Polarity {
        Unspecified = 0;
        Positive = 1;
        Negative = 2;
    }
    
    Polarity polarity = 3;
    bool is_something = 4;
}

Whenever I build the project, I get the error:

Severity Code Description Project File Line Suppression State Details Error CA1708 Names of 'Namespaces' and 'Existing, Existing' should differ by more than case (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1708)

The kicker is, if I change the csharp_namespace to any value without a . it compiles just fine; any value with . in it - even something completely random like Random.Name won't compile.

I am new to gRPC and protobuf, so I might have missed something obvious.

EDIT 1 Interestingly, when I create a new grpc project from scratch in Visual Studio it builds fine until I change the csharp_namespace to Whatever.GrpcService1 - then I get this: error messages


Solution

  • Stupid issue, but I had a casing issue in my csharp_namespace that conflicted with other classes in the project. So everywhere in my project it's ProjectName.Library.Namespace but in my proto I had

    option csharp_namespace = "Projectname.Library.Namespace.gRPC";
    

    ... note the difference between ProjectName and Projectname.

    Changed the capitalization to match and the error went away.