'Generating `proto` messages in one project and `gRPC` stubs and clients in another in `.NET`

I have a collection of .proto files which contain messages and service definitions.

I used to use those files to generate C# code to then write helpers around the generated types and publish this library for dependent systems to use. Now what I noticed is that if I do that, and one dependent system ends up depending on multiple of those libraries, it ends up having conflicting definitions of the shared protocol buffer messages. The solution to that problem (I thought) was to generate the C# code in a single project referenced by the libs and the dependent systems, potentially independently.

However, a lot of this code doesn't depend on gRPC part of this generated code (i.e. the stubs and the clients). It only depends on the messages. That is the case for example in places where I simply wish to use the model I defined in protocol buffers. But I am still forced to depend on

        <PackageReference Include="Google.Protobuf" Version="3.19.4"/>
        <PackageReference Include="Grpc" Version="2.44.0"/>
        <PackageReference Include="Grpc.Core" Version="2.44.0"/>

I don't find that very nice, since I think I would only need the dependency on the first of those packages.

My question is then: is there a way of only generating the messages in on project, and generate the clients and stubs in another project, and tell the client / stubs project that it should look for the messages it relies on in that first project ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source