Pseudorandom Knowledge

Where the g stands for gRPC

.NET 5 marks the end of .NET Framework and, since it isn't getting ported, the end of WCF as well. Microsoft recommend we move to REST (ASP.NET Core Web API) or gRPC instead.

gRPC is a Remote Procedure Call framework running over HTTP/2. Services are described in a language called Protocol Buffers which are used to generate partial implementations in a variety of languages.

gRPC in .NET

gRPC and Protocol Buffers integrates nicely with Visual Studio and C#. The following NuGet packages are necessary:

The service is described in a .proto file using Protocol Buffers syntax:

syntax = "proto3";

message GreetRequest {
    string name = 1;
}

message GreetResponse {
    string text = 1;
}
                
service Greeter {
    rpc Greet(GreetRequest) returns (GreetResponse) {}
}

On the server side the .proto file is seamlessly compiled to a base class that can be inherited to implement the service. Then the connection parameters gets defined and the server is started.

public class GreeterService : Greeter.GreeterBase
{
    public override Task Greet(GreetRequest request, ServerCallContext context)
    {
        return Task.FromResult(new GreetResponse
        {
            Text = $"Greetings {request.Name}!"
        });
    }
}

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

        Console.ReadLine();
    }
}

On the client side the same .proto file is compiled to a class that can be instantiated with the connection parameters. Calling the service is then as simple as calling a method. Which is, after all, the point of remote procedure calls.

public class Program
{
    public static void Main(string[] args)
    {
        var client = new Greeter.GreeterClient(
            new Channel("localhost:50051", ChannelCredentials.Insecure)                
        );

        var reply = client.Greet(new GreetRequest { Name = "Earth" });

        Console.WriteLine(reply.Text);
        Console.ReadLine();
    }
}

gRPC and streaming BLOBs

WCF is huge. That's the reason Microsoft don't want to port it. gRPC, on the other hand, isn't huge. It follows that gRPC can't do everything WCF does.

One thing that's missing is the ability to stream continous binary data. WCF has a streaming transfer mode that makes it possible to connect a Stream on the server side to a Stream on the client side. gRPC doesn't have any such ability natively, the stream would have to be divided into discrete messages manually.