In the example above, `greet.proto` is in a shared `Protos` directory outside
the project directory. Multiple projects can reference the proto file.
### Generating code in a class library
Create a class library that references `.proto` files and contains generated code. The other
projects in the solution can then reference this shared class library instead of each project
having to compile the same `.proto` files.
The advantages of this are:
- The `.proto` only need to be compiled once.
- It prevents some projects getting multiple definitions of the same generated code, which can in turn break the build.
There are a couple of examples in GitHub:
- The [Liber example](https://github.com/grpc/grpc-dotnet/tree/master/examples#liber)
demonstrates how common protocol buffers messages can be compiled once and used in other projects:
- The *Common* project creates a class library that includes the generates messages contained in `common.proto`
- The *Client* and *Server* projects reference the *Common* project.
- They do not need to recompile `common.proto` as those .NET types are already in
the *Common* class library.
- They do however each generate their own gRPC client or server code as both have a
`<Protobuf>` reference for `greet.proto`. The *Client* and *Server* projects each having their own version of `greet.proto` is OK since they don't reference each other - they only reference the shared *Common* class library.
- The [RouteGuide example](https://github.com/grpc/grpc/tree/v1.46.x/examples/csharp/RouteGuide)
demonstrates how the gRPC client and server code can be generated once and used in other
projects:
- **Note:** this example uses the *legacy c-core C#* packages, but the principles are the same
for *gRPC for .NET* projects.
- The *RouteGuide* project has a `<Protobuf>` reference to `route_guide.proto` and
generates both the gRPC client and server code.
- The *RouteGuideClient* and *RouteGuideServer* projects reference the *RouteGuide* project.
- They do not need any `<Protobuf>` references since the code has already been
generated in the *RouteGuide* project.
# Reference
## Protobuf item metadata reference
@ -252,6 +309,7 @@ Quick links to the examples below: