Protobuf-net 创建具有许多类和接口的 .proto



使用:protobuf-net
我正在尝试使用:

string proto = Serializer.GetProto<YourType>();

但我得到的输出有些错误
我想传递一个接口,这样客户端就可以使用&2级。

[ProtoContract]
public class Whatever
{
[ProtoMember(1)]
public string? Text { get; set; }
}
[ProtoContract]
public class Numb
{
[ProtoMember(1)]
public int? Num { get; set; }
}
[Service("Duplex")]
public interface IDuplex
{
public IAsyncEnumerable<Whatever> Duplex(Numb num, CallContext context = default);
}

所以我正在做一些事情:

using var writer = new StreamWriter("proto.proto");
writer.WriteLine(Serializer.GetProto<IDuplex>());
writer.WriteLine(Serializer.GetProto<Numb>());
writer.WriteLine(Serializer.GetProto<Whatever>());
writer.Close();

但我得到了:

syntax = "proto3";
package ConsoleApp1;
message IDuplex {
}
syntax = "proto3";
package ConsoleApp1;
message Numb {
int32 Num = 1;
}
syntax = "proto3";
package ConsoleApp1;
message Whatever {
string Text = 1;
}

而不是类似的东西:

syntax = "proto3";
package ConsoleApp1;
Service IDuplex {
rpc Duplex(Numb) returns(Whatever);
}
message Numb {
int32 Num = 1;
}
message Whatever {
string Text = 1;
}

我如何才能链接多个类,这样我就不会有那么多:

syntax = "proto3";
package ConsoleApp1;

并创建写入同一文件的服务?

protobuf-net本身并不了解服务;为此,你需要protobuf-net。Grpc,在这种情况下:protobuf-net。Grpc。反射,具有SchemaGenerator类型;用法:

var gen = new SchemaGenerator();
var schema = gen.GetSchema(typeof(IDuplex));
Console.WriteLine(schema);

这给出:

syntax = "proto3";
message Numb {
int32 Num = 1;
}
message Whatever {
string Text = 1;
}
service Duplex {
rpc Duplex (Numb) returns (stream Whatever);
}

(这里protobuf-net.Grpc.Reflection是一个单独的包的主要原因是protobuf-net.Grpc只需要protobuf-net v2.x,但此功能需要v3.x API,因此protobuf-net.Grpc.Reflection强制protobuf-netv3可传递(

最新更新