0

I am creating a gRPC service using Proto 3 and C#.

In the Google developer guide for Protobuff it says about package:

In C# the package is used as the namespace after converting to PascalCase, unless you explicitly provide an option csharp_namespace in your .proto file.

So I'm not sure from that what's the difference between package and option csharp_namespace? What happens if I declare them both? If I declare one of them then is the other one redundant?

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

2 Answers2

1

Packages are quite confusing in Protocol Buffers since people don't really see Protobuf as a language by itself. The protobuf package is useful for generation in your case (when you don't define csharp_namespace), but this is also useful within your proto files.

If you have:

a.proto

syntax = "proto3";

package my_package;

message A {}

in one file and you import this file in another which doesn't use the package, as so:

b.proto

syntax = "proto3";

import "a.proto";

message B {
  my_package.A a = 1;
}

you will need to specify the fully qualified name (my_package.A).

Now, for option csharp_namespace. This will tweak the code generation and so that the namespace in your C# code is the one that you provided as value. So, as for which one will prevail if you define both, it should be the option one since this is for tweaking the generation, where the package is more for you protobuf architecture.

Clément Jean
  • 1,735
  • 1
  • 14
  • 32
  • In the example where you imported a.proto, what happens if b.proto does define a different package of its own? it sounds like it should remain the same? And what if the package is the same as in a.proto? Can I then use the messages from A inside B even without importing? – CodeMonkey Jun 29 '22 at 08:28
  • if you have different package, you will still have to use the fully qualified name (`my_package.A`). And if you define the same package, you still need to import but you can drop the `my_package.` part – Clément Jean Jun 29 '22 at 09:28
0

The package name will be used to give a name to the generated service, and this name will be used to generate the URL, which will be invoked during the call to the remote procedure. e.g.

syntax = "proto3";
package gRPCDemo.v1;
option csharp_namespace = "Branch.Sample.gRPC";
   service BranchService {
     rpc GetById (CountrySearchRequest) returns (CountryReply) {}
   }

so If a package name is not set, then the __ServiceName property will have the value of BranchService.