Skip to content
This repository was archived by the owner on Apr 9, 2021. It is now read-only.

Commit e4676e4

Browse files
authored
Merge pull request #806 from jtattermusch/master
update C# docs with Grpc.Tools automatic codegen
2 parents 8aa74da + 8d7b57e commit e4676e4

File tree

2 files changed

+35
-66
lines changed

2 files changed

+35
-66
lines changed

Diff for: docs/quickstart/csharp.md

+18-35
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ This document will walk you through the "Hello World" example.
4242
The projects and source files can be found in the `examples/csharp/Helloworld` directory.
4343

4444
The example in this walkthrough already adds the necessary
45-
dependencies for you (Grpc, Grpc.Tools and Google.Protobuf NuGet packages).
45+
dependencies for you (`Grpc`, `Grpc.Tools` and `Google.Protobuf` NuGet packages).
4646

4747
## Build the example
4848

@@ -57,8 +57,8 @@ From the `examples/csharp/Helloworld` directory:
5757
> dotnet build Greeter.sln
5858
```
5959

60-
*NOTE: If you want to use gRPC C# from a project that uses the old-style .csproj files (supported by Visual Studio 2013, 2015 and older versions of Mono), please refer to the
61-
[Greeter using legacy .csproj](https://github.com/grpc/grpc/blob/{{ site.data.config.grpc_release_tag }}/examples/csharp/HelloworldLegacyCsproj/README.md) example.*
60+
*NOTE: If you want to use gRPC C# from a project that uses the "classic" .csproj files (supported by Visual Studio 2013, 2015 and older versions of Mono), please refer to the
61+
[Greeter using "classic" .csproj](https://github.com/grpc/grpc/blob/{{ site.data.config.grpc_release_tag }}/examples/csharp/HelloworldLegacyCsproj/README.md) example.*
6262

6363
## Run a gRPC application
6464

@@ -139,44 +139,27 @@ message HelloReply {
139139

140140
Next we need to update the gRPC code used by our application to use the new service definition.
141141

142-
The `Grpc.Tools` NuGet package contains the protoc and protobuf C# plugin binaries you will need to generate the code.
142+
The `Grpc.Tools` NuGet package contains the protoc and protobuf C# plugin binaries needed
143+
to generate the code. Starting from version 1.17 the package also integrates with
144+
MSBuild to provide [automatic C# code generation](https://github.com/grpc/grpc/blob/master/src/csharp/BUILD-INTEGRATION.md)
145+
from `.proto` files.
143146

144-
### Obtaining the Grpc.Tools NuGet package
147+
This example project already depends on the `Grpc.Tools.{{ site.data.config.grpc_release_tag | remove_first: "v" }}` NuGet package so just re-building the solution
148+
is enough to regenerate the code from our modified `.proto` file.
145149

146-
This example project already depends on the `Grpc.Tools.{{ site.data.config.grpc_release_tag | remove_first: "v" }}` NuGet package
147-
and the package will be downloaded to your local NuGet cache as soon as you restore the nuget packages by clicking "Restore NuGet Packages" in Visual Studio or running `dotnet restore RouteGuide.sln` from the `examples/csharp/RouteGuide` directory.
150+
You can rebuild just like we first built the original
151+
example by running `dotnet build Greeter.sln` or by clicking "Build" in Visual Studio.
148152

149-
### Commands to generate the gRPC code
150-
Note that you may have to change the `platform_architecture` directory names (e.g. windows_x86, linux_x64) in the commands below based on your environment.
153+
The build regenerates the following files
154+
under the `Greeter/obj/Debug/TARGET_FRAMEWORK` directory:
151155

152-
From the `examples/csharp/Helloworld` directory:
153-
154-
**Windows**
155-
156-
```
157-
@rem Local nuget cache on Windows is located in %UserProfile%\.nuget\packages
158-
> %UserProfile%\.nuget\packages\grpc.tools\{{ site.data.config.grpc_release_tag | remove_first: "v" }}\tools\windows_x86\protoc.exe -I../../protos --csharp_out Greeter --grpc_out Greeter ../../protos/helloworld.proto --plugin=protoc-gen-grpc=%UserProfile%\.nuget\packages\grpc.tools\{{ site.data.config.grpc_release_tag | remove_first: "v" }}\tools\windows_x86\grpc_csharp_plugin.exe
159-
```
160-
161-
**Linux (or OS X by using macosx_x64 directory)**
162-
Note:
163-
protoc-gen-grpc plugin requires fullpath of "grpc_csharp_plugin" executable instead of short form with "~" symbol. Make sure to change <HOME_FOLDER phrase in below command according to your local machine.
164-
165-
```
166-
# Local nuget cache on Linux and Mac is located in ~/.nuget/packages
167-
$ ~/.nuget/packages/grpc.tools/{{ site.data.config.grpc_release_tag | remove_first: "v" }}/tools/linux_x64/protoc -I../../protos --csharp_out Greeter --grpc_out Greeter ../../protos/helloworld.proto --plugin=protoc-gen-grpc=${HOME}/.nuget/packages/grpc.tools/{{ site.data.config.grpc_release_tag | remove_first: "v" }}/tools/linux_x64/grpc_csharp_plugin
168-
```
169-
170-
Running the appropriate command for your OS regenerates the following files in
171-
the directory:
172-
173-
* Greeter/Helloworld.cs contains all the protocol buffer code to populate,
156+
* `Helloworld.cs` contains all the protocol buffer code to populate,
174157
serialize, and retrieve our request and response message types
175-
* Greeter/HelloworldGrpc.cs provides generated client and server classes,
158+
* `HelloworldGrpc.cs` provides generated client and server classes,
176159
including:
177-
* an abstract class Greeter.GreeterBase to inherit from when defining
160+
* an abstract class `Greeter.GreeterBase` to inherit from when defining
178161
Greeter service implementations
179-
* a class Greeter.GreeterClient that can be used to access remote Greeter
162+
* a class `Greeter.GreeterClient` that can be used to access remote Greeter
180163
instances
181164

182165
## Update and run the application
@@ -234,7 +217,7 @@ public static void Main(string[] args)
234217
### Rebuild the modified example
235218

236219
Rebuild the newly modified example just like we first built the original
237-
example by running `dotnet build Greeter.sln`.
220+
example by running `dotnet build Greeter.sln` or by clicking "Build" in Visual Studio.
238221

239222
### Run!
240223

Diff for: docs/tutorials/basic/csharp.md

+17-31
Original file line numberDiff line numberDiff line change
@@ -149,38 +149,24 @@ message Point {
149149
## Generating client and server code
150150

151151
Next we need to generate the gRPC client and server interfaces from our .proto
152-
service definition. We do this using the protocol buffer compiler `protoc` with
153-
a special gRPC C# plugin.
154-
155-
If you want to run this yourself, the `Grpc.Tools` NuGet package contains the
156-
binaries you will need to generate the code. You can fetch a copy of
157-
the `Grpc.Tools` package into your local nuget cache by clicking
158-
"Restore NuGet Packages" in Visual Studio or running `dotnet restore RouteGuide.sln`
159-
from the `examples/csharp/RouteGuide` directory. Once that's done, you can generate the C# code.
160-
161-
To generate the code, the following command should be run from the
162-
`examples/csharp/RouteGuide` directory:
163-
164-
- Windows
165-
166-
```
167-
@rem Local nuget cache on Windows is located in %UserProfile%\.nuget\packages
168-
> %UserProfile%\.nuget\packages\Grpc.Tools.{{ site.data.config.grpc_release_tag | remove_first: "v" }}\tools\windows_x86\protoc.exe -I../../protos --csharp_out RouteGuide --grpc_out RouteGuide ../../protos/route_guide.proto --plugin=protoc-gen-grpc=%UserProfile%\.nuget\packages\Grpc.Tools.{{ site.data.config.grpc_release_tag | remove_first: "v" }}\tools\windows_x86\grpc_csharp_plugin.exe
169-
```
170-
171-
- Linux (or Mac OS X by using `macosx_x64` directory).
172-
173-
```
174-
# Local nuget cache on Linux and Mac is located in ~/.nuget/packages
175-
$ ~/.nuget/packages/grpc.tools.{{ site.data.config.grpc_release_tag | remove_first: "v" }}/tools/linux_x64/protoc -I../../protos --csharp_out RouteGuide --grpc_out RouteGuide ../../protos/route_guide.proto --plugin=protoc-gen-grpc=~/.nuget/packages/grpc.tools.{{ site.data.config.grpc_release_tag | remove_first: "v" }}/tools/linux_x64/grpc_csharp_plugin
176-
```
177-
178-
Running the appropriate command for your OS regenerates the following files in
179-
the RouteGuide directory:
180-
181-
- `RouteGuide/RouteGuide.cs` contains all the protocol buffer code to populate,
152+
service definition. This can be done by invoking the protocol buffer compiler `protoc` with
153+
a special gRPC C# plugin from the command line, but starting from version
154+
1.17 the `Grpc.Tools` NuGet package integrates with MSBuild to provide [automatic C# code generation](https://github.com/grpc/grpc/blob/master/src/csharp/BUILD-INTEGRATION.md)
155+
from `.proto` files, which gives much better developer experience by running
156+
the right commands for you as part of the build.
157+
158+
This example already has a dependency on `Grpc.Tools` NuGet package and the
159+
`route_guide.proto` has already been added to the project, so the only thing
160+
needed to generate the client and server code is to build the solution.
161+
That can be done by running `dotnet build RouteGuide.sln` or building directly
162+
in Visual Studio.
163+
164+
The build regenerates the following files
165+
under the `RouteGuide/obj/Debug/TARGET_FRAMEWORK` directory:
166+
167+
- `RouteGuide.cs` contains all the protocol buffer code to populate,
182168
serialize, and retrieve our request and response message types
183-
- `RouteGuide/RouteGuideGrpc.cs` provides generated client and server classes,
169+
- `RouteGuideGrpc.cs` provides generated client and server classes,
184170
including:
185171
- an abstract class `RouteGuide.RouteGuideBase` to inherit from when defining
186172
RouteGuide service implementations

0 commit comments

Comments
 (0)