Search code examples
.net-coregrpcprotobuf-net

How to add an existing .proto file as link to a .net core project


I followed the "hello world" grpc tutorial to create a grpc server and client project in .net core (3.0). Everything worked very well. When i walked through the steps of adding the .proto file to the client project, i wondered if there is a way to avoid copying the file and instead add the servers .proto file as link the client project.

From the docs on how to add the proto file to the client:

  • Create a Protos folder in the gRPC client project.
  • Copy the Protos\greet.proto file from the gRPC Greeter service to the gRPC client project.

the copied file is then added to the client .csproj like this:

<ItemGroup>
  <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
</ItemGroup>

Is it possible to add the .proto file from the server project as a link to the client?


Solution

  • After some trial and error i found a way to do it. You can set the include property to the path of the file you want to link and the Link property to the path you you want the file to be displayed in your project:

    <ItemGroup>
        <Protobuf Include="..\GreeterService\Protos\greet.proto" Link="Protos\greet.proto" GrpcServices="Client" />
    </ItemGroup>
    

    I found a blog post explaining how to add linked files to a .net core csproj here.