Search code examples
.netserializationprotocol-buffersgrpcprotobuf-net

How to include services in .proto file when using auto-generation (Serializer.GetProto<>())


So I need to create a full .proto file with a service and messages to fully describe one of the services I'm going to use. I already found this post that mentions the use of string proto = Serializer.GetProto<YourType>(); But sadly when used on my service it just generates a proto file with a "message" named after my service.

Talking with a the concrete file examples I give bellow, is there a way to get the Example.proto from the IExampleService.cs?

[ServiceContract]
public interface IExampleService
{
    Task<ExampleReply> ExampleMethod(ExampleRequest request);
}

[ProtoContract]
public class ExampleRequest
{
    [ProtoMember(1)]
    public string Prop1 { get; set; }

    [ProtoMember(2, DataFormat = DataFormat.WellKnown)]
    public DateTime TimeProp2 { get; set; }
}

[ProtoContract]
public class ExampleReply
{
    [ProtoMember(1)]
    public string Prop1 { get; set; }
}

Example.proto file

service IExampleService {
    rpc ExampleMethod (ExampleRequest) returns (ExampleReply) {}
}

message ExampleRequest{
   string Prop1 = 1;
   .google.protobuf.Timestamp TimeProp2 = 2;
 }

message ExampleReply {
  string message = 1;
}

Solution

  • Update: this now exists, but it is in protobuf-net.Grpc.Reflection, via the SchemaGenerator type. It also requires protobuf-net v3.


    Right now the code to do this has not been written. There is an open github ticket for it, but it will take some time. For now, you'll probably need to hack it by hand.