Create a .NET Core gRPC client and server in ASP.NET Core (original) (raw)

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.

In this tutorial, you:

Prerequisites

Create a gRPC service

Run the service

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.

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:<port>
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. 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:

Create the gRPC client in a .NET console app

Add required NuGet packages

The gRPC client project requires the following NuGet packages:

Install the packages using either the Package Manager Console (PMC) or Manage NuGet Packages.

PMC option to install packages

Install-Package Grpc.Net.Client  
Install-Package Google.Protobuf  
Install-Package Grpc.Tools  

Manage NuGet Packages option to install packages

Add greet.proto

option csharp_namespace = "GrpcGreeterClient";  

Right-click the project and select Edit Project File.

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

Create the Greeter client

Note

The GrpcGreeterClient types are generated automatically by the build process. The tooling package Grpc.Tools generates the following files based on the greet.proto file:

For more information on the C# assets automatically generated by Grpc.Tools, see gRPC services with C#: Generated C# assets.

using Grpc.Net.Client;  
using GrpcGreeterClient;  
// 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();  

Program.cs contains the entry point and logic for the gRPC client.

The Greeter client is created by:

// 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();

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

// 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();

Test the gRPC client with the gRPC Greeter service

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

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Microsoft.AspNetCore.Hosting": "Information",
      "Microsoft.AspNetCore.Routing.EndpointMiddleware": "Information"
    }
  }
}

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:<port>
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:<port>/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 HTTP/2 POST https://localhost:7042/greet.Greeter/SayHello - 200 - application/grpc 40.4615ms

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.

Next steps

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.

In this tutorial, you:

Prerequisites

Create a gRPC service

Run the service

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.

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:<port>
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. 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:

Create the gRPC client in a .NET console app

Add required NuGet packages

The gRPC client project requires the following NuGet packages:

Install the packages using either the Package Manager Console (PMC) or Manage NuGet Packages.

PMC option to install packages

Install-Package Grpc.Net.Client  
Install-Package Google.Protobuf  
Install-Package Grpc.Tools  

Manage NuGet Packages option to install packages

Add greet.proto

option csharp_namespace = "GrpcGreeterClient";  

Right-click the project and select Edit Project File.

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

Create the Greeter client

Note

The GrpcGreeterClient types are generated automatically by the build process. The tooling package Grpc.Tools generates the following files based on the greet.proto file:

For more information on the C# assets automatically generated by Grpc.Tools, see gRPC services with C#: Generated C# assets.

using System.Threading.Tasks;  
using Grpc.Net.Client;  
using GrpcGreeterClient;  
// 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();  

Program.cs contains the entry point and logic for the gRPC client.

The Greeter client is created by:

// 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();

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

// 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();

Test the gRPC client with the gRPC Greeter service

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

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
      ,"Microsoft.AspNetCore.Hosting": "Information",
      "Microsoft.AspNetCore.Routing.EndpointMiddleware": "Information"
    }
  }
}

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:<port>
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:<port>/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.

Next steps

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.

In this tutorial, you:

Prerequisites

Create a gRPC service

Run the service

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.

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:<port>
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. 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.

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:

Create the gRPC client in a .NET console app

Add required NuGet packages

The gRPC client project requires the following NuGet packages:

Install the packages using either the Package Manager Console (PMC) or Manage NuGet Packages.

PMC option to install packages

Install-Package Grpc.Net.Client  
Install-Package Google.Protobuf  
Install-Package Grpc.Tools  

Manage NuGet Packages option to install packages

Add greet.proto

option csharp_namespace = "GrpcGreeterClient";  

Right-click the project and select Edit Project File.

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

Create the Greeter client

Note

The GrpcGreeterClient types are generated automatically by the build process. The tooling package Grpc.Tools generates the following files based on the greet.proto file:

For more information on the C# assets automatically generated by Grpc.Tools, see gRPC services with C#: Generated C# assets.

using System.Threading.Tasks;  
using Grpc.Net.Client;  
using GrpcGreeterClient;  
// 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();  

Program.cs contains the entry point and logic for the gRPC client.

The Greeter client is created by:

// 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();

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

// 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();

Test the gRPC client with the gRPC Greeter service

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

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
      ,"Microsoft.AspNetCore.Hosting": "Information",
      "Microsoft.AspNetCore.Routing.EndpointMiddleware": "Information"
    }
  }
}

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:<port>
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:<port>/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.

Next steps

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.

In this tutorial, you:

Prerequisites

Create a gRPC service

Run the service

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.

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:<port>
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. 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.

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:

Create the gRPC client in a .NET console app

