Reputation: 3355
The protobuf format has the type google.protobuf.Value
described in Create Protobuf messages for .NET apps.
There is also a Google.Protobuf.WellKnownTypes.Value
type in the Google.Protobuf, Version=3.28.0.0
library.
I can send google.protobuf.Value
with a message described by such a proto-file:
syntax = "proto3";
import "google/protobuf/struct.proto";
import "google/protobuf/empty.proto";
service Greeter {
rpc SayHello (google.protobuf.Empty) returns (Status);
}
message Status {
google.protobuf.Value data = 3;
}
As a result, I will get a generated dot with a "data" field of the type Google.Protobuf.WellKnownTypes.Value
. I can assign an arbitrary JsonDocument to this "data" in the following way and everything will be transmitted perfectly.
var dto = new Status();
dto.Data = Google.Protobuf.WellKnownTypes.Value.Parser.ParseJson(myJsonDocument.RootElement.ToString())
I used protobuf-net.Grpc
and applied code-first by creating such a dto with the field Google.Protobuf.WellKnownTypes.Value
[DataContract]
public class ObjectTypeDto
{
[DataMember(Order = 1)]
public Google.Protobuf.WellKnownTypes.Value Data { get; set; }
}
If I pass my myJsonDocument
through it as before, I get an exception: System.InvalidOperationException: No serializer defined for type: Google.Protobuf.WellKnownTypes.Value
I used the tool https://protogen.marcgravell.com and generated the following dto from the above proto file:
// <auto-generated>
// This file was generated by a tool; you should avoid making direct changes.
// Consider using 'partial classes' to extend these types
// Input: my.proto
// </auto-generated>
#region Designer generated code
#pragma warning disable CS0612, CS0618, CS1591, CS3021, IDE0079, IDE1006, RCS1036, RCS1057, RCS1085, RCS1192
[global::ProtoBuf.ProtoContract()]
public partial class Status : global::ProtoBuf.IExtensible
{
private global::ProtoBuf.IExtension __pbn__extensionData;
global::ProtoBuf.IExtension
global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
=> global::ProtoBuf.Extensible.GetExtensionObject(ref
__pbn__extensionData, createIfMissing);
[global::ProtoBuf.ProtoMember(3, Name = @"data")]
public global::Google.Protobuf.WellKnownTypes.Value Data { get; set; }
}
[global::System.ServiceModel.ServiceContract(Name = @"Greeter")]
public partial interface IGreeter
{
global::System.Threading.Tasks.ValueTask<Status>
SayHelloAsync(global::ProtoBuf.Grpc.CallContext context = default);
}
#pragma warning restore CS0612, CS0618, CS1591, CS3021, IDE0079, IDE1006, RCS1036, RCS1057, RCS1085, RCS1192
#endregion
When I try to return the result from the method in this dto, I get the same exception.
How do I send a google.protobuf.Value
(Google.Protobuf.WellKnownTypes.Value
) object using protobuf-net.Grpc
and code-first?
Upvotes: 0
Views: 49