Skip to content

Latest commit

 

History

History
303 lines (201 loc) · 11.5 KB

grpc-start.md

File metadata and controls

303 lines (201 loc) · 11.5 KB
title author description ms.author ms.date uid
Create a .NET Core gRPC client and server in ASP.NET Core
juntaoluo
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.
johluo
12/05/2019
tutorials/grpc/grpc-start

Tutorial: Create a gRPC client and server in ASP.NET Core

By John Luo

This tutorial shows how to create a .NET Core gRPC client and an ASP.NET Core gRPC Server.

At the end, you'll have a gRPC client that communicates with the gRPC Greeter service.

View or download sample code (how to download).

In this tutorial, you:

[!div class="checklist"]

  • Create a gRPC Server.
  • Create a gRPC client.
  • Test the gRPC client service with the gRPC Greeter service.

Prerequisites

[!INCLUDE]

[!INCLUDE]

[!INCLUDE]


Create a gRPC service

  • Start Visual Studio and select Create a new project. Alternatively, from the Visual Studio File menu, select New > Project.

  • In the Create a new project dialog, select gRPC Service and select Next:

    Create a new project dialog

  • Name the project GrpcGreeter. It's important to name the project GrpcGreeter so the namespaces will match when you copy and paste code.

  • Select Create.

  • In the Create a new gRPC service dialog:

    • The gRPC Service template is selected.
    • Select Create.
  • Open the integrated terminal.

  • Change directories (cd) to a folder which will contain the project.

  • Run the following commands:

    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 folder in a new instance of Visual Studio Code.

    A dialog box appears with Required assets to build and debug are missing from 'GrpcGreeter'. Add them?

  • Select Yes.

From a terminal, run the following commands:

dotnet new grpc -o GrpcGreeter
cd GrpcGreeter

The preceding commands use the .NET Core CLI to create a gRPC service.

Open the project

From Visual Studio, select File > Open, and then select the GrpcGreeter.csproj file.


Run the service

[!INCLUDE]

The logs show the service listening on https://localhost:5001.

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:5001
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). gRPC clients need to use HTTPS to call the server.

macOS doesn't support ASP.NET Core gRPC with TLS. Additional configuration is required to successfully run gRPC services on macOS. For more information, see Unable to start ASP.NET Core gRPC app on macOS.

Examine the project files

GrpcGreeter project files:

  • greet.proto – The Protos/greet.proto file defines the Greeter gRPC and is used to generate the gRPC server assets. For more information, see Introduction to gRPC.
  • Services folder: Contains the implementation of the Greeter service.
  • appSettings.json – Contains configuration data, such as protocol used by Kestrel. For more information, see xref:fundamentals/configuration/index.
  • Program.cs – Contains the entry point for the gRPC service. For more information, see xref:fundamentals/host/generic-host.
  • Startup.cs – Contains code that configures app behavior. For more information, see App startup.

Create the gRPC client in a .NET console app

  • Open a second instance of Visual Studio and select Create a new project.
  • In the Create a new project dialog, select Console App (.NET Core) and select Next.
  • In the Name text box, enter GrpcGreeterClient and select Create.
  • Open the integrated terminal.

  • Change directories (cd) to a folder which will contain the project.

  • Run the following commands:

    dotnet new console -o GrpcGreeterClient
    code -r GrpcGreeterClient
    

Follow the instructions in Building a complete .NET Core solution on macOS using Visual Studio for Mac to create a console app with the name GrpcGreeterClient.


Add required packages

The gRPC client project requires the following packages:

  • Grpc.Net.Client, which contains the .NET Core client.
  • Google.Protobuf, which contains protobuf message APIs for C#.
  • Grpc.Tools, which contains C# tooling support for protobuf files. The tooling package isn't required at runtime, so the dependency is marked with PrivateAssets="All".

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:

    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.

Run the following commands from the Integrated Terminal:

dotnet add GrpcGreeterClient.csproj package Grpc.Net.Client
dotnet add GrpcGreeterClient.csproj package Google.Protobuf
dotnet add GrpcGreeterClient.csproj package Grpc.Tools
  • Right-click the Packages folder in Solution Pad > Add Packages
  • Enter Grpc.Net.Client in the search box.
  • Select the Grpc.Net.Client package from the results pane and select Add Package
  • 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 gRPC client project.

  • Edit the GrpcGreeterClient.csproj project file:

    Right-click the project and select Edit Project File.

    Select the GrpcGreeterClient.csproj file.

    Right-click the project and select Tools > Edit File.


  • Add an item group with a <Protobuf> element that refers to the greet.proto file:

    <ItemGroup>
      <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
    </ItemGroup>

Create the Greeter client

Build the project to create the types in the GrpcGreeter namespace. The GrpcGreeter types are generated automatically by the build process.

Update the gRPC client Program.cs file with the following code:

[!code-csharp]

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]

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

[!code-csharp]

Test the gRPC client with the gRPC Greeter service

  • 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.
  • Start the Greeter service.
  • Start the client.
  • 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:

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:

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:5001
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:5001/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 client fails with the message The remote certificate is invalid according to the validation procedure., the development certificate is not trusted. For instructions to fix this issue, see Trust the ASP.NET Core HTTPS development certificate on Windows and macOS.

[!INCLUDE]

Next steps

  • xref:grpc/index
  • xref:grpc/basics
  • xref:grpc/migration