I am trying to build a PoC at work that utilizes gRPC. The google document here takes us through a sample application. I was wondering if protobuf-net, and specifically protogen, had the capability to understand service definitions and classes necessary to perform gRPC calls? Or is this something being worked on? Would it work if I use google's protoc for client and server code generation(which involves the service definitions and RPC calls) and protobuf-net for my business objects.
-
1see update, note – Marc Gravell Mar 20 '19 at 21:15
2 Answers
protobuf-net.Grpc is now a thing... albeit in preview. When .NET Core 3 comes out, we should be able to make this available.
It is inspired by the WCF approach, so your service interfaces are defined via:
namespace Whatever {
[ServiceContract]
public interface IMyAmazingService {
ValueTask<SearchResponse> SearchAsync(SearchRequest request);
// ... etc
}
}
Servers just implement the interface:
public class MyServer : IMyAmazingService {
// ...
}
(how you host them depends on whether you're using ASP.NET Core, or the native/unmanaged gRPC libraries; both work)
and clients just request the interface:
var client = http.CreateGrpcService<IMyAmazingService>();
var result = await client.SearchAsync(query);
In the above case, this would be inferred to be the Whatever.MyAmazingService
/ Search
service in gRPC terms, i.e.
package Whatever;
// ...
service MyAmazingService {
rpc Search (SearchRequest) returns (SearchResponse) {}
}
but the service/method names can be configured more explicitly if you prefer. The above is a unary example; for unary operations, the result can be any of T
, Task<T>
, ValueTask<T>
- or void
/ Task
/ ValueTask
(which all map to .google.protobuf.Empty
, as does a method without a suitable input parameter).
The streaming/duplex operations are inferred automatically if you use IAsyncEnumerable<T>
(for some T
) for the input parameter (client-streaming), the return type (server-streaming), or both (duplex).

- 1,026,079
- 266
- 2,566
- 2,900
-
1you're a star Marc. gRPC is definitely the way forward. So glad this is getting love & attention. – michal krzych Aug 03 '19 at 09:21
-
Is it going to be supported on .net framework as well? (maybe targeting .net standard 2.x) – barakcaf Aug 26 '19 at 06:46
-
1
It is something that I would love to get around to, but to date, no: I haven't had need to look into this, and it hasn't hit the top of my backlog. I try and keep an eye on what features people want, so it is good to know that you're after it, but today: no. Mostly this is a time thing - protobuf-net gets progressed out of my free/spare time, unless I have a genuine justification to spend "work time" on it.
Update: I'm actively talking with the Microsoft folks who are working on gRPC for .NET, and it seems likely that we're going to try to work together here so that this becomes possible with the gRPC things in the .NET Core 3.0 timescale - meaning: we'd share an implementation of the service invocation code, but allow it to work with multiple serializer APIs.

- 1,026,079
- 266
- 2,566
- 2,900
-
Thanks for the swift response. Do you foresee trouble using a combination of protobuf-net with gRPC, by making use of tools google offers to write service definitions etc. and sticking to protobuf-net for defining my Models/business objects? At the end of the day, they are all going to become .NET classes, only the means of getting there would be different for different parts of the application. – Sai Mar 12 '19 at 17:05
-
@Sai the google API is based on the google approach to protobuf objects; I would *not* expect protobuf-net types to work with the google implementation of the gRPC APIs. – Marc Gravell Mar 12 '19 at 17:09