Advertisement
Intermediate ASP.NET Core, gRPC Services

What are JSON transcoding for gRPC services in ASP.NET Core, and why would you enable it?

Short answer

gRPC JSON transcoding (via the Microsoft.AspNetCore.Grpc.JsonTranscoding package) lets a single gRPC service automatically also be callable as a conventional RESTful JSON API, by annotating .proto methods with HTTP binding options (google.api.http) specifying the REST route/verb…

Save ASP.NET Core mock interview
Advertisement
AModel answer
gRPC JSON transcoding (via the Microsoft.AspNetCore.Grpc.JsonTranscoding package) lets a single gRPC service automatically also be callable as a conventional RESTful JSON API, by annotating .proto methods with HTTP binding options (google.api.http) specifying the REST route/verb mapping -- this lets you write and maintain one service implementation while supporting both efficient internal gRPC calls and REST/JSON calls for external clients or browsers that can't easily consume native gRPC, without hand-writing a separate REST facade.
import "google/api/annotations.proto";

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {
    option (google.api.http) = {
      get: "/v1/greet/{name}"
    };
  }
}

// Program.cs
builder.Services.AddGrpc().AddJsonTranscoding();
// Now callable BOTH as a native gRPC call AND as GET /v1/greet/World returning JSON
Real-world example
A service team maintains a single gRPC service implementation but enables JSON transcoding so external partner integrations (unable to easily adopt gRPC) can call the exact same underlying logic via familiar REST/JSON requests, avoiding the need to build and keep in sync a separate REST API layer.
Likely follow-ups
  • How does JSON transcoding handle streaming RPC methods, which don't map cleanly to typical REST semantics?
  • How does this compare to just building a separate REST API that internally calls the gRPC service?
Related topics