I'm starting a small example project with gRPC in C# and, when I implement the proto file, I get some errors when I type the same name function but different parameters.
Can I have functions overloading in the proto file?
No, the name of an RPC in a service in protobuf has to be unique.
The simplest way of achieving something a bit like overloading would be to have a single request type and a single response type, but use a oneof
at the top level to specify one "subrequest" or "subresponse". You could then very easily create another partial class in the C# code and write overloads in that, which basically create the request appropriately from a subrequest message, and then extract the appropriate subresponse.
Alternatively, if your RPC really wants to just do one thing and you're only trying to make it easier to call in different ways (which is the most appropriate form of overloading), then you can just add the partial class. For example, if you have a service like this:
service Math {
rpc Add (AddRequest) returns (AddResponse) {}
}
message AddRequest {
int32 x = 1;
int32 y = 2;
}
message AddResponse {
int32 result;
}
... then if the generated code looks something like this:
public partial class MathClient
{
public virtual AddResponse Add(AddRequest request, CallOptions options)
{
...
}
public virtual AsyncUnaryCall<AddResponse> AddAsync(AddRequest request,
headers = null, DateTime? deadline = null,
CancellationToken cancellationToken = default)
{
...
}
public virtual AsyncUnaryCall<AddResponse> AddAsync(
AddRequest request, CallOptions options)
{
...
}
}
... then you could write your own partial class like this:
public partial class MathClient
{
public int Add(int x, int y)
{
var request = new AddRequest { X = x, Y = y };
var response = Add(request);
return response.Result;
}
// Similarly for async
}