gRPC JSON转码与Any元素

huangapple go评论55阅读模式
英文:

gRPC JSON transcoding with Any element

问题

我正在使用.NET 7 gRPC服务中的JSON编码转换。在proto文件中的某个元素正在使用Any元素:
```protobuf
google.protobuf.Any payload = 3 [(validate.rules) = {any:{required:true}}];

当我发送包含Any元素的请求时

请求摘要:

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

它返回以下响应:

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

我有另一个proto文件,其中定义了myRequestPayload。如何注册此类型,以便gRPC服务能够理解Any元素的内容?


<details>
<summary>英文:</summary>

I&#39;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&#39;m sending a request containing the Any element

Request exerpt:
```json
&quot;inputPayload&quot;: {
  @type&quot;: &quot;type.googleapis.com/myRequestPayload&quot;,
  &quot;foo&quot;: &quot;bar&quot;,
}

it gives this response:

{
    &quot;code&quot;: 3,
    &quot;message&quot;: &quot;Type registry has no descriptor for type name &#39;myRequestPayload&#39;.&quot;,
    &quot;details&quot;: []
}

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?

答案1

得分: 0

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.

英文:

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 =&gt; 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...)

huangapple
  • 本文由 发表于 2023年3月9日 19:03:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定