Introduction to gRPC on .NET Core

09/20/2019

2 minutes to read

+2



In this article

By John Luo and James Newton-King

gRPC is a language agnostic, high-performance Remote Procedure Call (RPC) framework.

The main benefits of gRPC are:

Modern, high-performance, lightweight RPC framework.

Contract-first API development, using Protocol Buffers by default, allowing for language agnostic implementations.

Tooling available for many languages to generate strongly-typed servers and clients.

Supports client, server, and bi-directional streaming calls.

Reduced network usage with Protobuf binary serialization.

These benefits make gRPC ideal for:

Lightweight microservices where efficiency is critical.

Polyglot systems where multiple languages are required for development.

Point-to-point real-time services that need to handle streaming requests or responses.

Warning ASP.NET Core gRPC is not currently supported on Azure App Service or IIS. The HTTP/2 implementation of Http.Sys does not support HTTP response trailing headers which gRPC relies on. For more information, see this GitHub issue.

C# Tooling support for .proto files

gRPC uses a contract-first approach to API development. Services and messages are defined in *.proto files:

syntax = "proto3"; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply); } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }

.NET types for services, clients and messages are automatically generated by including *.proto files in a project:

Add a package reference to Grpc.Tools package.

Add *.proto files to the <Protobuf> item group.

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

For more information on gRPC tooling support, see gRPC services with C#.

gRPC services on ASP.NET Core

gRPC services can be hosted on ASP.NET Core. Services have full integration with popular ASP.NET Core features such as logging, dependency injection (DI), authentication and authorization.

The gRPC service project template provides a starter service:

public class GreeterService : Greeter.GreeterBase { private readonly ILogger<GreeterService> _logger; public GreeterService(ILogger<GreeterService> logger) { _logger = logger; } public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) { _logger.LogInformation("Saying hello to {Name}", request.Name); return Task.FromResult(new HelloReply { Message = "Hello " + request.Name }); } }

GreeterService inherits from the GreeterBase type, which is generated from the Greeter service in the *.proto file. The service is made accessible to clients in Startup.cs:

app.UseEndpoints(endpoints => { endpoints.MapGrpcService<GreeterService>(); });

To learn more about gRPC services on ASP.NET Core, see gRPC services with ASP.NET Core.

Call gRPC services with a .NET client

gRPC clients are concrete client types that are generated from *.proto files. The concrete gRPC client has methods that translate to the gRPC service in the *.proto file.

var channel = GrpcChannel.ForAddress("https://localhost:5001"); var client = new Greeter.GreeterClient(channel); var response = await client.SayHelloAsync( new HelloRequest { Name = "World" }); Console.WriteLine(response.Message);

A gRPC client is created using a channel, which represents a long-lived connection to a gRPC service. A channel can be created using GrpcChannel.ForAddress .

For more information on creating clients, and calling different service methods, see Call gRPC services with the .NET client.

Additional resources