Is it possible to create a GRPC Console app as a Server?

6.1k views Asked by At

I am using .NET Core 3.1 and want to create a GRPC Service with a Console App as the Server and a WPF App as the Client.

I can't see any examples online - for some reason all the Console Apps seem to connect and send messages, none are Servers.

Is it possible to have a Console App starting the Service and a WPF App connecting and sending a message to it?

I have downloaded below project and am trying to see if I can get a Console App to be the Server. [GRPC in .NET Core][1]

Any pointers appreciated. [1]: https://www.jenx.si/2019/06/14/experimenting-with-grpc-in-net-core/

The Console App below is now the Server but I am not able to read and store the Payload from the Client in the Main function - I can see it is received in from the Client.

How can I store the payload message from the client in the Main function?

//Console App Main function listening for

static async Task Main(string[] args)
{
        _gRpcServer = new Server
        {
            Services ={Jenx.Grpc.SimpleDemo.Contracts.JenxSimpleGrpcService.BindService(new ConsoleAppGrpcClient())},
            Ports = {new ServerPort("localhost", 505050, ServerCredentials.Insecure)}
        };
}

//receives Client Message successfully
public override Task<ReplyMessage> SendMessage(RequestMessage request, ServerCallContext context)
    {
        Console.WriteLine($"Message Received: {request.RequestPayload}");
        Console.WriteLine("Sending message back to the client...");

//<<---need to Store below in variable and return to Main function above--->>
        return Task.FromResult(new ReplyMessage { ResponsePayload = $"ServerSideTime is {DateTime.Now}" });
    }
5

There are 5 answers

2
Mauricio Kenny On

I hope you are well. About your ask, you can create a Console programm and use them to host a WCF Service to publish access to Client WPF or WinForm Client. In this architecture, you have 3 layers. The Database Layer, the middle Business Layer and Client Layer and the connection can be over TCP, or HTTPS, as you configure the End Points. Also it's possible host the WCF in a Windows Service or Daemon. In .NET Core I think you can create WCF service or similar and host them in a Console Program or Service.

1
CRM On

In .NET Core 3.1 a "gRPC Service" project is just a Console App with extra stuff added in the project template. A Console window shows up when you run it and you can write text to the console (usually through a Logger). If for some reason you can't use a new gRPC Service project you can still create a temporary one and cut and paste the extra files (Program.cs, Startup.cs, etc.) and Dependencies into your Console app.

0
Toine db On

I have been working from the start with console grpc server for testing purposes, first in .net 2.1 and now in 5.0.

    class Program
{
    const string ServerEndpoint = "localhost";
    const int Port = 50051;
    const string ExitCommand = "exit";

    static void Main(string[] args)
    {
        var serverStub = new MyGrpcServerStub();

        var server = new Server
        {
            Services =
            {
                grpcNameSpace.GrpcServer.BindService(serverStub),
            },
            Ports = {new ServerPort(ServerEndpoint, Port, ServerCredentials.Insecure)}
        };
        server.Start();

        var keyStroke = string.Empty;
        while (keyStroke != ExitCommand)
        {
            var random = new Random();
            keyStroke = Console.ReadLine();

            if (!string.IsNullOrWhiteSpace(keyStroke) && keyStroke != ExitCommand)
            {
                // Do some stuff to trigger things on the server side

            }
        }

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

Just inherit from the Grpc server base created by the Protobuf compiler, and overide all RPC calls from the proto like this:

    public class MyGrpcServerStub : grpcNameSpace.GrpcServer.MyXXXXBase
{

    public override Task<BoolValue> GetStuff(ProtoVersion request, ServerCallContext context)
    {
        return Task.FromResult(new BoolValue() { Value = true});
    }
}

For testing purposes I'm storing values inside the MyGrpcServerStub, but you can store it where ever you like.

2
Rui MGS Bras On

You should look at the Helloworld example in the gRPC GitHub repository. It contains a simple gRPC service where both the server part and the client part are C# console applications. Uses .NET SDK Core 2.1, but should work in .NET Core 3.1+ and .NET 5+ (I didn't try it on those versions).

1
HammerChi On

To store the message received from the client, you can add a received notification handler and within the handler, you could add your process for storing the received messages. Utilizing the MediatR framework