gRPC Service use models from library

Viewed 198

I am working on my first gRPC service. I have everything working. As in, I can call my service from the client and get a response. My question is can my gRPC service use models from another library?

I have several projects in my solution.

gRPC Server
gRPC Client
Common DTO Library
And a Few more

When I define my proto file is it possible to use the classes from the Common DTO Library?

my.proto

syntax = "proto3";

option csharp_namespace = "myNameSpace";

package myPackageName;

// The service definition.
service MyService{
  rpc MyMethodName (DtoFromAnotherLibrary) returns (byte[]);
}

Thank you, Travis

2 Answers

That's not possible because Proto does not know about your C# projects.

You may consider using code-first gRPC though, where you write C# code that is then creating your proto.

As @Ray stated, you cannot use your model objects through the gRPC interface and he provided a link to the code-first method.

I tend to think of my proto definitions as my external interface and update them with care to ensure backwards compatibility as the interface ages. Because of that, I will code up model objects separate from the gRPC definitions and write extension methods (ToProto for the model, ToModel for the gRPC message) to go back and forth between the two types. It may seem like duplicated effort, but having the flexibility to add things to my model objects like property change notifications, or other convenience methods/properties without affecting the external interface is a plus to me. I spend a lot of time working on the front end, so it analogous to the model/view model relationship.

Related