In their environments, many companies such as Cisco and Netflix are already using gRPC to connect multiple services. The use case varies from connecting a handful of services in on-prem or cloud environments to hundreds of services across different languages. Below are some of our early adopters ' details and quotes.

As we are testing the first preview bits of .NET Core 3.0 covering almost every .NET Core developer needs – starting with IoT, ML, Web to Desktop applications – Microsoft is contributing to the gRPC project to ensure first-class support for .NET developers will be provided by gRPC. This is a reply to a common question from customers which is how to have an RPC (as in .NET Remoting and Windows Communication Foundation) experience on .NET Core.

Prerequisites

I used the following tool and SDK:

  • Visual Studio Code Insiders
  • .NET Core SDK 3.0 preview

TargetFramework

The Hello World sample is made up with client and server .NET Core 3.0 preview console projects.

TargetFramework

Both client and server projects are using the same target framework .NET Core 3.0 preview.

GreeterClient

Creating the Server

The next step is to create an ASP.NET Core 3.0 server that will use code generated automatically from the proto file to reduce the amount of code that needs to be added.

using System;
using System.Threading.Tasks;
using Grpc.Core;
using Helloworld;

namespace GreeterServer
{
    class GreeterImpl : Greeter.GreeterBase
    {
        // Server side handler of the SayHello RPC
        public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
        {
            return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
        }
    }

    class Program
    {
        const int Port = 50051;

        public static void Main(string[] args)
        {
            Server server = new Server
            {
                Services = { Greeter.BindService(new GreeterImpl()) },
                Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
            };
            server.Start();

            Console.WriteLine("Greeter server listening on port " + Port);
            Console.WriteLine("Press any key to stop the server...");
            Console.ReadKey();

            server.ShutdownAsync().Wait();
        }
    }
}

Creating the Client

I created a basic. NET Core console app client to send request to the server. For this, the csproj file also had a reference to the proto file in order to generate the client code.

using System;
using Grpc.Core;
using Helloworld;

namespace GreeterClient
{
    class Program
    {
        public static void Main(string[] args)
        {
            Channel channel = new Channel("127.0.0.1:50051", ChannelCredentials.Insecure);

            var client = new Greeter.GreeterClient(channel);
            String user = "you";

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

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

Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data.

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

Running the Server

Since gRPC is a Server-side technology. We need first to run the server first.

dotnet run -p GreeterServer

The option "-p" or "--project " set the path to the project file to run (defaults to the current directory if there is only one project).

GreeterServer

Running the Client

dotnet run -p GreeterServer

The Client should be run in a different Visual Studio Code instance or at least in a different terminal.

dotnetrun

Summary

gRPC is a modern, high-performance open source RPC framework capable of running in any environment. With pluggable support for load balancing, tracing, health checking and authentication, it can efficiently connect services in and across data centers. It also applies to connecting devices, mobile applications and browsers to backend services in the last mile of distributed computing.