Add required NuGet packages

The gRPC client project requires the following NuGet packages:

Install the packages using either the Package Manager Console (PMC) or Manage NuGet Packages.

PMC option to install packages

Install-Package Grpc.Net.Client  
Install-Package Google.Protobuf  
Install-Package Grpc.Tools  

Manage NuGet Packages option to install packages

Add greet.proto

option csharp_namespace = "GrpcGreeterClient";  

Right-click the project and select Edit Project File.

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

Create the Greeter client

Note

The GrpcGreeterClient types are generated automatically by the build process. The tooling package Grpc.Tools generates the following files based on the greet.proto file:

For more information on the C# assets automatically generated by Grpc.Tools, see gRPC services with C#: Generated C# assets.

using System.Threading.Tasks;  
using Grpc.Net.Client;  
using GrpcGreeterClient;  
// 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();  

Program.cs contains the entry point and logic for the gRPC client.

The Greeter client is created by:

// 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();

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

// 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();

Test the gRPC client with the gRPC Greeter service

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:<port>
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:<port>/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

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

"Microsoft.AspNetCore.Hosting": "Information",
"Microsoft.AspNetCore.Routing.EndpointMiddleware": "Information"

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.

Next steps

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:

Prerequisites

Create a gRPC service

Run the service

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

Examine the project files

GrpcGreeter project files:

Create the gRPC client in a .NET console app

Add required packages

The gRPC client project requires the following packages:

Install the packages using either the Package Manager Console (PMC) or Manage NuGet Packages.

PMC option to install packages

Install-Package Grpc.Net.Client  
Install-Package Google.Protobuf  
Install-Package Grpc.Tools  

Manage NuGet Packages option to install packages

Add greet.proto

option csharp_namespace = "GrpcGreeterClient";  

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

Create the Greeter client

Note

The GrpcGreeterClient types are generated automatically by the build process. The tooling package Grpc.Tools generates the following files based on the greet.proto file:

For more information on the C# assets automatically generated by Grpc.Tools, see gRPC services with C#: Generated C# assets.

using System;  
using System.Net.Http;  
using System.Threading.Tasks;  
using Grpc.Net.Client;  
namespace GrpcGreeterClient  
{  
    class Program  
    {  
        static async Task Main(string[] args)  
        {  
            // The port number(5001) must match the port of the gRPC server.  
            using var channel = GrpcChannel.ForAddress("https://localhost:5001");  
            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();  
        }  
    }  
}  

Program.cs contains the entry point and logic for the gRPC client.

The Greeter client is created by:

static async Task Main(string[] args)
{
    // The port number(5001) must match the port of the gRPC server.
    using var channel = GrpcChannel.ForAddress("https://localhost:5001");
    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();
}

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

static async Task Main(string[] args)
{
    // The port number(5001) must match the port of the gRPC server.
    using var channel = GrpcChannel.ForAddress("https://localhost:5001");
    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();
}

Test the gRPC client with the gRPC Greeter service

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 .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.

Next steps

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:

Prerequisites

Create a gRPC service

Run the service

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

Examine the project files

GrpcGreeter project files:

Create the gRPC client in a .NET console app

Add required packages

The gRPC client project requires the following packages:

Install the packages using either the Package Manager Console (PMC) or Manage NuGet Packages.

PMC option to install packages

Install-Package Grpc.Net.Client  
Install-Package Google.Protobuf  
Install-Package Grpc.Tools  

Manage NuGet Packages option to install packages

Add greet.proto

option csharp_namespace = "GrpcGreeterClient";  

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

Create the Greeter client

Note

The GrpcGreeterClient types are generated automatically by the build process. The tooling package Grpc.Tools generates the following files based on the greet.proto file:

For more information on the C# assets automatically generated by Grpc.Tools, see gRPC services with C#: Generated C# assets.

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

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Grpc.Net.Client;

namespace GrpcGreeterClient
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // The port number(5001) must match the port of the gRPC server.
            using var channel = GrpcChannel.ForAddress("https://localhost:5001");
            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();
        }
    }
}

Program.cs contains the entry point and logic for the gRPC client.

The Greeter client is created by:

static async Task Main(string[] args)
{
    // The port number(5001) must match the port of the gRPC server.
    using var channel = GrpcChannel.ForAddress("https://localhost:5001");
    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();
}

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

static async Task Main(string[] args)
{
    // The port number(5001) must match the port of the gRPC server.
    using var channel = GrpcChannel.ForAddress("https://localhost:5001");
    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();
}

Test the gRPC client with the gRPC Greeter service

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 .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.

Next steps