diff --git a/aspnetcore/includes/run-the-app9.0.md b/aspnetcore/includes/run-the-app9.0.md new file mode 100644 index 000000000000..f2284876a802 --- /dev/null +++ b/aspnetcore/includes/run-the-app9.0.md @@ -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. + +--- diff --git a/aspnetcore/tutorials/grpc/grpc-start.md b/aspnetcore/tutorials/grpc/grpc-start.md index 9b1786091a3f..610cd9b3392e 100644 --- a/aspnetcore/tutorials/grpc/grpc-start.md +++ b/aspnetcore/tutorials/grpc/grpc-start.md @@ -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: @@ -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)] --- @@ -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: @@ -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:`, where `` is the localhost port number randomly assigned when the project is created and set in `Properties/launchSettings.json`. @@ -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: @@ -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 @@ -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 @@ -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 `` element that refers to the *greet.proto* file: @@ -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. @@ -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: @@ -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:/Greet.Greeter/SayHello application/grpc + Request starting HTTP/2 POST https://localhost:/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] @@ -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)] diff --git a/aspnetcore/tutorials/grpc/grpc-start/includes/grpc-start8.md b/aspnetcore/tutorials/grpc/grpc-start/includes/grpc-start8.md new file mode 100644 index 000000000000..3b641696593b --- /dev/null +++ b/aspnetcore/tutorials/grpc/grpc-start/includes/grpc-start8.md @@ -0,0 +1,306 @@ +:::moniker range="= aspnetcore-8.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: + +> [!div class="checklist"] +> * Create a gRPC Server. +> * Create a gRPC client. +> * Test the gRPC client with the gRPC Greeter service. + +## Prerequisites + +# [Visual Studio](#tab/visual-studio) + +[!INCLUDE[](~/includes/net-prereqs-vs-8.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)] + +--- + +## Create a gRPC service + +# [Visual Studio](#tab/visual-studio) + +* Start Visual Studio 2022 and select **New Project**. +* 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**. + +# [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). +* Change to the directory (`cd`) that will contain the project. +* Run the following commands: + + ```dotnetcli + dotnet new grpc -o GrpcGreeter + code -r GrpcGreeter + ``` + + The `dotnet new` command creates a new gRPC service in the *GrpcGreeter* folder. + + The `code` command opens the *GrpcGreeter* project folder in the current instance of Visual Studio Code. + +[!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)] + +The logs show the service listening on `https://localhost:`, where `` is the localhost port number randomly assigned when the project is created and set in `Properties/launchSettings.json`. + +```console +info: Microsoft.Hosting.Lifetime[0] + Now listening on: https://localhost: +info: Microsoft.Hosting.Lifetime[0] + Application started. Press Ctrl+C to shut down. +info: Microsoft.Hosting.Lifetime[0] + Hosting environment: Development +``` + +> [!NOTE] +> The gRPC template is configured to use [Transport Layer Security (TLS)](https://tools.ietf.org/html/rfc5246). gRPC clients need to use HTTPS to call the server. The gRPC service localhost port number is randomly assigned when the project is created and set in the *Properties\launchSettings.json* file of the gRPC service project. + +### Examine the project files + +*GrpcGreeter* project files: + +* `Protos/greet.proto`: defines the `Greeter` gRPC and is used to generate the gRPC server assets. For more information, see [Introduction to gRPC](xref:grpc/index). +* `Services` folder: Contains the implementation of the `Greeter` service. +* `appSettings.json`: Contains configuration data such as the protocol used by Kestrel. For more information, see . +* `Program.cs`, which contains: + * The entry point for the gRPC service. For more information, see . + * Code that configures app behavior. For more information, see [App startup](xref:fundamentals/startup). + +## Create the gRPC client in a .NET console app + +# [Visual Studio](#tab/visual-studio) + +* 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**. + +# [Visual Studio Code](#tab/visual-studio-code) + +* Open the [integrated terminal](https://code.visualstudio.com/docs/editor/integrated-terminal). +* Change directories (`cd`) to a folder for the project. +* Run the following commands: + + ```dotnetcli + dotnet new console -o GrpcGreeterClient + code -r GrpcGreeterClient + ``` + +[!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 + +The gRPC client project requires the following NuGet packages: + +* [Grpc.Net.Client](https://www.nuget.org/packages/Grpc.Net.Client), which contains the .NET Core client. +* [Google.Protobuf](https://www.nuget.org/packages/Google.Protobuf/), which contains protobuf message APIs for C#. +* [Grpc.Tools](https://www.nuget.org/packages/Grpc.Tools/), which contain C# tooling support for protobuf files. The tooling package isn't required at runtime, so the dependency is marked with `PrivateAssets="All"`. + +# [Visual Studio](#tab/visual-studio) + +Install the packages using either the Package Manager Console (PMC) or Manage NuGet Packages. + +#### PMC option to install packages + +* From Visual Studio, select **Tools** > **NuGet Package Manager** > **Package Manager Console** +* From the **Package Manager Console** window, run `cd GrpcGreeterClient` to change directories to the folder containing the `GrpcGreeterClient.csproj` files. +* Run the following commands: + + ```powershell + Install-Package Grpc.Net.Client + Install-Package Google.Protobuf + Install-Package Grpc.Tools + ``` + +#### Manage NuGet Packages option to install packages + +* Right-click the project in **Solution Explorer** > **Manage NuGet Packages**. +* Select the **Browse** tab. +* Enter **Grpc.Net.Client** in the search box. +* Select the **Grpc.Net.Client** package from the **Browse** tab and select **Install**. +* Repeat for `Google.Protobuf` and `Grpc.Tools`. + +# [Visual Studio Code](#tab/visual-studio-code) + +Run the following commands from the **Integrated Terminal**: + +```dotnetcli +dotnet add GrpcGreeterClient.csproj package Grpc.Net.Client +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 + +* Create a *Protos* folder in the gRPC client project. +* Copy the *Protos\greet.proto* file from the gRPC Greeter service to the *Protos* folder in the gRPC client project. +* Update the namespace inside the `greet.proto` file to the project's namespace: + + ```json + option csharp_namespace = "GrpcGreeterClient"; + ``` + +* Edit the `GrpcGreeterClient.csproj` project file: + +# [Visual Studio](#tab/visual-studio) + + Right-click the project and select **Edit Project File**. + +# [Visual Studio Code](#tab/visual-studio-code) + + 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 `` element that refers to the *greet.proto* file: + + ```xml + + + + ``` + +### Create the Greeter client + +* Build the client project to create the types in the `GrpcGreeterClient` namespace. + +> [!NOTE] +> The `GrpcGreeterClient` types are generated automatically by the build process. The tooling package [Grpc.Tools](https://www.nuget.org/packages/Grpc.Tools/) generates the following files based on the *greet.proto* file: +> +> * `GrpcGreeterClient\obj\Debug\[TARGET_FRAMEWORK]\Protos\Greet.cs`: The protocol buffer code which populates, serializes and retrieves the request and response message types. +> * `GrpcGreeterClient\obj\Debug\[TARGET_FRAMEWORK]\Protos\GreetGrpc.cs`: Contains the generated client classes. +> +> For more information on the C# assets automatically generated by [Grpc.Tools](https://www.nuget.org/packages/Grpc.Tools/), see [gRPC services with C#: Generated C# assets](xref:grpc/basics#generated-c-assets). + +* 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)] + +* 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. + +`Program.cs` contains the entry point and logic for the gRPC client. + +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)] + +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)] + +## 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)] + +# [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. + +# [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: + +```console +Greeting: Hello GreeterClient +Press any key to exit... +``` + +The gRPC service records the details of the successful call in the logs written to the command prompt: + +```console +info: Microsoft.Hosting.Lifetime[0] + Now listening on: https://localhost: +info: Microsoft.Hosting.Lifetime[0] + Application started. Press Ctrl+C to shut down. +info: Microsoft.Hosting.Lifetime[0] + Hosting environment: Development +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:/Greet.Greeter/SayHello application/grpc +info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0] + Executing endpoint 'gRPC - /Greet.Greeter/SayHello' +info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1] + Executed endpoint 'gRPC - /Greet.Greeter/SayHello' +info: Microsoft.AspNetCore.Hosting.Diagnostics[2] + Request finished in 78.32260000000001ms 200 application/grpc +``` + +> [!NOTE] +> The code in this article requires the ASP.NET Core HTTPS development certificate to secure the gRPC service. If the .NET gRPC client fails with the message `The remote certificate is invalid according to the validation procedure.` or `The SSL connection could not be established.`, the development certificate isn't trusted. To fix this issue, see [Call a gRPC service with an untrusted/invalid certificate](xref:grpc/troubleshoot#call-a-grpc-service-with-an-untrustedinvalid-certificate). + +### Next steps + +* View or download [the completed sample code for this tutorial](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/tutorials/grpc/grpc-start/sample) ([how to download](xref:index#how-to-download-a-sample)). +* +* +* + +:::moniker-end diff --git a/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/GrpcGreeter.csproj b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/GrpcGreeter.csproj new file mode 100644 index 000000000000..6ae05574958b --- /dev/null +++ b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/GrpcGreeter.csproj @@ -0,0 +1,17 @@ + + + + net9.0 + enable + enable + + + + + + + + + + + diff --git a/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/Program.cs b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/Program.cs new file mode 100644 index 000000000000..d28e71194b21 --- /dev/null +++ b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/Program.cs @@ -0,0 +1,14 @@ +using GrpcGreeter.Services; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddGrpc(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +app.MapGrpcService(); +app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); + +app.Run(); diff --git a/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/Protos/greet.proto b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/Protos/greet.proto new file mode 100644 index 000000000000..02343fa083fc --- /dev/null +++ b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/Protos/greet.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +option csharp_namespace = "GrpcGreeter"; + +package greet; + +// The greeting service definition. +service Greeter { + // Sends a greeting + rpc SayHello (HelloRequest) returns (HelloReply); +} + +// The request message containing the user's name. +message HelloRequest { + string name = 1; +} + +// The response message containing the greetings. +message HelloReply { + string message = 1; +} diff --git a/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/Services/GreeterService.cs b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/Services/GreeterService.cs new file mode 100644 index 000000000000..cd001d4e8b29 --- /dev/null +++ b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/Services/GreeterService.cs @@ -0,0 +1,23 @@ +using Grpc.Core; +using GrpcGreeter; + +namespace GrpcGreeter.Services; + +#region snippet +public class GreeterService : Greeter.GreeterBase +{ + private readonly ILogger _logger; + public GreeterService(ILogger logger) + { + _logger = logger; + } + + public override Task SayHello(HelloRequest request, ServerCallContext context) + { + return Task.FromResult(new HelloReply + { + Message = "Hello " + request.Name + }); + } +} +#endregion diff --git a/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/appsettings.Development.json b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/appsettings.Development.json new file mode 100644 index 000000000000..c1401263e40e --- /dev/null +++ b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/appsettings.Development.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Microsoft.AspNetCore.Hosting": "Information", + "Microsoft.AspNetCore.Routing.EndpointMiddleware": "Information" + } + } +} diff --git a/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/appsettings.json b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/appsettings.json new file mode 100644 index 000000000000..1aef5074f6fa --- /dev/null +++ b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeter/appsettings.json @@ -0,0 +1,14 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "Kestrel": { + "EndpointDefaults": { + "Protocols": "Http2" + } + } +} diff --git a/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/GrpcGreeterClient.csproj b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/GrpcGreeterClient.csproj new file mode 100644 index 000000000000..37a5d86af888 --- /dev/null +++ b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/GrpcGreeterClient.csproj @@ -0,0 +1,22 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + diff --git a/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/Program.cs b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/Program.cs new file mode 100644 index 000000000000..fa564594aaf2 --- /dev/null +++ b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/Program.cs @@ -0,0 +1,15 @@ +#region snippet2 +using Grpc.Net.Client; +using GrpcGreeterClient; + +#region snippet +// The port number must match the port of the gRPC server. +using var channel = GrpcChannel.ForAddress("https://localhost:7042"); +var client = new Greeter.GreeterClient(channel); +var reply = await client.SayHelloAsync( + new HelloRequest { Name = "GreeterClient" }); +Console.WriteLine("Greeting: " + reply.Message); +Console.WriteLine("Press any key to exit..."); +Console.ReadKey(); +#endregion +#endregion \ No newline at end of file diff --git a/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/Protos/greet.proto b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/Protos/greet.proto new file mode 100644 index 000000000000..1a015952296c --- /dev/null +++ b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/GrpcGreeterClient/Protos/greet.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +option csharp_namespace = "GrpcGreeterClient"; + +package greet; + +// The greeting service definition. +service Greeter { + // Sends a greeting + rpc SayHello (HelloRequest) returns (HelloReply); +} + +// The request message containing the user's name. +message HelloRequest { + string name = 1; +} + +// The response message containing the greetings. +message HelloReply { + string message = 1; +} diff --git a/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/readme.md b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/readme.md new file mode 100644 index 000000000000..88379a8e4efc --- /dev/null +++ b/aspnetcore/tutorials/grpc/grpc-start/sample/sample9/readme.md @@ -0,0 +1,23 @@ +--- +page_type: sample +description: "Sample projects for a gRPC Service and gRPC client on ASP.NET Core." +languages: +- csharp +products: +- dotnet-core +- aspnet-core +- vs +urlFragment: create-grpc-client +--- + +# Create a gRPC client and server in ASP.NET Core 9 + +This sample demonstrates a .NET Core [gRPC](https://grpc.io/docs/guides/) client and an ASP.NET Core gRPC Server. + +For a tutorial on this sample see [Tutorial: Create a gRPC client and server in ASP.NET Core](https://learn.microsoft.com/aspnet/core/tutorials/grpc/grpc-start?view=aspnetcore-9.0) + +### Docs help & next steps for gRPC + +* [Introduction to gRPC on ASP.NET Core](https://learn.microsoft.com/aspnet/core/grpc/) +* [gRPC services with C#](https://learn.microsoft.com/aspnet/core/grpc/basics/) +* [Migrating gRPC services from C-core to ASP.NET Core](https://learn.microsoft.com/aspnet/core/grpc/migration/)