24

I am specifying a number of independent gRPC services that will all be hosted out of the same server process. Each service is defined in its own protobuf file. These are then run through the gRPC tools to give me the target language (c# in my case) in which I can then implement my server and client.

Each of those separate APIs uses a number of common elements, things like error response enumerations, the Empty message type (which seems to be available in the gRPC WellKnownTypes; but I cannot see how I include that either so I defined my own).

At the moment I end up with each proto building duplicate enums and classes into their own namespace. Though I know I can share the definitions in a common proto file and include that; I do not see how to end up with only a single code gen of these into a common namespace. Though this works it would be neater to keep it to one set; it may also have issues later in conversion and equivalence if doing things like aggregating errors across services.

I assume I am missing something as my reading of things such as the WellKnownTypes namespace suggests that this should be possible but, as mentioned before, I don't see how I refer to that in the Proto either.

SO seems pretty light on gRPC for the moment so my searches are not turning up much, and I am new to this so any pointers?

Wizbit
  • 421
  • 1
  • 4
  • 13

1 Answers1

32

Protocol buffers solve this problem by using a different package identifier. Each message will be placed in a different Protocol buffer specific package, which is independent of the C# namespace. For example:

// common.proto
syntax "proto3";
package my.api.common;

option csharp_namespace = "My.Api.Common";

message Shared {
  // ...
}

And then in the service specific file:

// service1.proto
syntax "proto3";
package my.api.service1;

import "common.proto";

option csharp_namespace = "My.Api.Service1";

message Special {
    my.api.common.Shared shared = 1;
}

You need to make sure there is only one copy of the common proto, otherwise they could get out of sync. You can put common messages in the common.proto file and reference them from each of your specific other proto files.

Carl Mastrangelo
  • 5,970
  • 1
  • 28
  • 37
  • 1
    I am sooooo sorry it took me so long to get back to verifying this. Was dragged onto another task. – Wizbit Mar 23 '17 at 18:13