xref: /aosp_15_r20/external/grpc-grpc/src/csharp/BUILD-INTEGRATION.md (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1*cc02d7e2SAndroid Build Coastguard Worker# Protocol Buffers/gRPC Codegen Integration Into .NET Build
2*cc02d7e2SAndroid Build Coastguard Worker
3*cc02d7e2SAndroid Build Coastguard WorkerThe [Grpc.Tools](https://www.nuget.org/packages/Grpc.Tools) NuGet package provides C# tooling support for generating C# code from `.proto` files in `.csproj` projects:
4*cc02d7e2SAndroid Build Coastguard Worker* It contains protocol buffers compiler and gRPC plugin to generate C# code.
5*cc02d7e2SAndroid Build Coastguard Worker* It can be used in building both grpc-dotnet projects and legacy c-core C# projects.
6*cc02d7e2SAndroid Build Coastguard Worker
7*cc02d7e2SAndroid Build Coastguard WorkerUsing `Grpc.Tools` in `.csproj` files is described below. Other packages providing the runtime libraries for gRPC are described elsewhere.
8*cc02d7e2SAndroid Build Coastguard Worker
9*cc02d7e2SAndroid Build Coastguard Worker## Getting Started
10*cc02d7e2SAndroid Build Coastguard Worker
11*cc02d7e2SAndroid Build Coastguard WorkerThe package [Grpc.Tools](https://www.nuget.org/packages/Grpc.Tools) is used automatically to generate the C# code for protocol buffer messages and gRPC service stubs from
12*cc02d7e2SAndroid Build Coastguard Worker`.proto` files. These files:
13*cc02d7e2SAndroid Build Coastguard Worker* are generated on an as-needed basis each time the project is built.
14*cc02d7e2SAndroid Build Coastguard Worker* aren't added to the project or checked into source control.
15*cc02d7e2SAndroid Build Coastguard Worker* are a build artifact usually contained in the obj directory.
16*cc02d7e2SAndroid Build Coastguard Worker
17*cc02d7e2SAndroid Build Coastguard WorkerThis package is optional. You may instead choose to generate the C# source files from
18*cc02d7e2SAndroid Build Coastguard Worker`.proto` files by running the `protoc` compiler manually or from a script.
19*cc02d7e2SAndroid Build Coastguard WorkerHowever this package helps to simplify generating the C# source files by
20*cc02d7e2SAndroid Build Coastguard Workerintegrating the code generation into the build process. It can be used when building both
21*cc02d7e2SAndroid Build Coastguard Workerthe server and client projects, and by both c-core C# projects and grpc-dotnet projects:
22*cc02d7e2SAndroid Build Coastguard Worker* The `Grpc.AspNetCore` metapackage already includes a reference to `Grpc.Tools`.
23*cc02d7e2SAndroid Build Coastguard Worker* gRPC for .NET client projects and projects using `Grpc.Core` need to reference `Grpc.Tools` explicity if you want code generation for those projects
24*cc02d7e2SAndroid Build Coastguard Worker
25*cc02d7e2SAndroid Build Coastguard Worker`Grpc.Tools` is only used at build-time and has no runtime components.
26*cc02d7e2SAndroid Build Coastguard WorkerIt should be marked with `PrivateAssets="All"` to prevent it from being included at runtime, e.g.
27*cc02d7e2SAndroid Build Coastguard Worker```xml
28*cc02d7e2SAndroid Build Coastguard Worker<PackageReference Include="Grpc.Tools" Version="2.50.0">
29*cc02d7e2SAndroid Build Coastguard Worker  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
30*cc02d7e2SAndroid Build Coastguard Worker  <PrivateAssets>all</PrivateAssets>
31*cc02d7e2SAndroid Build Coastguard Worker</PackageReference>
32*cc02d7e2SAndroid Build Coastguard Worker```
33*cc02d7e2SAndroid Build Coastguard Worker
34*cc02d7e2SAndroid Build Coastguard WorkerSupport is provided for the following platforms:
35*cc02d7e2SAndroid Build Coastguard Worker* Windows (x86, x64, and arm64 via using the x86 binaries)
36*cc02d7e2SAndroid Build Coastguard Worker* MacOS (x64 and arm64 via using the x64 binaries)
37*cc02d7e2SAndroid Build Coastguard Worker* Linux (x86, x64, and arm64)
38*cc02d7e2SAndroid Build Coastguard Worker
39*cc02d7e2SAndroid Build Coastguard WorkerYou may still use the MSBuild integration provided by `Grpc.Tools` for other architectures provided you can supply the codegen binaries for that platform/architecture.
40*cc02d7e2SAndroid Build Coastguard WorkerSee [Using Grpc.Tools with unsupported architectures](#unsupported-arch) below.
41*cc02d7e2SAndroid Build Coastguard Worker
42*cc02d7e2SAndroid Build Coastguard Worker## Adding `.proto` files to a project
43*cc02d7e2SAndroid Build Coastguard Worker
44*cc02d7e2SAndroid Build Coastguard WorkerTo add `.proto` files to a project edit the project’s `.csproj` file and add an item group with a `<Protobuf>` element that refers to the `.proto` file, e.g.
45*cc02d7e2SAndroid Build Coastguard Worker
46*cc02d7e2SAndroid Build Coastguard Worker```xml
47*cc02d7e2SAndroid Build Coastguard Worker<ItemGroup>
48*cc02d7e2SAndroid Build Coastguard Worker    <Protobuf Include="Protos\greet.proto" />
49*cc02d7e2SAndroid Build Coastguard Worker</ItemGroup>
50*cc02d7e2SAndroid Build Coastguard Worker```
51*cc02d7e2SAndroid Build Coastguard Worker
52*cc02d7e2SAndroid Build Coastguard WorkerWildcards can be used to select several `.proto` files, e.g.
53*cc02d7e2SAndroid Build Coastguard Worker
54*cc02d7e2SAndroid Build Coastguard Worker```xml
55*cc02d7e2SAndroid Build Coastguard Worker<ItemGroup>
56*cc02d7e2SAndroid Build Coastguard Worker    <Protobuf Include="**\*.proto" />
57*cc02d7e2SAndroid Build Coastguard Worker</ItemGroup>
58*cc02d7e2SAndroid Build Coastguard Worker```
59*cc02d7e2SAndroid Build Coastguard Worker
60*cc02d7e2SAndroid Build Coastguard WorkerBy default, a `<Protobuf>` reference generates gRPC client and a service base class from the `service` definitions in the `.proto` files.
61*cc02d7e2SAndroid Build Coastguard WorkerThe `GrpcServices` attribute can be used to limit C# asset generation. See the reference section below for all
62*cc02d7e2SAndroid Build Coastguard Workeroptions. E.g. to only generate client code:
63*cc02d7e2SAndroid Build Coastguard Worker
64*cc02d7e2SAndroid Build Coastguard Worker```xml
65*cc02d7e2SAndroid Build Coastguard Worker<ItemGroup>
66*cc02d7e2SAndroid Build Coastguard Worker    <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
67*cc02d7e2SAndroid Build Coastguard Worker</ItemGroup>
68*cc02d7e2SAndroid Build Coastguard Worker```
69*cc02d7e2SAndroid Build Coastguard Worker
70*cc02d7e2SAndroid Build Coastguard WorkerFor `.proto` files that are outside of the project directory a link can be added so that the files are visible in Visual Studio. E.g.
71*cc02d7e2SAndroid Build Coastguard Worker
72*cc02d7e2SAndroid Build Coastguard Worker```xml
73*cc02d7e2SAndroid Build Coastguard Worker<ItemGroup>
74*cc02d7e2SAndroid Build Coastguard Worker   <Protobuf Include="..\Proto\aggregate.proto" GrpcServices="Client" Link="Protos\aggregate.proto"/>
75*cc02d7e2SAndroid Build Coastguard Worker   <Protobuf Include="..\Proto\greet.proto" GrpcServices="None" Link="Protos\greet.proto"/>
76*cc02d7e2SAndroid Build Coastguard Worker   <Protobuf Include="..\Proto\count.proto" GrpcServices="None" Link="Protos\count.proto"/>
77*cc02d7e2SAndroid Build Coastguard Worker</ItemGroup>
78*cc02d7e2SAndroid Build Coastguard Worker```
79*cc02d7e2SAndroid Build Coastguard Worker
80*cc02d7e2SAndroid Build Coastguard WorkerFor more examples see the example project files in GitHub: https://github.com/grpc/grpc-dotnet/tree/master/examples
81*cc02d7e2SAndroid Build Coastguard Worker
82*cc02d7e2SAndroid Build Coastguard Worker## Sharing `.proto` files between multiple projects (in the same VS solution)
83*cc02d7e2SAndroid Build Coastguard Worker
84*cc02d7e2SAndroid Build Coastguard WorkerIt's common to want to share `.proto` files between projects. For example, a gRPC client
85*cc02d7e2SAndroid Build Coastguard Workerand gRPC server share the same contract. It is preferable to share contracts without
86*cc02d7e2SAndroid Build Coastguard Workercopying `.proto` files because copies can go out of sync over time.
87*cc02d7e2SAndroid Build Coastguard Worker
88*cc02d7e2SAndroid Build Coastguard WorkerThere are a couple of ways to use `.proto` files in multiple projects without duplication:
89*cc02d7e2SAndroid Build Coastguard Worker
90*cc02d7e2SAndroid Build Coastguard Worker* Sharing `.proto` files between projects with MSBuild links.
91*cc02d7e2SAndroid Build Coastguard Worker* Generating code in a class library and sharing the library.
92*cc02d7e2SAndroid Build Coastguard Worker
93*cc02d7e2SAndroid Build Coastguard Worker### Sharing `.proto` with MSBuild links
94*cc02d7e2SAndroid Build Coastguard Worker
95*cc02d7e2SAndroid Build Coastguard Worker`.proto` files can be placed in a shared location and referenced by multiple projects
96*cc02d7e2SAndroid Build Coastguard Workerusing MSBuild's [`Link` or `LinkBase` settings](https://learn.microsoft.com/visualstudio/msbuild/common-msbuild-item-metadata).
97*cc02d7e2SAndroid Build Coastguard Worker
98*cc02d7e2SAndroid Build Coastguard Worker```xml
99*cc02d7e2SAndroid Build Coastguard Worker<ItemGroup>
100*cc02d7e2SAndroid Build Coastguard Worker   <Protobuf Include="..\Protos\greet.proto" GrpcServices="None" Link="Protos\greet.proto"/>
101*cc02d7e2SAndroid Build Coastguard Worker</ItemGroup>
102*cc02d7e2SAndroid Build Coastguard Worker```
103*cc02d7e2SAndroid Build Coastguard Worker
104*cc02d7e2SAndroid Build Coastguard WorkerIn the example above, `greet.proto` is in a shared `Protos` directory outside
105*cc02d7e2SAndroid Build Coastguard Workerthe project directory. Multiple projects can reference the proto file.
106*cc02d7e2SAndroid Build Coastguard Worker
107*cc02d7e2SAndroid Build Coastguard Worker### Generating code in a class library
108*cc02d7e2SAndroid Build Coastguard Worker
109*cc02d7e2SAndroid Build Coastguard WorkerCreate a class library that references `.proto` files and contains generated code. The other
110*cc02d7e2SAndroid Build Coastguard Workerprojects in the solution can then reference this shared class library instead of each project
111*cc02d7e2SAndroid Build Coastguard Workerhaving to compile the same `.proto` files.
112*cc02d7e2SAndroid Build Coastguard Worker
113*cc02d7e2SAndroid Build Coastguard WorkerThe advantages of this are:
114*cc02d7e2SAndroid Build Coastguard Worker- The `.proto` only need to be compiled once.
115*cc02d7e2SAndroid Build Coastguard Worker- It prevents some projects getting multiple definitions of the same generated code, which can in turn break the build.
116*cc02d7e2SAndroid Build Coastguard Worker
117*cc02d7e2SAndroid Build Coastguard WorkerThere are a couple of examples in GitHub:
118*cc02d7e2SAndroid Build Coastguard Worker- The [Liber example](https://github.com/grpc/grpc-dotnet/tree/master/examples#liber)
119*cc02d7e2SAndroid Build Coastguard Worker  demonstrates how common protocol buffers messages can be compiled once and used in other projects:
120*cc02d7e2SAndroid Build Coastguard Worker  - The *Common* project creates a class library that includes the generates messages contained in `common.proto`
121*cc02d7e2SAndroid Build Coastguard Worker  - The *Client* and *Server* projects reference the *Common* project.
122*cc02d7e2SAndroid Build Coastguard Worker  - They do not need to recompile `common.proto` as those .NET types are already in
123*cc02d7e2SAndroid Build Coastguard Worker    the *Common* class library.
124*cc02d7e2SAndroid Build Coastguard Worker  - They do however each generate their own gRPC client or server code as both have a
125*cc02d7e2SAndroid Build Coastguard Worker    `<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.
126*cc02d7e2SAndroid Build Coastguard Worker
127*cc02d7e2SAndroid Build Coastguard Worker- The [RouteGuide example](https://github.com/grpc/grpc/tree/v1.46.x/examples/csharp/RouteGuide)
128*cc02d7e2SAndroid Build Coastguard Worker  demonstrates how the gRPC client and server code can be generated once and used in other
129*cc02d7e2SAndroid Build Coastguard Worker  projects:
130*cc02d7e2SAndroid Build Coastguard Worker  - **Note:** this example uses the *legacy c-core C#* packages, but the principles are the same
131*cc02d7e2SAndroid Build Coastguard Worker    for *gRPC for .NET* projects.
132*cc02d7e2SAndroid Build Coastguard Worker  - The *RouteGuide* project has a `<Protobuf>` reference to `route_guide.proto` and
133*cc02d7e2SAndroid Build Coastguard Worker    generates both the gRPC client and server code.
134*cc02d7e2SAndroid Build Coastguard Worker  - The *RouteGuideClient* and *RouteGuideServer* projects reference the *RouteGuide* project.
135*cc02d7e2SAndroid Build Coastguard Worker  - They do not need any `<Protobuf>` references since the code has already been
136*cc02d7e2SAndroid Build Coastguard Worker    generated in the *RouteGuide* project.
137*cc02d7e2SAndroid Build Coastguard Worker
138*cc02d7e2SAndroid Build Coastguard Worker
139*cc02d7e2SAndroid Build Coastguard Worker# Reference
140*cc02d7e2SAndroid Build Coastguard Worker
141*cc02d7e2SAndroid Build Coastguard Worker## Protobuf item metadata reference
142*cc02d7e2SAndroid Build Coastguard Worker
143*cc02d7e2SAndroid Build Coastguard WorkerThe following metadata are recognized on the `<Protobuf>` items.
144*cc02d7e2SAndroid Build Coastguard Worker
145*cc02d7e2SAndroid Build Coastguard Worker| Name           | Default   | Value                | Synopsis                         |
146*cc02d7e2SAndroid Build Coastguard Worker|----------------|-----------|----------------------|----------------------------------|
147*cc02d7e2SAndroid Build Coastguard Worker| Access         | `public`  | `public`, `internal`               | Generated class access           |
148*cc02d7e2SAndroid Build Coastguard Worker| AdditionalProtocArguments | | arbitrary cmdline arguments | Extra command line flags passed to `protoc` command. To specify multiple arguments use semi-colons (;) to separate them. See example below |
149*cc02d7e2SAndroid Build Coastguard Worker| ProtoCompile   | `true`    | `true`, `false`                    | If `false`, don't invoke `protoc` to generate code. |
150*cc02d7e2SAndroid Build Coastguard Worker| ProtoRoot      | See notes | A directory                        | Common root for set of files     |
151*cc02d7e2SAndroid Build Coastguard Worker| CompileOutputs | `true`    | `true`, `false`                    | If `false`, C# code will be generated, but it won't be included in the C# build. |
152*cc02d7e2SAndroid Build Coastguard Worker| OutputDir      | See notes | A directory                        | Directory for generated C# files with protobuf messages |
153*cc02d7e2SAndroid Build Coastguard Worker| OutputOptions  | | arbitrary options                  | Extra options passed to C# codegen as `--csharp_opt=opt1,opt2` |
154*cc02d7e2SAndroid Build Coastguard Worker| GrpcOutputDir  | See notes | A directory                        | Directory for generated gRPC stubs    |
155*cc02d7e2SAndroid Build Coastguard Worker| GrpcOutputOptions | | arbitrary options                  | Extra options passed to gRPC codegen as `--grpc_opt=opt1,opt2` |
156*cc02d7e2SAndroid Build Coastguard Worker| GrpcServices   | `Both`    | `None`, `Client`, `Server`, `Both` | Generated gRPC stubs             |
157*cc02d7e2SAndroid Build Coastguard Worker| AdditionalImportDirs | See notes | Directories                        | Specify additional directories in which to search for imports `.proto` files |
158*cc02d7e2SAndroid Build Coastguard Worker
159*cc02d7e2SAndroid Build Coastguard Worker__Notes__
160*cc02d7e2SAndroid Build Coastguard Worker
161*cc02d7e2SAndroid Build Coastguard Worker* __ProtoRoot__
162*cc02d7e2SAndroid Build Coastguard WorkerFor files _inside_ the project directory or its subdirectories, `ProtoRoot` is set by default to the
163*cc02d7e2SAndroid Build Coastguard Workerproject directory.
164*cc02d7e2SAndroid Build Coastguard Worker
165*cc02d7e2SAndroid Build Coastguard Worker    For files _outside_ of the project directory, the value
166*cc02d7e2SAndroid Build Coastguard Workeris set to the file's containing directory name, individually per file. If you
167*cc02d7e2SAndroid Build Coastguard Workerinclude a subtree of `.proto` files that lies outside of the project directory, you
168*cc02d7e2SAndroid Build Coastguard Workerneed to set `ProtoRoot`. There is an example of this below. The path in
169*cc02d7e2SAndroid Build Coastguard Workerthis variable is relative to the project directory.
170*cc02d7e2SAndroid Build Coastguard Worker
171*cc02d7e2SAndroid Build Coastguard Worker* __OutputDir__
172*cc02d7e2SAndroid Build Coastguard WorkerThe default value is the value of the property
173*cc02d7e2SAndroid Build Coastguard Worker`Protobuf_OutputPath`. This property, in turn, unless you set it in your
174*cc02d7e2SAndroid Build Coastguard Workerproject, will be set to the value of the standard MSBuild property
175*cc02d7e2SAndroid Build Coastguard Worker`IntermediateOutputPath`, which points to the location of compilation object
176*cc02d7e2SAndroid Build Coastguard Workeroutputs, such as `"obj/Release/netstandard1.5/"`. The path in this property is
177*cc02d7e2SAndroid Build Coastguard Workerconsidered relative to the project directory.
178*cc02d7e2SAndroid Build Coastguard Worker
179*cc02d7e2SAndroid Build Coastguard Worker* __GrpcOutputDir__
180*cc02d7e2SAndroid Build Coastguard WorkerUnless explicitly set, will follow `OutputDir` for any given file.
181*cc02d7e2SAndroid Build Coastguard Worker
182*cc02d7e2SAndroid Build Coastguard Worker* __Access__
183*cc02d7e2SAndroid Build Coastguard WorkerSets generated class access on _both_ generated message and gRPC stub classes.
184*cc02d7e2SAndroid Build Coastguard Worker
185*cc02d7e2SAndroid Build Coastguard Worker* __AdditionalProtocArguments__
186*cc02d7e2SAndroid Build Coastguard WorkerPass additional commandline arguments to the `protoc` command being invoked.
187*cc02d7e2SAndroid Build Coastguard WorkerNormally this option should not be used, but it exists for scenarios when you need to pass
188*cc02d7e2SAndroid Build Coastguard Workerotherwise unsupported (e.g. experimental) flags to protocol buffer compiler.
189*cc02d7e2SAndroid Build Coastguard Worker
190*cc02d7e2SAndroid Build Coastguard Worker* __OutputOptions__
191*cc02d7e2SAndroid Build Coastguard WorkerPass additional C# code generation options to `protoc` in the form `--csharp_opt=opt1,opt2`. See [C#-specific options](https://protobuf.dev/reference/csharp/csharp-generated/#compiler_options) for possible values.
192*cc02d7e2SAndroid Build Coastguard Worker
193*cc02d7e2SAndroid Build Coastguard Worker* __GrpcOutputOptions__
194*cc02d7e2SAndroid Build Coastguard WorkerPass additional options to the `grpc_csharp_plugin` in form of the `--grpc_opt` flag.
195*cc02d7e2SAndroid Build Coastguard WorkerNormally this option should not be used as its values are already controlled by `Access`
196*cc02d7e2SAndroid Build Coastguard Workerand `GrpcServices` metadata, but it might be useful in situations where you want
197*cc02d7e2SAndroid Build Coastguard Workerto explicitly pass some otherwise unsupported (e.g. experimental) options to the
198*cc02d7e2SAndroid Build Coastguard Worker`grpc_csharp_plugin`.
199*cc02d7e2SAndroid Build Coastguard Worker
200*cc02d7e2SAndroid Build Coastguard Worker* __AdditionalImportDirs__
201*cc02d7e2SAndroid Build Coastguard WorkerSpecify additional directories in which to search for imports in `.proto` files. The directories are searched in the order given. You may specify directories _outside_ of the
202*cc02d7e2SAndroid Build Coastguard Workerproject directory. The directories are passed to the `protoc` code generator via the `-I/--proto_path` option
203*cc02d7e2SAndroid Build Coastguard Workertogether with `Protobuf_StandardImportsPath` and `ProtoRoot` directories.
204*cc02d7e2SAndroid Build Coastguard Worker
205*cc02d7e2SAndroid Build Coastguard Worker__Specifying multiple values in properties__
206*cc02d7e2SAndroid Build Coastguard Worker
207*cc02d7e2SAndroid Build Coastguard WorkerSome properties allow you to specify multiple values in a list. The items in a list need to
208*cc02d7e2SAndroid Build Coastguard Workerbe separated by semi-colons (;). This is the syntax that MsBuild uses for lists.
209*cc02d7e2SAndroid Build Coastguard Worker
210*cc02d7e2SAndroid Build Coastguard WorkerThe properties that can have lists of items are: `OutputOptions`, `AdditionalProtocArguments`,
211*cc02d7e2SAndroid Build Coastguard Worker `GrpcOutputOptions`, `AdditionalImportDirs`
212*cc02d7e2SAndroid Build Coastguard Worker
213*cc02d7e2SAndroid Build Coastguard WorkerExample: to specify two additional arguments: `--plugin=protoc-gen-myplugin=D:\myplugin.exe --myplugin_out=.`
214*cc02d7e2SAndroid Build Coastguard Worker
215*cc02d7e2SAndroid Build Coastguard Worker```xml
216*cc02d7e2SAndroid Build Coastguard Worker  <ItemGroup>
217*cc02d7e2SAndroid Build Coastguard Worker    <Protobuf Include="proto_root/**/*.proto" ProtoRoot="proto_root"
218*cc02d7e2SAndroid Build Coastguard Worker              OutputDir="%(RelativeDir)" CompileOutputs="false"
219*cc02d7e2SAndroid Build Coastguard Worker              AdditionalProtocArguments="--plugin=protoc-gen-myplugin=D:\myplugin.exe;--myplugin_out=." />
220*cc02d7e2SAndroid Build Coastguard Worker  </ItemGroup>
221*cc02d7e2SAndroid Build Coastguard Worker```
222*cc02d7e2SAndroid Build Coastguard Worker
223*cc02d7e2SAndroid Build Coastguard Worker## `grpc_csharp_plugin` command line options
224*cc02d7e2SAndroid Build Coastguard Worker
225*cc02d7e2SAndroid Build Coastguard WorkerUnder the hood, the `Grpc.Tools` build integration invokes the `protoc` and `grpc_csharp_plugin` binaries
226*cc02d7e2SAndroid Build Coastguard Workerto perform code generation. Here is an overview of the available `grpc_csharp_plugin` options:
227*cc02d7e2SAndroid Build Coastguard Worker
228*cc02d7e2SAndroid Build Coastguard Worker| Name            | Default   | Synopsis                                                 |
229*cc02d7e2SAndroid Build Coastguard Worker|---------------- |-----------|----------------------------------------------------------|
230*cc02d7e2SAndroid Build Coastguard Worker| no_client       | off       | Don't generate the client stub                           |
231*cc02d7e2SAndroid Build Coastguard Worker| no_server       | off       | Don't generate the server-side stub                      |
232*cc02d7e2SAndroid Build Coastguard Worker| internal_access | off       | Generate classes with "internal" visibility              |
233*cc02d7e2SAndroid Build Coastguard Worker| file_suffix     | Grpc.cs   | The suffix that will get appended to the name of the generated file. **Can only be used on the command line.** |
234*cc02d7e2SAndroid Build Coastguard Worker| base_namespace | none       | *Experimental - may change or be removed.* Same as `base_namespace` for `protoc` [C# options](https://protobuf.dev/reference/csharp/csharp-generated/#compiler_options) .  **Can only be used on the command line.** |
235*cc02d7e2SAndroid Build Coastguard Worker
236*cc02d7e2SAndroid Build Coastguard WorkerTo use these options with `Grpc.Tools` specify them in the __GrpcOutputOptions__
237*cc02d7e2SAndroid Build Coastguard Workermetadata in the `<Protobuf>` item.
238*cc02d7e2SAndroid Build Coastguard Worker
239*cc02d7e2SAndroid Build Coastguard WorkerNotes:
240*cc02d7e2SAndroid Build Coastguard Worker- `file_suffix` and `base_namespace` should not be used with `Grpc.Tools`. Using them will break the build.
241*cc02d7e2SAndroid Build Coastguard Worker
242*cc02d7e2SAndroid Build Coastguard Worker- using `base_namespace` changes the algorithm for the generated file names to align it with the algorithm used by `protoc`.
243*cc02d7e2SAndroid Build Coastguard Worker
244*cc02d7e2SAndroid Build Coastguard Worker  This only affects files with punctuation or numbers in the name. E.g. `hello.world2d.proto` now generates file `HelloWorld2DGrpc.cs` instead of `Hello.world2dGrpc.cs`
245*cc02d7e2SAndroid Build Coastguard Worker
246*cc02d7e2SAndroid Build Coastguard WorkerTo use these options on the command line specify them with the `--grpc_opt`
247*cc02d7e2SAndroid Build Coastguard Workeroption.
248*cc02d7e2SAndroid Build Coastguard Worker
249*cc02d7e2SAndroid Build Coastguard WorkerCode generated by `protoc` is independent of the plugin and you may also need to specify C# options for this with `--csharp_opt`.
250*cc02d7e2SAndroid Build Coastguard WorkerThese are [documented here](https://protobuf.dev/reference/csharp/csharp-generated/#compiler_options).
251*cc02d7e2SAndroid Build Coastguard Worker
252*cc02d7e2SAndroid Build Coastguard Worker e.g.:
253*cc02d7e2SAndroid Build Coastguard Worker
254*cc02d7e2SAndroid Build Coastguard Worker```bash
255*cc02d7e2SAndroid Build Coastguard Workerprotoc --plugin=protoc-gen-grpc=grpc_csharp_plugin \
256*cc02d7e2SAndroid Build Coastguard Worker    --csharp_out=OUT_DIR \
257*cc02d7e2SAndroid Build Coastguard Worker    --csharp_opt=base_namespace=Example \
258*cc02d7e2SAndroid Build Coastguard Worker    --grpc_out=OUT_DIR \
259*cc02d7e2SAndroid Build Coastguard Worker    --grpc_opt=no_server,base_namespace=Example \
260*cc02d7e2SAndroid Build Coastguard Worker    -I INCLUDE_DIR foo.proto
261*cc02d7e2SAndroid Build Coastguard Worker```
262*cc02d7e2SAndroid Build Coastguard Worker## Environment Variables
263*cc02d7e2SAndroid Build Coastguard Worker
264*cc02d7e2SAndroid Build Coastguard WorkerEnvironment variables can be set to change the behavior of `Grpc.Tools` - setting the CPU architecture or operating system, or using custom built protocol buffers compiler and gRPC plugin.
265*cc02d7e2SAndroid Build Coastguard Worker
266*cc02d7e2SAndroid Build Coastguard Worker| Name                | Synopsis                                                                      |
267*cc02d7e2SAndroid Build Coastguard Worker|---------------------|-------------------------------------------------------------------------------|
268*cc02d7e2SAndroid Build Coastguard Worker|`PROTOBUF_TOOLS_OS`  | Operating system version of the tools to use: `linux`, `macosx`, or `windows` |
269*cc02d7e2SAndroid Build Coastguard Worker|`PROTOBUF_TOOLS_CPU` | CPU architecture version of the tools to use: `x86`, `x64`, or `arm64`        |
270*cc02d7e2SAndroid Build Coastguard Worker|`PROTOBUF_PROTOC`    | Full path to the protocol buffers compiler                                    |
271*cc02d7e2SAndroid Build Coastguard Worker|`GRPC_PROTOC_PLUGIN` | Full path to the grpc_csharp_plugin                                           |
272*cc02d7e2SAndroid Build Coastguard Worker
273*cc02d7e2SAndroid Build Coastguard WorkerFor example, to use a custom built protoc compiler and grpc_csharp_plugin:
274*cc02d7e2SAndroid Build Coastguard Worker```bash
275*cc02d7e2SAndroid Build Coastguard Workerexport PROTOBUF_PROTOC=$my_custom_build/protoc
276*cc02d7e2SAndroid Build Coastguard Workerexport GRPC_PROTOC_PLUGIN=$my_custom_build/grpc_csharp_plugin
277*cc02d7e2SAndroid Build Coastguard Workerdotnet build myproject.csproj
278*cc02d7e2SAndroid Build Coastguard Worker```
279*cc02d7e2SAndroid Build Coastguard Worker
280*cc02d7e2SAndroid Build Coastguard Worker## MSBuild Properties
281*cc02d7e2SAndroid Build Coastguard Worker
282*cc02d7e2SAndroid Build Coastguard WorkerYou can set some Properties in your project file or on the MSBuild command line. The
283*cc02d7e2SAndroid Build Coastguard Workerfollowing properties change the behavior of `Grpc.Tools`:
284*cc02d7e2SAndroid Build Coastguard Worker
285*cc02d7e2SAndroid Build Coastguard Worker| Name                | Synopsis                                                                      |
286*cc02d7e2SAndroid Build Coastguard Worker|---------------------|-------------------------------------------------------------------------------|
287*cc02d7e2SAndroid Build Coastguard Worker| `Protobuf_ToolsOs`  | Same as `PROTOBUF_TOOLS_OS` environment variable                              |
288*cc02d7e2SAndroid Build Coastguard Worker| `Protobuf_ToolsCpu` | Same as `PROTOBUF_TOOLS_CPU` environment variable                             |
289*cc02d7e2SAndroid Build Coastguard Worker| `Protobuf_ProtocFullPath` | Same as `PROTOBUF_PROTOC` environment variable                          |
290*cc02d7e2SAndroid Build Coastguard Worker| `gRPC_PluginFullPath` | Same as `GRPC_PROTOC_PLUGIN` environment variable                           |
291*cc02d7e2SAndroid Build Coastguard Worker| `Protobuf_NoWarnMissingExpected` | Default: `false`. If `true` then no warnings are given if expected files not generated. See example below for an explanation. |
292*cc02d7e2SAndroid Build Coastguard Worker| `Protobuf_OutputPath`| Default: `IntermediateOutputPath` - ususally the `obj` directory. Sets the default value for `OutputDir` on `<Protobuf>` items.|
293*cc02d7e2SAndroid Build Coastguard Worker| `EnableDefaultProtobufItems` | Default: `false`. If `true` then `.proto` files under the project are automatically included without the need to specify any `<Protobuf>` items. |
294*cc02d7e2SAndroid Build Coastguard Worker| `Protobuf_StandardImportsPath` | The path for protobuf's [well known types](https://protobuf.dev/reference/protobuf/google.protobuf/) included in the NuGet package. It is automcatically passed to `protoc`  via the `-I/--proto_path` option. |
295*cc02d7e2SAndroid Build Coastguard Worker
296*cc02d7e2SAndroid Build Coastguard Worker# Scenarios and Examples
297*cc02d7e2SAndroid Build Coastguard Worker
298*cc02d7e2SAndroid Build Coastguard WorkerFor other examples see also the `.csproj` files in the examples in GitHub:
299*cc02d7e2SAndroid Build Coastguard Worker* [grpc-dotnet examples](https://github.com/grpc/grpc-dotnet/tree/master/examples)
300*cc02d7e2SAndroid Build Coastguard Worker* [`Grpc.Core` examples](https://github.com/grpc/grpc/tree/v1.46.x/examples/csharp)
301*cc02d7e2SAndroid Build Coastguard Worker
302*cc02d7e2SAndroid Build Coastguard WorkerQuick links to the examples below:
303*cc02d7e2SAndroid Build Coastguard Worker
304*cc02d7e2SAndroid Build Coastguard Worker* [ProtoRoot - Common root for one or more `.proto` files](#ProtoRoot)
305*cc02d7e2SAndroid Build Coastguard Worker* [AdditionalImportDirs - Setting location of imported `.proto` files](#AdditionalImportDirs)
306*cc02d7e2SAndroid Build Coastguard Worker* [GrpcServices - Generating gRPC services and protocol buffers messages](#grpcservices)
307*cc02d7e2SAndroid Build Coastguard Worker* [Automatically including `.proto` files](#autoinclude)
308*cc02d7e2SAndroid Build Coastguard Worker* [Generate proto and gRPC C# sources from .proto files (no C# compile)](#nocompile)
309*cc02d7e2SAndroid Build Coastguard Worker* [Visual Studio: setting per-file `.proto` file options](#visualstudio)
310*cc02d7e2SAndroid Build Coastguard Worker* [Bypassing Grpc.Tools to run the protocol buffers compiler explicitly](#compiler)
311*cc02d7e2SAndroid Build Coastguard Worker* [Using Grpc.Tools with unsupported architectures](#unsupported-arch)
312*cc02d7e2SAndroid Build Coastguard Worker* [Including `.proto` files in NuGet packages](#proto-only-nuget)
313*cc02d7e2SAndroid Build Coastguard Worker
314*cc02d7e2SAndroid Build Coastguard Worker---
315*cc02d7e2SAndroid Build Coastguard Worker## <a name="ProtoRoot"></a>ProtoRoot - Common root for one or more `.proto` files
316*cc02d7e2SAndroid Build Coastguard Worker
317*cc02d7e2SAndroid Build Coastguard Worker`ProtoRoot` specifies a common directory that is an ancestor for a set of `.proto` files.
318*cc02d7e2SAndroid Build Coastguard Worker
319*cc02d7e2SAndroid Build Coastguard WorkerIt has two purposes:
320*cc02d7e2SAndroid Build Coastguard Worker* working out relative directories to preserve the structure when generating `.cs` files
321*cc02d7e2SAndroid Build Coastguard Worker* adding a directory to be searched for imported `.proto` files
322*cc02d7e2SAndroid Build Coastguard Worker
323*cc02d7e2SAndroid Build Coastguard WorkerThese are explained in an example below.
324*cc02d7e2SAndroid Build Coastguard Worker
325*cc02d7e2SAndroid Build Coastguard WorkerFor `.proto` files under the project directory `ProtoRoot` is by default set to `.`.
326*cc02d7e2SAndroid Build Coastguard WorkerIt can also be explicitly set.
327*cc02d7e2SAndroid Build Coastguard Worker
328*cc02d7e2SAndroid Build Coastguard WorkerFor `.proto` files outside of the project the value is set to the file's containing directory name.
329*cc02d7e2SAndroid Build Coastguard WorkerIf you include a subtree of `.proto` files then you must set `ProtoRoot` to give the
330*cc02d7e2SAndroid Build Coastguard Workerparent of the directory tree.
331*cc02d7e2SAndroid Build Coastguard Worker
332*cc02d7e2SAndroid Build Coastguard WorkerIn either case if you are importing a `.proto` file from within another file then you should set
333*cc02d7e2SAndroid Build Coastguard Worker`ProtoRoot` so that the import paths can be found. (See also `AdditionalImportDirs` below.)
334*cc02d7e2SAndroid Build Coastguard Worker
335*cc02d7e2SAndroid Build Coastguard WorkerGenerated files in the output directory will have the same directory structure as the
336*cc02d7e2SAndroid Build Coastguard Worker`.proto` files under `ProtoRoot`.
337*cc02d7e2SAndroid Build Coastguard Worker
338*cc02d7e2SAndroid Build Coastguard WorkerBy default the output directory for generated files is `obj\CONFIGURATION\FRAMEWORK\` (e.g. `obj\Debug\net6.0\`) unless `OutputDir` or `GrpcOutputDir` are specified.
339*cc02d7e2SAndroid Build Coastguard Worker### Example use of `ProtoRoot`
340*cc02d7e2SAndroid Build Coastguard WorkerSpecifying:
341*cc02d7e2SAndroid Build Coastguard Worker```xml
342*cc02d7e2SAndroid Build Coastguard Worker<Protobuf Include="Protos\Services\**\*.proto"
343*cc02d7e2SAndroid Build Coastguard Worker          ProtoRoot="Protos" />
344*cc02d7e2SAndroid Build Coastguard Worker<Protobuf Include="Protos\Messages\**\*.proto"
345*cc02d7e2SAndroid Build Coastguard Worker          ProtoRoot="Protos"
346*cc02d7e2SAndroid Build Coastguard Worker          GrpcServices="None" />
347*cc02d7e2SAndroid Build Coastguard Worker<Protobuf Include="..\OutsideProjectProtos\**\*.proto"
348*cc02d7e2SAndroid Build Coastguard Worker          ProtoRoot="..\OutsideProjectProtos" />
349*cc02d7e2SAndroid Build Coastguard Worker```
350*cc02d7e2SAndroid Build Coastguard Workerfor files:
351*cc02d7e2SAndroid Build Coastguard Worker```
352*cc02d7e2SAndroid Build Coastguard Worker	ProjectFolder\Protos\Services\v1\hello.proto
353*cc02d7e2SAndroid Build Coastguard Worker	ProjectFolder\Protos\Services\v2\hello.proto
354*cc02d7e2SAndroid Build Coastguard Worker	ProjectFolder\Protos\Messages\v1\message.proto
355*cc02d7e2SAndroid Build Coastguard Worker	..\OutsideProjectProtos\MyApi\alpha.proto
356*cc02d7e2SAndroid Build Coastguard Worker	..\OutsideProjectProtos\MyApi\beta.proto
357*cc02d7e2SAndroid Build Coastguard Worker```
358*cc02d7e2SAndroid Build Coastguard Workerwill generate files:
359*cc02d7e2SAndroid Build Coastguard Worker```
360*cc02d7e2SAndroid Build Coastguard Worker  ProjectFolder\obj\Debug\net6.0\Services\v1\Hello.cs
361*cc02d7e2SAndroid Build Coastguard Worker  ProjectFolder\obj\Debug\net6.0\Services\v1\HelloGrpc.cs
362*cc02d7e2SAndroid Build Coastguard Worker  ProjectFolder\obj\Debug\net6.0\Services\v2\Hello.cs
363*cc02d7e2SAndroid Build Coastguard Worker  ProjectFolder\obj\Debug\net6.0\Services\v2\HelloGrpc.cs
364*cc02d7e2SAndroid Build Coastguard Worker  ProjectFolder\obj\Debug\net6.0\Messages\v1\Message.cs
365*cc02d7e2SAndroid Build Coastguard Worker  ProjectFolder\obj\Debug\net6.0\MyApi\Alpha.cs
366*cc02d7e2SAndroid Build Coastguard Worker  ProjectFolder\obj\Debug\net6.0\MyApi\AlphaGrpc.cs
367*cc02d7e2SAndroid Build Coastguard Worker  ProjectFolder\obj\Debug\net6.0\MyApi\Beta.cs
368*cc02d7e2SAndroid Build Coastguard Worker  ProjectFolder\obj\Debug\net6.0\MyApi\BetaGrpc.cs
369*cc02d7e2SAndroid Build Coastguard Worker
370*cc02d7e2SAndroid Build Coastguard Worker```
371*cc02d7e2SAndroid Build Coastguard WorkerThings to notes:
372*cc02d7e2SAndroid Build Coastguard Worker* the directory structures under `ProjectFolder\Protos\` and `..\OutsideProjectProtos\` are mirrored in the output directory.
373*cc02d7e2SAndroid Build Coastguard Worker* the import search paths passed to `protoc` via `-I/--proto_path` option will include
374*cc02d7e2SAndroid Build Coastguard Worker `ProjectFolder\Protos` and `..\OutsideProjectProtos`
375*cc02d7e2SAndroid Build Coastguard Worker---
376*cc02d7e2SAndroid Build Coastguard Worker## <a name="AdditionalImportDirs"></a>AdditionalImportDirs - Setting location of imported `.proto` files
377*cc02d7e2SAndroid Build Coastguard Worker
378*cc02d7e2SAndroid Build Coastguard WorkerIn addition to specifying `ProtoRoot` other import directories can be specified for
379*cc02d7e2SAndroid Build Coastguard Workerdirectories to search when importing `.proto` files by specifying `AdditionalImportDirs`
380*cc02d7e2SAndroid Build Coastguard Workerand provide a list of directories. The directories are searched in the order given.
381*cc02d7e2SAndroid Build Coastguard Worker
382*cc02d7e2SAndroid Build Coastguard WorkerYou would use this when you want to import `.proto` files that you don't need to
383*cc02d7e2SAndroid Build Coastguard Workerseparately compile as they are only used in import statements. E.g.:
384*cc02d7e2SAndroid Build Coastguard Worker
385*cc02d7e2SAndroid Build Coastguard Worker```xml
386*cc02d7e2SAndroid Build Coastguard Worker  <Protobuf Include="protos/*.proto"
387*cc02d7e2SAndroid Build Coastguard Worker            ProtoRoot="protos"
388*cc02d7e2SAndroid Build Coastguard Worker            AdditionalImportDirs="/folder/protos/mytypes/;/another/folder/"
389*cc02d7e2SAndroid Build Coastguard Worker      ... />
390*cc02d7e2SAndroid Build Coastguard Worker```
391*cc02d7e2SAndroid Build Coastguard Worker
392*cc02d7e2SAndroid Build Coastguard WorkerNote: The path for protobuf's [well known types](https://protobuf.dev/reference/protobuf/google.protobuf/) is automatically included. E.g. the `import` below will work without having to explicity specifying the path in `AdditionalImportDirs`:
393*cc02d7e2SAndroid Build Coastguard Worker```protobuf
394*cc02d7e2SAndroid Build Coastguard Workerimport "google/protobuf/wrappers.proto";
395*cc02d7e2SAndroid Build Coastguard Worker```
396*cc02d7e2SAndroid Build Coastguard Worker---
397*cc02d7e2SAndroid Build Coastguard Worker## <a name="grpcservices"></a>GrpcServices - Generating gRPC services and protocol buffers messages
398*cc02d7e2SAndroid Build Coastguard WorkerThe protocol buffers files (`.proto` files) define both the service interface and the
399*cc02d7e2SAndroid Build Coastguard Workerstructure of the payload messages.
400*cc02d7e2SAndroid Build Coastguard Worker
401*cc02d7e2SAndroid Build Coastguard WorkerTwo `.cs` file can be generated from a `.proto` file. For example, if the `.proto` file
402*cc02d7e2SAndroid Build Coastguard Workeris `myfile.proto` then the two possible files are:
403*cc02d7e2SAndroid Build Coastguard Worker* `Myfile.cs` - contains the generated code for protocol buffers messages
404*cc02d7e2SAndroid Build Coastguard Worker* `MyfileGrpc.cs`  - contains the generated code for gRPC client and/or server
405*cc02d7e2SAndroid Build Coastguard Worker
406*cc02d7e2SAndroid Build Coastguard WorkerWhen a `.proto` file contains service definitions the protocol buffers compiler calls
407*cc02d7e2SAndroid Build Coastguard Workerthe gRPC plugin to generate gRPC client and/or server stub code. Whether or not the `*Grpc.cs` file
408*cc02d7e2SAndroid Build Coastguard Workeris generated and what it contains is controlled by the `GrpcServices` metadata
409*cc02d7e2SAndroid Build Coastguard Workeron the `<Protobuf>` item.
410*cc02d7e2SAndroid Build Coastguard Worker
411*cc02d7e2SAndroid Build Coastguard Worker* `GrpcServices="Both"` (the default) - `Myfile.cs` and `MyfileGrpc.cs` generated
412*cc02d7e2SAndroid Build Coastguard Worker* `GrpcServices="None"` - just `Myfile.cs` generated
413*cc02d7e2SAndroid Build Coastguard Worker* `GrpcServices="Client"` - `Myfile.cs` and `MyfileGrpc.cs` (just client code)
414*cc02d7e2SAndroid Build Coastguard Worker* `GrpcServices="Server"` - `Myfile.cs` and `MyfileGrpc.cs` (just server code)
415*cc02d7e2SAndroid Build Coastguard Worker
416*cc02d7e2SAndroid Build Coastguard WorkerHowever when a `.proto` **does not file contains any service definitions** but only contains
417*cc02d7e2SAndroid Build Coastguard Workermessage definitions then an empty (zero length) `MyfileGrpc.cs` may still be created
418*cc02d7e2SAndroid Build Coastguard Workerby `Grpc.Tools` unless the `.proto` file is specified with `GrpcServices="None"` in the project file.
419*cc02d7e2SAndroid Build Coastguard Worker
420*cc02d7e2SAndroid Build Coastguard WorkerThis is because `Grpc.Tools` has no way of knowing in advanced of running the protocol buffers
421*cc02d7e2SAndroid Build Coastguard Workercompiler whether a `.proto` file has a service clause. It creates the empty files as a marker
422*cc02d7e2SAndroid Build Coastguard Workerfor incremental builds so that the `.proto` files are not unnecessarily recompiled. Empty files
423*cc02d7e2SAndroid Build Coastguard Workerare not a problem on a small project but you may wish to avoid them on a larger project.
424*cc02d7e2SAndroid Build Coastguard Worker
425*cc02d7e2SAndroid Build Coastguard WorkerTherefore it is better to explicitly mark files with the correct `GrpcServices` metadata if you can. For
426*cc02d7e2SAndroid Build Coastguard Workerexample:
427*cc02d7e2SAndroid Build Coastguard Worker
428*cc02d7e2SAndroid Build Coastguard Worker```xml
429*cc02d7e2SAndroid Build Coastguard Worker<ItemGroup>
430*cc02d7e2SAndroid Build Coastguard Worker  <Protobuf Include="**/*.proto" GrpcServices="None" />
431*cc02d7e2SAndroid Build Coastguard Worker  <Protobuf Update="**/hello/*.proto;**/bye/*.proto" GrpcServices="Both" />
432*cc02d7e2SAndroid Build Coastguard Worker</ItemGroup>
433*cc02d7e2SAndroid Build Coastguard Worker```
434*cc02d7e2SAndroid Build Coastguard WorkerIn the above example all `.proto` files are compiled with `GrpcServices="None"`, except for `.proto`
435*cc02d7e2SAndroid Build Coastguard Workerfiles in subdirectories on any tree level named `hello` and `bye`, which will take
436*cc02d7e2SAndroid Build Coastguard Worker`GrpcServices="Both"`. Note the use of the `Update` attribute instead of `Include` - otherwise
437*cc02d7e2SAndroid Build Coastguard Workerthe files would be added twice.
438*cc02d7e2SAndroid Build Coastguard Worker
439*cc02d7e2SAndroid Build Coastguard WorkerAnother example would be the use of globbing if your service `.proto` files are named according
440*cc02d7e2SAndroid Build Coastguard Workerto a pattern, for example `*_services.proto`. In this case the `Update` attribute can be written
441*cc02d7e2SAndroid Build Coastguard Workeras `Update="**/*_service.proto"` to set the attribute `GrpcServices="Both"` only on these files.
442*cc02d7e2SAndroid Build Coastguard Worker
443*cc02d7e2SAndroid Build Coastguard Worker### Seeing a warning about a missing expected file
444*cc02d7e2SAndroid Build Coastguard Worker
445*cc02d7e2SAndroid Build Coastguard WorkerYou will see the warning message:
446*cc02d7e2SAndroid Build Coastguard Worker ```
447*cc02d7e2SAndroid Build Coastguard Worker Some expected protoc outputs were not generated
448*cc02d7e2SAndroid Build Coastguard Worker ```
449*cc02d7e2SAndroid Build Coastguard Worker if all these are true:
450*cc02d7e2SAndroid Build Coastguard Worker * the location for the generated files is configured to a directory outside of the project, e.g. `OutputDir="..\outside-project\"`
451*cc02d7e2SAndroid Build Coastguard Worker * `*Grpc.cs` files have not been created because the `.proto` file does not contain a
452*cc02d7e2SAndroid Build Coastguard Workerservice definintion
453*cc02d7e2SAndroid Build Coastguard Worker* you have not specified `GrpcServices="None"`
454*cc02d7e2SAndroid Build Coastguard Worker
455*cc02d7e2SAndroid Build Coastguard WorkerThis is because `Grpc.Tools` only creates empty `*Grpc.cs` files in directories
456*cc02d7e2SAndroid Build Coastguard Workerwithin the project (such as the intermediate `obj` directory). Empty files are not created
457*cc02d7e2SAndroid Build Coastguard Workeroutside of the project directory so as not to pollute non-project directories.
458*cc02d7e2SAndroid Build Coastguard Worker
459*cc02d7e2SAndroid Build Coastguard WorkerThis warning can be suppressed by setting the MSBuild property:
460*cc02d7e2SAndroid Build Coastguard Worker```xml
461*cc02d7e2SAndroid Build Coastguard Worker<PropertyGroup>
462*cc02d7e2SAndroid Build Coastguard Worker  <Protobuf_NoWarnMissingExpected>true</Protobuf_NoWarnMissingExpected>
463*cc02d7e2SAndroid Build Coastguard Worker</PropertyGroup>
464*cc02d7e2SAndroid Build Coastguard Worker```
465*cc02d7e2SAndroid Build Coastguard Workerhowever it is better to set `GrpcServices="None"` on the `.proto` files affected to avoid
466*cc02d7e2SAndroid Build Coastguard Workerunnecessary rebuilds.
467*cc02d7e2SAndroid Build Coastguard Worker
468*cc02d7e2SAndroid Build Coastguard Worker
469*cc02d7e2SAndroid Build Coastguard Worker---
470*cc02d7e2SAndroid Build Coastguard Worker## <a name="autoinclude"></a>Automatically including `.proto` files
471*cc02d7e2SAndroid Build Coastguard Worker
472*cc02d7e2SAndroid Build Coastguard WorkerFor SDK projects it is possible to automatically include `.proto` files found in the project
473*cc02d7e2SAndroid Build Coastguard Workerdirectory or sub-directories, without having to specify them with a `<Protobuf>` item.
474*cc02d7e2SAndroid Build Coastguard WorkerTo do this the property `EnableDefaultProtobufItems` has be set to `true` in the project file or on the MSBuild command line.
475*cc02d7e2SAndroid Build Coastguard Worker
476*cc02d7e2SAndroid Build Coastguard WorkerIt is recommended that you do not rely on automatic inclusion of `.proto` files except for
477*cc02d7e2SAndroid Build Coastguard Workerthe simplest of projects since it does not allow you to control other settings such as
478*cc02d7e2SAndroid Build Coastguard Worker`GrpcServices`.
479*cc02d7e2SAndroid Build Coastguard Worker
480*cc02d7e2SAndroid Build Coastguard WorkerBy default `EnableDefaultProtobufItems` is not set and `<Protobuf>` items must be included
481*cc02d7e2SAndroid Build Coastguard Workerin the project for the `.proto` files to be compiled.
482*cc02d7e2SAndroid Build Coastguard Worker
483*cc02d7e2SAndroid Build Coastguard Worker---
484*cc02d7e2SAndroid Build Coastguard Worker
485*cc02d7e2SAndroid Build Coastguard Worker## <a name="nocompile"></a>Generate proto and gRPC C# sources from .proto files (no C# compile)
486*cc02d7e2SAndroid Build Coastguard Worker
487*cc02d7e2SAndroid Build Coastguard WorkerIf you just want to generate the C# sources from `.proto` files without compiling the C# files
488*cc02d7e2SAndroid Build Coastguard Worker (e.g. for use in other projects) then you can do something similar to this to a `.csproj` file:
489*cc02d7e2SAndroid Build Coastguard Worker ```xml
490*cc02d7e2SAndroid Build Coastguard Worker<ItemGroup>
491*cc02d7e2SAndroid Build Coastguard Worker  <Protobuf Include="**/*.proto"
492*cc02d7e2SAndroid Build Coastguard Worker      OutputDir="%(RelativeDir)" CompileOutputs="false"  />
493*cc02d7e2SAndroid Build Coastguard Worker</ItemGroup>
494*cc02d7e2SAndroid Build Coastguard Worker```
495*cc02d7e2SAndroid Build Coastguard Worker
496*cc02d7e2SAndroid Build Coastguard Worker* `Include` tells the build system to recursively examine project directory and its
497*cc02d7e2SAndroid Build Coastguard Workersubdirectories (**) include all files matching the wildcard `*.proto`.
498*cc02d7e2SAndroid Build Coastguard Worker* `OutputDir="%(RelativeDir)"` makes the output directory for each `.cs` file to be
499*cc02d7e2SAndroid Build Coastguard Workersame as the corresponding `.proto` directory.
500*cc02d7e2SAndroid Build Coastguard Worker* `CompileOutputs="false"` prevents compiling the generated files into an assembly.
501*cc02d7e2SAndroid Build Coastguard Worker
502*cc02d7e2SAndroid Build Coastguard WorkerNote that an empty assembly is still generated which can be ignored.
503*cc02d7e2SAndroid Build Coastguard Worker
504*cc02d7e2SAndroid Build Coastguard WorkerNOTE: To start with an empty project to add your `.proto` files to you can do the following
505*cc02d7e2SAndroid Build Coastguard Workerat a command prompt:
506*cc02d7e2SAndroid Build Coastguard Worker```bash
507*cc02d7e2SAndroid Build Coastguard Workerdotnet new classlib
508*cc02d7e2SAndroid Build Coastguard Workerrm *.cs              # remove .cs files - for Windows the command is: del *.cs /y
509*cc02d7e2SAndroid Build Coastguard Workerdotnet add package Grpc.Tools
510*cc02d7e2SAndroid Build Coastguard Worker```
511*cc02d7e2SAndroid Build Coastguard Worker
512*cc02d7e2SAndroid Build Coastguard Worker---
513*cc02d7e2SAndroid Build Coastguard Worker## <a name="visualstudio"></a>Visual Studio: setting per-file `.proto` file options
514*cc02d7e2SAndroid Build Coastguard Worker
515*cc02d7e2SAndroid Build Coastguard WorkerIn Visual Studio it is possible to set some frequently used per-file options on `.proto` files
516*cc02d7e2SAndroid Build Coastguard Workerwithout editing the `.csproj` file directly. However editing the `.csproj` gives you more
517*cc02d7e2SAndroid Build Coastguard Workerflexibilty.
518*cc02d7e2SAndroid Build Coastguard Worker
519*cc02d7e2SAndroid Build Coastguard Worker### "dotnet SDK" projects
520*cc02d7e2SAndroid Build Coastguard Worker
521*cc02d7e2SAndroid Build Coastguard WorkerFor a "dotnet SDK" project, you have more control of some frequently used options.
522*cc02d7e2SAndroid Build Coastguard Worker**You may need to open and close Visual Studio** for this form to appear in the
523*cc02d7e2SAndroid Build Coastguard Workerproperties window after adding a reference to `Grpc.Tools` package:
524*cc02d7e2SAndroid Build Coastguard Worker
525*cc02d7e2SAndroid Build Coastguard Worker![Properties in an SDK project](doc/integration.md-fig.2-sdk.png)
526*cc02d7e2SAndroid Build Coastguard Worker
527*cc02d7e2SAndroid Build Coastguard WorkerYou can also change options of multiple files at once by selecting them in the
528*cc02d7e2SAndroid Build Coastguard WorkerProject Explorer together.
529*cc02d7e2SAndroid Build Coastguard Worker
530*cc02d7e2SAndroid Build Coastguard Worker### "classic" projects
531*cc02d7e2SAndroid Build Coastguard Worker
532*cc02d7e2SAndroid Build Coastguard WorkerFor a "classic" project, you can only add `.proto` files with all options set to
533*cc02d7e2SAndroid Build Coastguard Workerdefault. Click on the "show all files" button, add files to project, then
534*cc02d7e2SAndroid Build Coastguard Workerchange file type of the `.proto` files to "Protobuf" in the Properties window
535*cc02d7e2SAndroid Build Coastguard Workerdrop-down. This menu item will appear after you import the `Grpc.Tools` package:
536*cc02d7e2SAndroid Build Coastguard Worker
537*cc02d7e2SAndroid Build Coastguard Worker![Properties in a classic project](doc/integration.md-fig.1-classic.png)
538*cc02d7e2SAndroid Build Coastguard Worker
539*cc02d7e2SAndroid Build Coastguard Worker---
540*cc02d7e2SAndroid Build Coastguard Worker## <a name="compiler"></a>Bypassing Grpc.Tools to run the protocol buffers compiler explicitly
541*cc02d7e2SAndroid Build Coastguard Worker
542*cc02d7e2SAndroid Build Coastguard WorkerIt is possible to bypass all the build logic in `Grpc.Tools` and run the protocol buffers compiler
543*cc02d7e2SAndroid Build Coastguard Workerexplicitly in your project file, and just use the `Grpc.Tools` as a means of getting the compiler.
544*cc02d7e2SAndroid Build Coastguard Worker**This is not recommended** but there may be situations where you want to do this.
545*cc02d7e2SAndroid Build Coastguard Worker
546*cc02d7e2SAndroid Build Coastguard WorkerYou can use the following Properties:
547*cc02d7e2SAndroid Build Coastguard Worker* `Protobuf_ProtocFullPath` points to the full path and filename of protoc executable, e.g.
548*cc02d7e2SAndroid Build Coastguard Worker  `"...\.nuget\packages\grpc.tools\2.51.0\build\native\bin\windows\protoc.exe"`
549*cc02d7e2SAndroid Build Coastguard Worker
550*cc02d7e2SAndroid Build Coastguard Worker* `gRPC_PluginFullPath` points to the full path and filename of gRPC plugin, e.g.
551*cc02d7e2SAndroid Build Coastguard Worker  `"...\.nuget\packages\grpc.tools\2.51.0\build\native\bin\windows\grpc_csharp_plugin.exe"`
552*cc02d7e2SAndroid Build Coastguard Worker
553*cc02d7e2SAndroid Build Coastguard Worker* `Protobuf_StandardImportsPath` points to the standard proto import directory, e.g.
554*cc02d7e2SAndroid Build Coastguard Worker  `"...\.nuget\packages\grpc.tools\2.51.0\build\native\include"`. This is
555*cc02d7e2SAndroid Build Coastguard Worker  the directory where a declaration such as `import "google/protobuf/wrappers.proto";`
556*cc02d7e2SAndroid Build Coastguard Worker  in a proto file would find its target.
557*cc02d7e2SAndroid Build Coastguard Worker
558*cc02d7e2SAndroid Build Coastguard Workerthen in your project file:
559*cc02d7e2SAndroid Build Coastguard Worker
560*cc02d7e2SAndroid Build Coastguard Worker```xml
561*cc02d7e2SAndroid Build Coastguard Worker  <Target Name="MyProtoCompile">
562*cc02d7e2SAndroid Build Coastguard Worker    <PropertyGroup>
563*cc02d7e2SAndroid Build Coastguard Worker      <ProtoCCommand>$(Protobuf_ProtocFullPath) --plugin=protoc-gen-grpc=$(gRPC_PluginFullPath)  -I $(Protobuf_StandardImportsPath) ....rest of your command.... </ProtoCCommand>
564*cc02d7e2SAndroid Build Coastguard Worker    </PropertyGroup>
565*cc02d7e2SAndroid Build Coastguard Worker    <Message Importance="high" Text="$(ProtoCCommand)" />
566*cc02d7e2SAndroid Build Coastguard Worker    <Exec Command="$(ProtoCCommand)" />
567*cc02d7e2SAndroid Build Coastguard Worker  </Target>
568*cc02d7e2SAndroid Build Coastguard Worker```
569*cc02d7e2SAndroid Build Coastguard WorkerDo not include any `<Protobuf>` items in the project file as that will invoke the `Grpc.Tools` build and your files will be compiled twice.
570*cc02d7e2SAndroid Build Coastguard Worker
571*cc02d7e2SAndroid Build Coastguard Worker---
572*cc02d7e2SAndroid Build Coastguard Worker
573*cc02d7e2SAndroid Build Coastguard Worker## <a name="unsupported-arch"></a>Using Grpc.Tools with unsupported architectures
574*cc02d7e2SAndroid Build Coastguard Worker
575*cc02d7e2SAndroid Build Coastguard WorkerYou may still use the MSBuild integration provided by `Grpc.Tools` for architectures where
576*cc02d7e2SAndroid Build Coastguard Workerthe binaries are not included in the `Grpc.Tools` NuGet package.
577*cc02d7e2SAndroid Build Coastguard Worker
578*cc02d7e2SAndroid Build Coastguard WorkerIf you are able to build your own binaries for `protoc` and `grpc_csharp_plugin`, or
579*cc02d7e2SAndroid Build Coastguard Workerfind pre-built binaries provided by the community, then you can define a couple of
580*cc02d7e2SAndroid Build Coastguard Workerenvironment variables to tell `Grpc.Tools` to use those binaries instead of the ones
581*cc02d7e2SAndroid Build Coastguard Workerprovided in the NuGet package:
582*cc02d7e2SAndroid Build Coastguard Worker* `PROTOBUF_PROTOC`  - Full path to the protocol buffers compiler
583*cc02d7e2SAndroid Build Coastguard Worker* `GRPC_PROTOC_PLUGIN` - Full path to the `grpc_csharp_plugin`
584*cc02d7e2SAndroid Build Coastguard Worker
585*cc02d7e2SAndroid Build Coastguard WorkerThings to note:
586*cc02d7e2SAndroid Build Coastguard Worker*  You need `Grpc.Tools` version 2.50.0 or later for these environment variables to
587*cc02d7e2SAndroid Build Coastguard Workerbe recognised.
588*cc02d7e2SAndroid Build Coastguard Worker
589*cc02d7e2SAndroid Build Coastguard Worker* The binaries bundled in `Grpc.Tools` already ensure that the correct and mutually
590*cc02d7e2SAndroid Build Coastguard Workercompatible version of `protoc` and `grpc_csharp_plugin` will be chosen, but when
591*cc02d7e2SAndroid Build Coastguard Workerproviding them yourself, you're in charge.
592*cc02d7e2SAndroid Build Coastguard Worker
593*cc02d7e2SAndroid Build Coastguard Worker* If the versions of `protoc` and `grpc_csharp_plugin` you provide are mutually
594*cc02d7e2SAndroid Build Coastguard Workerincompatible then code generated may not work with your application (e.g. breaks
595*cc02d7e2SAndroid Build Coastguard Workerthe build).
596*cc02d7e2SAndroid Build Coastguard Worker
597*cc02d7e2SAndroid Build Coastguard Worker* Specifically, older version of plugins may generate incompatible code
598*cc02d7e2SAndroid Build Coastguard Workeror may not contain patches/fixes.
599*cc02d7e2SAndroid Build Coastguard Worker
600*cc02d7e2SAndroid Build Coastguard Worker_An example for Alpine Linux_
601*cc02d7e2SAndroid Build Coastguard Worker
602*cc02d7e2SAndroid Build Coastguard WorkerFor Alpine Linux (which uses the *musl* C standard library) there are community
603*cc02d7e2SAndroid Build Coastguard Workerprovided packages for the protocol buffers compiler and gRPC plugins:
604*cc02d7e2SAndroid Build Coastguard Worker[https://pkgs.alpinelinux.org/packages?name=grpc-plugins](https://pkgs.alpinelinux.org/packages?name=grpc-plugins)
605*cc02d7e2SAndroid Build Coastguard Worker
606*cc02d7e2SAndroid Build Coastguard WorkerTo use these:
607*cc02d7e2SAndroid Build Coastguard Worker```bash
608*cc02d7e2SAndroid Build Coastguard Worker# Build or install the binaries for your architecture.
609*cc02d7e2SAndroid Build Coastguard Worker
610*cc02d7e2SAndroid Build Coastguard Worker# e.g. for Alpine Linux the grpc-plugins package can be used
611*cc02d7e2SAndroid Build Coastguard Worker#  See https://pkgs.alpinelinux.org/package/edge/community/x86_64/grpc-plugins
612*cc02d7e2SAndroid Build Coastguard Workerapk add grpc-plugins  # Alpine Linux specific package installer
613*cc02d7e2SAndroid Build Coastguard Worker
614*cc02d7e2SAndroid Build Coastguard Worker# Set environment variables for the built/installed protoc
615*cc02d7e2SAndroid Build Coastguard Worker# and grpc_csharp_plugin binaries
616*cc02d7e2SAndroid Build Coastguard Workerexport PROTOBUF_PROTOC=/usr/bin/protoc
617*cc02d7e2SAndroid Build Coastguard Workerexport GRPC_PROTOC_PLUGIN=/usr/bin/grpc_csharp_plugin
618*cc02d7e2SAndroid Build Coastguard Worker
619*cc02d7e2SAndroid Build Coastguard Worker# When the dotnet build runs the Grpc.Tools NuGet package will
620*cc02d7e2SAndroid Build Coastguard Worker# use the binaries pointed to by the environment variables
621*cc02d7e2SAndroid Build Coastguard Workerdotnet build
622*cc02d7e2SAndroid Build Coastguard Worker```
623*cc02d7e2SAndroid Build Coastguard Worker
624*cc02d7e2SAndroid Build Coastguard Worker---
625*cc02d7e2SAndroid Build Coastguard Worker
626*cc02d7e2SAndroid Build Coastguard Worker## <a name="proto-only-nuget"></a>Including `.proto` files in NuGet packages
627*cc02d7e2SAndroid Build Coastguard Worker
628*cc02d7e2SAndroid Build Coastguard WorkerThere might be occassions when you are given a NuGet package that contains
629*cc02d7e2SAndroid Build Coastguard Worker`.proto` files that you wish to use in your own project, or you may wish to
630*cc02d7e2SAndroid Build Coastguard Workerpackage your own `.proto` files in a NuGet package for others to use.
631*cc02d7e2SAndroid Build Coastguard Worker
632*cc02d7e2SAndroid Build Coastguard WorkerThere is no automatic way for `Grpc.Tools` to locate and include `.proto`
633*cc02d7e2SAndroid Build Coastguard Workerfiles from other NuGet packages. Below is a suggested convention to use
634*cc02d7e2SAndroid Build Coastguard Workerwhen creating NuGet packages that contain `.proto` files.
635*cc02d7e2SAndroid Build Coastguard Worker
636*cc02d7e2SAndroid Build Coastguard Worker__Note:__ This is not the same as a NuGet package providing a library built from
637*cc02d7e2SAndroid Build Coastguard Workerthe code generated from the `.proto` files. Below just describes how to provide
638*cc02d7e2SAndroid Build Coastguard Workerthe uncompiled `.proto` files in a NuGet package and have `Grpc.Tools` automatically
639*cc02d7e2SAndroid Build Coastguard Workeruse them.
640*cc02d7e2SAndroid Build Coastguard Worker
641*cc02d7e2SAndroid Build Coastguard Worker### Creating the NuGet package
642*cc02d7e2SAndroid Build Coastguard Worker
643*cc02d7e2SAndroid Build Coastguard WorkerThe NuGet package should:
644*cc02d7e2SAndroid Build Coastguard Worker- provide the `.proto` files in a `content\protos` subdirectory in the package
645*cc02d7e2SAndroid Build Coastguard Worker- provide a `packagename.targets` file in the `build` subdirectory in the package that:
646*cc02d7e2SAndroid Build Coastguard Worker  - defines an MSBuild property giving the path to the `.proto` files in the
647*cc02d7e2SAndroid Build Coastguard Worker    installed package
648*cc02d7e2SAndroid Build Coastguard Worker  - conditionally updates the `Protobuf_StandardImportsPath` property with the
649*cc02d7e2SAndroid Build Coastguard Worker    above path so that the files can be found by the protocol buffers compiler
650*cc02d7e2SAndroid Build Coastguard Worker    - it should be made optional forcing users to *opt in* to including
651*cc02d7e2SAndroid Build Coastguard Worker      the `.proto` files
652*cc02d7e2SAndroid Build Coastguard Worker
653*cc02d7e2SAndroid Build Coastguard WorkerFor example, for the package `My.Example.Protos`:
654*cc02d7e2SAndroid Build Coastguard Worker
655*cc02d7e2SAndroid Build Coastguard WorkerMy.Example.Protos.nuspec:
656*cc02d7e2SAndroid Build Coastguard Worker```xml
657*cc02d7e2SAndroid Build Coastguard Worker<?xml version="1.0"?>
658*cc02d7e2SAndroid Build Coastguard Worker<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
659*cc02d7e2SAndroid Build Coastguard Worker  <metadata>
660*cc02d7e2SAndroid Build Coastguard Worker    <id>My.Example.Protos</id>
661*cc02d7e2SAndroid Build Coastguard Worker    <version>1.0.0</version>
662*cc02d7e2SAndroid Build Coastguard Worker    <title>Example package containing proto files</title>
663*cc02d7e2SAndroid Build Coastguard Worker    <authors>author</authors>
664*cc02d7e2SAndroid Build Coastguard Worker    <owners>owner</owners>
665*cc02d7e2SAndroid Build Coastguard Worker    <licenseUrl>license url</licenseUrl>
666*cc02d7e2SAndroid Build Coastguard Worker    <projectUrl>project url</projectUrl>
667*cc02d7e2SAndroid Build Coastguard Worker    <description>See project site for more info.</description>
668*cc02d7e2SAndroid Build Coastguard Worker    <summary>Example package containing proto files.</summary>
669*cc02d7e2SAndroid Build Coastguard Worker    <releaseNotes>Example package containing proto files</releaseNotes>
670*cc02d7e2SAndroid Build Coastguard Worker    <copyright>Copyright 2023, My Company.</copyright>
671*cc02d7e2SAndroid Build Coastguard Worker  </metadata>
672*cc02d7e2SAndroid Build Coastguard Worker
673*cc02d7e2SAndroid Build Coastguard Worker  <files>
674*cc02d7e2SAndroid Build Coastguard Worker    <!-- copy the My.Example.Protos.targets file for MSBuild integration -->
675*cc02d7e2SAndroid Build Coastguard Worker    <file src="build\**" target="build" />
676*cc02d7e2SAndroid Build Coastguard Worker    <!-- copy the .proto files into the package -->
677*cc02d7e2SAndroid Build Coastguard Worker    <file src="proto\**" target="content\protos" />
678*cc02d7e2SAndroid Build Coastguard Worker  </files>
679*cc02d7e2SAndroid Build Coastguard Worker</package>
680*cc02d7e2SAndroid Build Coastguard Worker```
681*cc02d7e2SAndroid Build Coastguard Worker
682*cc02d7e2SAndroid Build Coastguard WorkerMy.Example.Protos.targets:
683*cc02d7e2SAndroid Build Coastguard Worker```xml
684*cc02d7e2SAndroid Build Coastguard Worker<?xml version="1.0"?>
685*cc02d7e2SAndroid Build Coastguard Worker<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
686*cc02d7e2SAndroid Build Coastguard Worker  <!-- This targets file allows .proto files bundled in package,
687*cc02d7e2SAndroid Build Coastguard Worker  to be included in Grpc.Tools compilation. -->
688*cc02d7e2SAndroid Build Coastguard Worker
689*cc02d7e2SAndroid Build Coastguard Worker  <PropertyGroup>
690*cc02d7e2SAndroid Build Coastguard Worker    <!-- Define a property containing the path of the proto files.
691*cc02d7e2SAndroid Build Coastguard Worker         Content from the nupkg. -->
692*cc02d7e2SAndroid Build Coastguard Worker    <MyExampleProtos_ProtosPath>$( [System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)../content/protos) )</MyExampleProtos_ProtosPath>
693*cc02d7e2SAndroid Build Coastguard Worker  </PropertyGroup>
694*cc02d7e2SAndroid Build Coastguard Worker
695*cc02d7e2SAndroid Build Coastguard Worker  <!-- Run immediately before the Protobuf_BeforeCompile extension point. -->
696*cc02d7e2SAndroid Build Coastguard Worker  <!-- Only include protos if project has set <IncludeMyExampleProtosProtos>
697*cc02d7e2SAndroid Build Coastguard Worker       property to true. -->
698*cc02d7e2SAndroid Build Coastguard Worker  <Target Name="MyExampleProtos_BeforeCompile"
699*cc02d7e2SAndroid Build Coastguard Worker          BeforeTargets="Protobuf_BeforeCompile"
700*cc02d7e2SAndroid Build Coastguard Worker          Condition=" '$(IncludeMyExampleProtosProtos)' == 'true' ">
701*cc02d7e2SAndroid Build Coastguard Worker    <PropertyGroup>
702*cc02d7e2SAndroid Build Coastguard Worker      <!-- Add proto files by including path in Protobuf_StandardImportsPath.
703*cc02d7e2SAndroid Build Coastguard Worker           This path is passed to protoc via the -I option -->
704*cc02d7e2SAndroid Build Coastguard Worker      <Protobuf_StandardImportsPath>$(Protobuf_StandardImportsPath);$(MyPackage_ProtosPath)</Protobuf_StandardImportsPath>
705*cc02d7e2SAndroid Build Coastguard Worker    </PropertyGroup>
706*cc02d7e2SAndroid Build Coastguard Worker
707*cc02d7e2SAndroid Build Coastguard Worker    <!-- These message are not required but included here for diagnostics -->
708*cc02d7e2SAndroid Build Coastguard Worker    <Message Text="Included proto files at $(MyExampleProtos_ProtosPath) in import path." Importance="high" />
709*cc02d7e2SAndroid Build Coastguard Worker    <Message Text="Updated proto imports path: $(Protobuf_StandardImportsPath)" Importance="high" />
710*cc02d7e2SAndroid Build Coastguard Worker  </Target>
711*cc02d7e2SAndroid Build Coastguard Worker</Project>
712*cc02d7e2SAndroid Build Coastguard Worker```
713*cc02d7e2SAndroid Build Coastguard Worker
714*cc02d7e2SAndroid Build Coastguard Worker### Using the NuGet package
715*cc02d7e2SAndroid Build Coastguard Worker
716*cc02d7e2SAndroid Build Coastguard WorkerThe project needs to add the package containing the `.proto` files:
717*cc02d7e2SAndroid Build Coastguard Worker```xml
718*cc02d7e2SAndroid Build Coastguard Worker<PackageReference Include="My.Example.Protos" Version="1.0.0" />
719*cc02d7e2SAndroid Build Coastguard Worker```
720*cc02d7e2SAndroid Build Coastguard Worker
721*cc02d7e2SAndroid Build Coastguard WorkerIf the project only wants to compile the `.proto` files included in the package
722*cc02d7e2SAndroid Build Coastguard Workerthen all it needs to do is add the `<Protobuf>` items using the property defined
723*cc02d7e2SAndroid Build Coastguard Workerin the package for the path to the files. For example, if the NuGet package contained
724*cc02d7e2SAndroid Build Coastguard Workerthe file `greet.proto`, then the project should add:
725*cc02d7e2SAndroid Build Coastguard Worker
726*cc02d7e2SAndroid Build Coastguard Worker```xml
727*cc02d7e2SAndroid Build Coastguard Worker<Protobuf Include="$(MyExampleProtos_ProtosPath)/greet.proto" />
728*cc02d7e2SAndroid Build Coastguard Worker```
729*cc02d7e2SAndroid Build Coastguard Worker
730*cc02d7e2SAndroid Build Coastguard WorkerHowever if the provided `.proto` files are to be *imported* by the projects own `.proto`
731*cc02d7e2SAndroid Build Coastguard Workerfiles then the `Protobuf_StandardImportsPath` needs updated to add the directory
732*cc02d7e2SAndroid Build Coastguard Workercontaining the package's files.  This is done by setting to `true` the property
733*cc02d7e2SAndroid Build Coastguard Workerused in the package. For example, if the project has the local `.proto` file
734*cc02d7e2SAndroid Build Coastguard Worker`my_services.proto` and it imported a file from the package `common_message.proto`,
735*cc02d7e2SAndroid Build Coastguard Workerthen:
736*cc02d7e2SAndroid Build Coastguard Worker
737*cc02d7e2SAndroid Build Coastguard Worker```xml
738*cc02d7e2SAndroid Build Coastguard Worker<PropertyGroup>
739*cc02d7e2SAndroid Build Coastguard Worker  <!-- Update the Protobuf_StandardImportsPath -->
740*cc02d7e2SAndroid Build Coastguard Worker  <IncludeMyExampleProtosProtos>true</IncludeMyExampleProtosProtos>
741*cc02d7e2SAndroid Build Coastguard Worker</PropertyGroup>
742*cc02d7e2SAndroid Build Coastguard Worker
743*cc02d7e2SAndroid Build Coastguard Worker<ItemGroup>
744*cc02d7e2SAndroid Build Coastguard Worker  <!-- my_services.proto imports common_message.proto from the package
745*cc02d7e2SAndroid Build Coastguard Worker   My.Example.Protos -->
746*cc02d7e2SAndroid Build Coastguard Worker  <Protobuf Include="my_services.proto" />
747*cc02d7e2SAndroid Build Coastguard Worker</ItemGroup>
748*cc02d7e2SAndroid Build Coastguard Worker```
749*cc02d7e2SAndroid Build Coastguard Worker
750*cc02d7e2SAndroid Build Coastguard Worker---
751*cc02d7e2SAndroid Build Coastguard Worker
752*cc02d7e2SAndroid Build Coastguard Worker## See also
753*cc02d7e2SAndroid Build Coastguard Worker
754*cc02d7e2SAndroid Build Coastguard WorkergRPC project documentation:
755*cc02d7e2SAndroid Build Coastguard Worker* [gRPC for .NET](https://github.com/grpc/grpc-dotnet)
756*cc02d7e2SAndroid Build Coastguard Worker* [gRPC C# (legacy implementation using gRPC Core library)](https://github.com/grpc/grpc/blob/master/src/csharp/README.md)
757*cc02d7e2SAndroid Build Coastguard Worker* [Grpc.Tools internals](Grpc.Tools/implementation_notes.md) - implementation notes for how `Grpc.Tools` works
758*cc02d7e2SAndroid Build Coastguard Worker
759*cc02d7e2SAndroid Build Coastguard WorkerMicrosoft documentation:
760*cc02d7e2SAndroid Build Coastguard Worker* [Microsoft: Overview for gRPC on .NET](https://learn.microsoft.com/en-us/aspnet/core/grpc/)
761*cc02d7e2SAndroid Build Coastguard Worker* [Microsoft: C# Tooling support for .proto files](https://learn.microsoft.com/en-us/aspnet/core/grpc/basics#c-tooling-support-for-proto-files)
762