Search code examples
c#grpc-dotnet

gRPC JSON transcoding with Any element


I'm using JSON transcoding in a .NET 7 gRPC service. A certain element in the proto file is using the Any element:

google.protobuf.Any payload = 3 [(validate.rules) = {any:{required:true}}];

When I'm sending a request containing the Any element

Request exerpt:

"inputPayload": {
  @type": "type.googleapis.com/myRequestPayload",
  "foo": "bar",
}

it gives this response:

{
    "code": 3,
    "message": "Type registry has no descriptor for type name 'myRequestPayload'.",
    "details": []
}

I do have another proto with the definition of myRequestPayload. How can I register this type so that the gRPC service understands the contents of the Any element?


Solution

  • While I haven't tried it, I'd expect you to set GrpcJsonTranscodingOptions.TypeRegistry to a type registry containing the relevant type, e.g.

    var registry = TypeRegistry.FromFiles(MyProtoReflection.FileDescriptor);
    services.AddGrpc().AddJsonTranscoding(options => options.TypeRegistry = registry);
    

    Where MyProtoReflection is the name of the reflection class generated from the proto.

    (You could specify just the message types you expect to be in the registry, but I'd normally do it via a set of files...)