Working with gRPC in ASP.NET Core

April 14, 2019 by Anuraj

ASP.NET Core gRPC

This post is about Working with gRPC in ASP.NET Core. 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.

If you’re using Visual Studio 2019, you will get gRPC project templates to build services with gRPC. gRPC templates will only available in .NET Core 3.0 projects.

gRPC project template

Once you create a gRPC app, you will get two projects - one server project and one client project.

gRPC Application

In the server project, the proto files defines the services. protobuf aka Protocol Buffers is a method of serializing structured data. It is developed by Google. Here is the default Greet service which comes as part of the template.

syntax = "proto3";
package Greet;
service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

This definition is simple and straight forward. You’re creating a package, and a service with one SayHello method. This method accepts one parameter HelloRequest and returns a response object HelloReply. That data structures as well included in the method. You can generate code from proto files - if you’re using Visual Studio, when you build the project, it will generate the code for you. It will generate Greeter.GreeterBase and you need to write the implementation by inheriting from this class. So here is the implementation of GreeterService class.

public class GreeterService : Greeter.GreeterBase
{
    public override Task<HelloReply> SayHello(
        HelloRequest request, ServerCallContext context)
    {
        return Task.FromResult(new HelloReply
        {
            Message = "Hello " + request.Name
        });
    }
}

The ServerCallContextparameter helps you to access HTTP/2 message data. You won’t get the HttpContext from this parameter, there is one extension method available GetHttpContext() which helps you to access the HttpContext object.

In the server project, you can find reference of Grpc.AspNetCore.Server package and Google.Protobuf package. You will be able to find reference of Grpc.Tools package reference, which is used to generate code from proto files. In the client project similar to server you will be able to find reference of Grpc.Core package and Google.Protobuf package. And the Grpc.Tools as well. The proto files are added in a Protos folder in the root level. Visual Studio will display it is a shortcut in both server and client projects.

In the startup.cs file, you need to add the Grpc middleware in the ConfigureServices() method and you need to map the implemented services in the Configure() method.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddGrpc();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting(routes =>
        {
            routes.MapGrpcService<GreeterService>();
        });
    }
}

And in the client project, you can create an instance of GreeterClient class and invoke the SayHello method, like this.

static async Task Main(string[] args)
{
    var port = args.Length > 0 ? args[0] : "60051";

    var channel = new Channel("localhost:" + port, ChannelCredentials.Insecure);
    var client = new Greeter.GreeterClient(channel);

    var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
    Console.WriteLine("Greeting: " + reply.Message);

    await channel.ShutdownAsync();

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}

This post was a very basic introduction to gRPC in ASP.NET Core. Here are some helpful resources about gRPC on .NET and C#.

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub