Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions aspnetcore/includes/run-the-app9.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# [Visual Studio](#tab/visual-studio)

* Press Ctrl+F5 to run without the debugger.

[!INCLUDE[](~/includes/trustCertVS.md)]

Visual Studio:

* Starts [Kestrel](xref:fundamentals/servers/index#kestrel) server.
* Launches a browser.
* Navigates to `http://localhost:port`, such as `http://localhost:7042`.
* *port*: A randomly assigned port number for the app.
* `localhost`: The standard hostname for the local computer. Localhost only serves web requests from the local computer.

# [Visual Studio Code](#tab/visual-studio-code)

[!INCLUDE[](~/includes/trustCertVSC.md)]

* Press **Ctrl-F5** to run without the debugger.

Visual Studio Code:

* Starts [Kestrel](xref:fundamentals/servers/index#kestrel) server.
* Launches a browser.
* Navigates to `http://localhost:port`, such as `http://localhost:7042`.
* *port*: A randomly assigned port number for the app.
* `localhost`: The standard hostname for the local computer. Localhost only serves web requests from the local computer.

---
78 changes: 21 additions & 57 deletions aspnetcore/tutorials/grpc/grpc-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ author: jamesnk
description: This tutorial shows how to create a gRPC Service and gRPC client on ASP.NET Core. Learn how to create a gRPC Service project, edit a proto file, and add a duplex streaming call.
monikerRange: '>= aspnetcore-3.0'
ms.author: wpickett
ms.date: 08/30/2023
ms.date: 01/30/2025
uid: tutorials/grpc/grpc-start
---
# Tutorial: Create a gRPC client and server in ASP.NET Core

:::moniker range=">= aspnetcore-8.0"
:::moniker range=">= aspnetcore-9.0"
This tutorial shows how to create a .NET Core [gRPC](xref:grpc/index) client and an ASP.NET Core gRPC Server. At the end, you'll have a gRPC client that communicates with the gRPC Greeter service.

In this tutorial, you:
Expand All @@ -23,15 +23,11 @@ In this tutorial, you:

# [Visual Studio](#tab/visual-studio)

[!INCLUDE[](~/includes/net-prereqs-vs-8.0.md)]
[!INCLUDE[](~/includes/net-prereqs-vs-9.0.md)]

# [Visual Studio Code](#tab/visual-studio-code)

[!INCLUDE[](~/includes/net-prereqs-vsc-8.0.md)]

# [Visual Studio for Mac](#tab/visual-studio-mac)

[!INCLUDE[](~/includes/net-prereqs-mac-8.0.md)]
[!INCLUDE[](~/includes/net-prereqs-vsc-9.0.md)]

---

Expand All @@ -43,13 +39,13 @@ In this tutorial, you:
* In the **Create a new project** dialog, search for `gRPC`. Select **ASP.NET Core gRPC Service** and select **Next**.
* In the **Configure your new project** dialog, enter `GrpcGreeter` for **Project name**. It's important to name the project *GrpcGreeter* so the namespaces match when you copy and paste code.
* Select **Next**.
* In the **Additional information** dialog, select **.NET 8.0 (Long Term Support)** and then select **Create**.
* In the **Additional information** dialog, select **.NET 9.0 (Standard Term Support)** and then select **Create**.

# [Visual Studio Code](#tab/visual-studio-code)

The tutorial assumes familiarity with VS Code. For more information, see [Getting started with VS Code](https://code.visualstudio.com/docs).

* Select **New Terminal** from the **Terminal** menu to open the [integrated terminal](https://code.visualstudio.com/docs/editor/integrated-terminal).
* Select **New Terminal** from the **Terminal** menu to open the [integrated terminal](https://code.visualstudio.com/docs/terminal/basics).
* Change to the directory (`cd`) that will contain the project.
* Run the following commands:

Expand All @@ -64,19 +60,11 @@ The tutorial assumes familiarity with VS Code. For more information, see [Gettin

[!INCLUDE[](~/includes/vscode-trust-authors-add-assets.md)]

# [Visual Studio for Mac](#tab/visual-studio-mac)

* Start Visual Studio 2022 for Mac and select **File** > **New Project**.
* In the **Choose a template for your new project** dialog, select **Web and Console** > **App** > **gRPC Service** and select **Continue**.
* Select **.NET 8.0** for the target framework and select **Continue**.
* Name the project **GrpcGreeter**. It's important to name the project *GrpcGreeter* so the namespaces match when you copy and paste code.
* Select **Continue**.

---

### Run the service

[!INCLUDE[](~/includes/run-the-app6.0.md)]
[!INCLUDE[](~/includes/run-the-app9.0.md)]

The logs show the service listening on `https://localhost:<port>`, where `<port>` is the localhost port number randomly assigned when the project is created and set in `Properties/launchSettings.json`.

Expand Down Expand Up @@ -110,11 +98,11 @@ info: Microsoft.Hosting.Lifetime[0]
* Open a second instance of Visual Studio and select **New Project**.
* In the **Create a new project** dialog, select **Console App**, and select **Next**.
* In the **Project name** text box, enter **GrpcGreeterClient** and select **Next**.
* In the **Additional information** dialog, select **.NET 8.0 (Long Term Support)** and then select **Create**.
* In the **Additional information** dialog, select **.NET 9.0 (Standard Term Support)** and then select **Create**.

# [Visual Studio Code](#tab/visual-studio-code)

* Open the [integrated terminal](https://code.visualstudio.com/docs/editor/integrated-terminal).
* Open the [integrated terminal](https://code.visualstudio.com/docs/terminal/basics).
* Change directories (`cd`) to a folder for the project.
* Run the following commands:

Expand All @@ -125,14 +113,6 @@ info: Microsoft.Hosting.Lifetime[0]

[!INCLUDE[](~/includes/vscode-trust-authors-add-assets.md)]

# [Visual Studio for Mac](#tab/visual-studio-mac)

* In Visual Studio 2022 for Mac select **File** > **Add** > **Project...**.
* In the **Choose a template for your new project** dialog, select **Web and Console** > **App** > **Console Application**, and select **Continue**.
* Select **.NET 8.0** for the target framework, and select **Continue**.
* Name the project **GrpcGreeterClient**. It's important to name the project *GrpcGreeterClient* so the namespaces match when you copy and paste code.
* Select **Continue**.

---

### Add required NuGet packages
Expand Down Expand Up @@ -177,15 +157,6 @@ dotnet add GrpcGreeterClient.csproj package Google.Protobuf
dotnet add GrpcGreeterClient.csproj package Grpc.Tools
```

# [Visual Studio for Mac](#tab/visual-studio-mac)

* Right-click **GrpcGreeterClient** project in the **Solution Pad** and select **Manage NuGet Packages**.
* Enter **Grpc.Net.Client** in the search box.
* Select the **Grpc.Net.Client** package from the results pane and select **Add Package**.
* In **Select Projects** select **OK**.
* If the **License Acceptance** dialog appears, select **Accept** if you agree to the license terms.
* Repeat for `Google.Protobuf` and `Grpc.Tools`.

---

### Add greet.proto
Expand All @@ -208,10 +179,6 @@ dotnet add GrpcGreeterClient.csproj package Grpc.Tools

Select the `GrpcGreeterClient.csproj` file.

# [Visual Studio for Mac](#tab/visual-studio-mac)

Right-click the project and select **Edit Project File**.

---

* Add an item group with a `<Protobuf>` element that refers to the *greet.proto* file:
Expand All @@ -236,7 +203,7 @@ dotnet add GrpcGreeterClient.csproj package Grpc.Tools

* Update the gRPC client `Program.cs` file with the following code.

[!code-csharp[](~/tutorials/grpc/grpc-start/sample/sample8/GrpcGreeterClient/Program.cs?name=snippet2&highlight=6)]
[!code-csharp[](~/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/Program.cs?name=snippet2&highlight=5)]

* In the preceding highlighted code, replace the localhost port number `7042` with the `HTTPS` port number specified in `Properties/launchSettings.json` within the `GrpcGreeter` service project.

Expand All @@ -247,33 +214,28 @@ The Greeter client is created by:
* Instantiating a `GrpcChannel` containing the information for creating the connection to the gRPC service.
* Using the `GrpcChannel` to construct the Greeter client:

[!code-csharp[](~/tutorials/grpc/grpc-start/sample/sample8/GrpcGreeterClient/Program.cs?name=snippet&highlight=1-3)]
[!code-csharp[](~/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/Program.cs?name=snippet&highlight=1-3)]

The Greeter client calls the asynchronous `SayHello` method. The result of the `SayHello` call is displayed:

[!code-csharp[](~/tutorials/grpc/grpc-start/sample/sample8/GrpcGreeterClient/Program.cs?name=snippet&highlight=4-7)]
[!code-csharp[](~/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/Program.cs?name=snippet&highlight=4-6)]

## Test the gRPC client with the gRPC Greeter service

Update the `appsettings.Development.json` file by adding the following highlighted lines:

[!code-csharp[](~/tutorials/grpc/grpc-start/sample/sample8/GrpcGreeter/appsettings.Development.json?highlight=6-7)]
[!code-csharp[](~/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/appsettings.Development.json?highlight=6-7)]

# [Visual Studio](#tab/visual-studio)

* In the Greeter service, press `Ctrl+F5` to start the server without the debugger.
* In the `GrpcGreeterClient` project, press `Ctrl+F5` to start the client without the debugger.
* In the `GrpcGreeter` service project, press `Ctrl+F5` to start the server without the debugger.
* In the `GrpcGreeterClient` console project, press `Ctrl+F5` to start the client without the debugger.

# [Visual Studio Code](#tab/visual-studio-code)

* Start the Greeter service.
* Start the client.

# [Visual Studio for Mac](#tab/visual-studio-mac)

* Start the Greeter service.
* Start the client.

---

The client sends a greeting to the service with a message containing its name, *GreeterClient*. The service sends the message "Hello GreeterClient" as a response. The "Hello GreeterClient" response is displayed in the command prompt:
Expand All @@ -295,13 +257,13 @@ info: Microsoft.Hosting.Lifetime[0]
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\GH\aspnet\docs\4\Docs\aspnetcore\tutorials\grpc\grpc-start\sample\GrpcGreeter
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/2 POST https://localhost:<port>/Greet.Greeter/SayHello application/grpc
Request starting HTTP/2 POST https://localhost:<port>/greet.Greeter/SayHello application/grpc
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint 'gRPC - /Greet.Greeter/SayHello'
Executing endpoint 'gRPC - /greet.Greeter/SayHello'
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint 'gRPC - /Greet.Greeter/SayHello'
Executed endpoint 'gRPC - /greet.Greeter/SayHello'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 78.32260000000001ms 200 application/grpc
Request finished HTTP/2 POST https://localhost:7042/greet.Greeter/SayHello - 200 - application/grpc 40.4615ms
```

> [!NOTE]
Expand All @@ -316,6 +278,8 @@ info: Microsoft.AspNetCore.Hosting.Diagnostics[2]

:::moniker-end

[!INCLUDE[](~/tutorials/grpc/grpc-start/includes/grpc-start8.md)]

[!INCLUDE[](~/tutorials/grpc/grpc-start/includes/grpc-start7.md)]

[!INCLUDE[](~/tutorials/grpc/grpc-start/includes/grpc-start6.md)]
Expand Down
Loading