Introduction to gRPC on .NET Core

Introduction to gRPC on .NET Core

Introduction to gRPC on .NET Core: Learn about gRPC services with Kestrel server and the ASP.NET Core stack.

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.

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

gRPC not supported on Azure App Service

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.

gRPC dotnet netccore

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

gRPC: An Introduction With Scala Compiler

Does your application have a client-server architecture or microservices architecture or distributed architecture? Then, you must be using some kind of protocol for inter-application communication or you want to get started with one. So, Let’s start digging.

How to Build Microservices with gRPC and .NET

This gRPC and ASP.NET Core tutorial explains how to build Microservices with gRPC and .NET. Where does gRPC come from? What's different about gRPC, why this? What's next for .NET Core and gRPC / Roadmap? Are there any challenges running gRPC? Microservices based applications consist of multiple services and are built with a variety of programming languages. Based on the business use case, you can pick the most appropriate technology to build your service. gRPC can be one of those choices.

gRPC-Web for .NET Now Available

gRPC-Web for .NET is now officially released. Use gRPC in the browser with gRPC-Web and .NET today.

gRPC for ASP.NET Core, a new framework for high performance APIs

In this talk, I will introduce gRPC for ASP.NET Core, a new gRPC framework that Microsoft is contributing to the Cloud Native Computing Foundation. I will compare gRPC with familiar REST-based JSON APIs, and recommend scenarios for using gRPC over other technologies. You will see how to integrate gRPC services with existing ASP.NET Core technologies like dependency injection, logging, and authentication, as we use gRPC to create lightning fast microservices and frontend applications.

Akka-gRPC,Features of Akka-gRPC

Akka gRPC provides support for building streaming gRPC servers and clients on top of Akka Streams and Akka Http. Features of Akka-gRPC A generator, that starts from a protobuf service definitions, for: Model classes The service API as a Scala trait using Akka Stream Sources On the server side code to create an Akka HTTP route based on your implementation of the service On the client side, a client for the service gRPC Runtime implementation that uses Akka Http/2 support or the server side and grpc-netty-shaded for the client side.