Reputation: 2803
Im completely new in using GRPC. I have and question regarding setting up the .proto file. In my existing solution i have forexample this class:
public class Car
{
public int Id { get; set; }
public string Brand { get; set; }
public string? Type {get; set;}
}
The Car class is placed in a Core project, since it's used by other logic around the solution. But if i like to return in my GRPC Server, is it really necessary to define it in the .proto file again ala this:
message CarReply {
int32 Id = 1;
string Brand = 2;
string Type = 3;
}
What i liked was an reference to my Car() class. Is this not possible?
Upvotes: 1
Views: 789
Reputation: 1062492
If you want to use vanilla "contract first" protobuf, then yes: you need to use a schema and the generated type - it is the generated type that knows how to perform the serialization.
However! There may be an alternative; protobuf-net (and protobuf-net.Grpc) provide an independent implementation of protobuf that supports "code first" usage, which allows you to use your existing type model.
The easiest way to do this with protobuf-net is to annotate your model (using either protobuf-net's attributes, or the framework data-contract attributes - the first option giving more control); for example:
[ProtoContract]
public class Car
{
[ProtoMember(1)]
public int Id { get; set; }
[ProtoMember(2)]
public string Brand { get; set; }
[ProtoMember(3)]
public string? Type {get; set;}
}
It is also possible to configure everything at runtime, but that's a bit harder.
This can also be used fully with gRPC, using interfaces to define the service contract; full guidance is here
Upvotes: 1